- uses OpenGL12, SysUtils, Windows, Commdlg, Forms, Graphics,
- Printers, Classes;
- //...
- procedure PrintGL(SetupProc,RenderProc : TPrintGLProcedure);
- var
- PFDescriptor: TPixelFormatDescriptor;
- cxPage,cyPage,nPixelFormat : integer;
- hPRC : HGLRC;
- MyBitmap : TBitmap;
- begin
- Printer.Orientation := poLandscape;
- Printer.BeginDoc;
- MyBitmap := nil;
- try
- // Get the dimensions of the page
- cxPage := GetDeviceCaps(Printer.Handle, HORZRES);
- cyPage := GetDeviceCaps(Printer.Handle, VERTRES);
- MyBitmap := TBitmap.Create;
- MyBitmap.Width := cxPage div 4;
- MyBitmap.Height := cyPage div 4;
- MyBitmap.PixelFormat := pf24bit;
- FillChar(PFDescriptor, SizeOf(PFDescriptor), 0);
- with PFDescriptor do
- begin
- nSize := SizeOf(PFDescriptor);
- nVersion := 1;
- dwFlags := PFD_SUPPORT_OPENGL or PFD_SUPPORT_GDI or
- PFD_TYPE_RGBA or PFD_DRAW_TO_BITMAP;
- cColorBits := 24;
- cDepthBits := 16;
- end;
- // Choose a pixel format that best matches that described in pfd
- nPixelFormat := ChoosePixelFormat(MyBitmap.Canvas.Handle, @PFDescriptor);
- // Watch for no pixel format available for this printer
- if nPixelFormat = 0 then
- raise Exception.Create('Print error: no pixel format');
- // Set the pixel format for the device context, but watch for failure
- if not SetPixelFormat(MyBitmap.Canvas.Handle, nPixelFormat, @PFDescriptor) then
- begin
- DescribePixelFormat(MyBitmap.Canvas.Handle,nPixelFormat,PFDescriptor.nSize,PFDescriptor);
- if not SetPixelFormat(MyBitmap.Canvas.Handle, nPixelFormat, @PFDescriptor) then
- raise Exception.Create('Print error: no pixel format');
- end;
- // Create the Rendering context and make it current
- hPRC := wglCreateContext(MyBitmap.Canvas.Handle);
- if HasActiveContext then
- DeActivateRenderingContext;
- ActivateRenderingContext(MyBitmap.Canvas.Handle, hPRC);
- // Setup rendering context state
- SetupProc;
- // Perform the OpenGL Commands
- RenderProc;
- glFinish();
- // Cleanup the rendering context for the printer
- CheckGLError;
- DeactivateRenderingContext;
- DestroyRenderingContext(hPRC);
- // Printer.Canvas.Draw(0,0,MyBitmap);
- Printer.Canvas.StretchDraw( Rect(0,0,cxPage,cyPage), MyBitmap);
- //MyBitmap.SaveToFile('c:\test.bmp');
- Printer.EndDoc;
- finally
- if Printer.Printing then
- Printer.Abort;
- MyBitmap.Free;
- end;
- end;
Jag behöver kunna printa ut OpenGL-grafik. Utskrifterna ska använda skrivarens upplösning så det går inte att bara göra en skärmdump. Det är i Win32-miljö.
När jag googlar hittar jag olika exempel på att gå via bitmappar och metafiler, men när jag provat så har det inte fungerat. Det verkar vara i hög grad beroende av Windows-version och skrivardrivrutin ifall det ska lyckas.
Är det någon som gjort detta? Tacksam för ett kod-exempel i så fall.
