PDA

View Full Version : How to draw on PaintBox on Delphi?



earth_walker
08-05-2003, 11:53 AM
Hi! I'm using Delphi 5.0. I tried to draw on a TPaintBox object but failed. Before trying this I have successfully drawn on TForm and TImage. I cannot find what the problem is. Can you help me to see the code?
Thank you!

This is the simplified code to identify the problem, when I runs the program, it immediately give the error message defined in OnCreate event.



unit testPaintBox1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, OpenGL;

type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
procedure PaintBox1Paint(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
GLContext : HGLRC;
ErrorCode: GLenum;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
ShowMessage('onPaint');
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glFlush;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
FormatIndex: integer;
begin
fillchar(pfd,SizeOf(pfd),0);
with pfd do
begin
nSize := SizeOf(pfd);
nVersion := 1; {The current version of the desccriptor is 1}
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 24; {support 24-bit color}
cDepthBits := 32; {depth of z-axis}
iLayerType := PFD_MAIN_PLANE;
end; {with}
FormatIndex := ChoosePixelFormat(Form1.PaintBox1.Canvas.Handle,@p fd);
SetPixelFormat(Form1.PaintBox1.Canvas.Handle,Forma tIndex,@pfd);
GLContext := wglCreateContext(Form1.PaintBox1.Canvas.Handle);
wglMakeCurrent(Form1.PaintBox1.Canvas.Handle,GLCon text);
errorcode:= glGetError;
if errorcode<>GL_NO_ERROR then ShowMessage('gl error occurs');
end;

end.