function SetupPixelFormat(var dc:HDC):Boolean; var ppfd:PPIXELFORMATDESCRIPTOR; npixelformat:Integer; begin New(ppfd); ppfd^.nSize:=sizeof(PIXELFORMATDESCRIPTOR); ppfd^.nVersion:=1; ppfd^.dwFlags:=PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER; ppfd^.dwLayerMask:=PFD_MAIN_PLANE; ppfd^.iPixelType:=PFD_TYPE_COLORINDEX; ppfd^.cColorBits:=8; ppfd^.cDepthBits:=16; ppfd^.cAccumBits:=0; ppfd^.cStencilBits:=0; npixelformat:=ChoosePixelFormat(dc, ppfd); if (nPixelformat=0) then begin MessageBox(NULL, ’choosePixelFormat failed’, ’Error’, MB_OK); Result:=False; Exit; end; if (SetPixelFormat(dc, npixelformat, ppfd)= FALSE) then begin MessageBox(NULL, ’SetPixelFormat failed’, ’Error’, MB_OK); Result:=False; Exit; end; Result:=True; Dispose(ppfd); end;
也可以向下面这样进行设置如:
var pfd: PixelFormatDescriptor; nPixelFormat : Integer; begin FillChar(pfd,SizeOf(pfd),0); with pfd do begin nSize:=sizeof(pfd); nVersion:=1; dwFlags:=PFD_SUPPORT_OPENGL or PFD_DRAW_TO_BITMAP or PFD_DOUBLEBUFFER; iPixelType:=PFD_TYPE_RGBA; cColorBits:=32; cDepthBits:=32; iLayerType:=Byte(PFD_MAIN_PLANE); end; nPixelFormat:=ChoosePixelFormat(DC,@pfd); SetPixelFormat(DC,nPixelFormat,@pfd); { // 使用DescribePixelFormat检查象素格式是否设置正确 DescribePixelFormat(DC,nPixelFormat,SizeOf(pfd),@pfd); if (pfd.dwFlags and PFD_NEED_PALETTE) < > 0 then SetupPalette(DC,pfd); //SetupPalette是自定义函数 }end;