OpenGL cannot be initialized in Delphi5

Hi! I’m using Delphi5 to do OpenGL programming. The following code is from a book, it have been working, but doesn’t work now. Can you help to see how to fix it?
Thank you!

procedure TForm1.FormCreate(Sender: TObject);
pfd: TPixelFormatDescriptor;
FormatIndex: integer;

FormatIndex := ChoosePixelFormat(Canvas.Handle,@pfd);
GLContext := wglCreateContext(Canvas.Handle);

{error checking}
errorCode := glGetError;
if errorCode<>GL_NO_ERROR then
raise Exception.Create(‘Error in Paint’#13+
end; {FormCreate}

The information given by watching variables:

also, the ShowMessage statement tells that both vendor and version returns empty string.

I remember this program worked in summer this year, but it doesn’t work during these couple month, is it possible to because of the update of Windows? I’m under Windows 2000


Don’t use the handle of the form’s canvas, use the form’s handle itself instead. I see a lot of people using the handle of a canvas for creating their contexts, but that’s wrong. So for example just do

 FormatIndex := ChoosePixelFormat(Handle,@pfd); 

Thank you!
I tried to modify it but it doesn’t work…
Here is the code after modification,can you check it? Thank you!

FormatIndex := ChoosePixelFormat(HDC(Handle),@pfd);
GLContext := wglCreateContext(HDC(Handle));

{error checking}
errorCode := glGetError;
if errorCode<>GL_NO_ERROR then
raise Exception.Create(‘Error in Paint’#13+

running result:
FormatIndex becomes 0, other variables do not change.

This is another approach, still failed…

FormatIndex := ChoosePixelFormat(GetDC(Handle),@pfd);
GLContext := wglCreateContext(GetDC(Handle));

Try this :

DC := GetDC(Handle);
FormatIndex := ChoosePixelFormat(DC,@pfd);
GLContext := wglCreateContext(DC);
wglMakeCurrent(DC, GLContext);

Using a call to GetDC in every function isn’t good, since you’ll most likely get different DCs which all take up resource you can’t easily free later. And moreover, SetPixelFormat, wglCreateContext and wglMakeCurrent also need the DC instead of just the handle.
If the above approach isn’t working, then make sure that your pixelformat is really supported and also valid.

[This message has been edited by PanzerSchreck (edited 12-29-2003).]

Thank you! It works under Win2K now. I will test it in other versions of windows later.

It can be also usefull using RaiseOSError(or RaiseLastWin32Error) after every windows API call to catch windows exceptions.

I dont see any pfd fields definitions… Dont you knew that at least dwFlags must be set?