Here’s some more detail of using a second form to access wglChoosePixelFormatARB.
This is based on your original code, in your last post you use windows functions to create a new window, but its much simpler to just let delphi create it for you.
From the menu select File/New/Form to create a new form unit.
Select View/Units, select the program name from the list and press OK.
This displays the program DPR file which should have two lines like this:
Application.CreateForm(TForm2, Form2);
Application.CreateForm(TmainGL, mainGL);
They will probably be in the wrong order, so swap the two lines so that the new form is created before your mainGL form.
Both Forms should have visible=true in the object inspector.
Duplicate the mainGL variables in TForm2:
TForm2 = class(TForm)
private
public
DC : HDC;
hrc: HGLRC;
end;
Add to Unit2:
implementation
uses main;
The FormCreate methods of TmainGL and TForm2 should be as follows:
procedure TForm2.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
begin
//Setup a temporary context so we can call wglChoosePixelFormatARB
FillChar(pfd, SizeOf(pfd), 0);
DC := GetDC (Handle);
SetPixelFormat( DC, 1, @PFD ); {Dont care what pixelformat is, as long as its hardware accelerated}
hrc := wglCreateContext(DC);
wglMakeCurrent(DC, hrc);
end;
procedure TmainGL.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
begin
//
DC := GetDC (Handle);
SetDCPixelFormat(DC, true);
hrc := wglCreateContext(DC);
wglMakeCurrent(DC, hrc);
//Setup the OpenGL context
Init;
end;
To prevent flicker you should also override the EraseBackground message handler, and if you want to change any of the window class parameters you can also override CreateParams:
TmainGL = class(TForm)
procedure CreateParams(var Params: TCreateParams); override;
private
{ Private declarations }
procedure ERASEBKGND(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
end;
procedure TmainGL.ERASEBKGND(var Message: TWMEraseBkGnd);
begin
Message.Result := 1 {This prevents flicker from windows floodfilling client area}
end;
procedure TmainGL.CreateParams(var Params: TCreateParams);
begin
inherited; {This sets-up most of class, we just add OwnDC and any other flags we want to the class}
Params.WindowClass.style := CS_HReDraw + CS_VReDraw + CS_OwnDC + CS_DBLCLKS;
Params.Style := Params.Style or WS_CLIPCHILDREN;
end;