How to handle the changing of resolution under windows?

Hi! I wrote a program with Delphi under windows, and put some error checking routine in it.

If I run the program, then change the resolution of windows desktop. The error message pops up.

Is there any mechanism to handle this?

Thank you!

Not sure if this helps but here you go. I would add some code that changes the users settings then resets after the program is terminated. It would take care of that error message, and allow your program to run without error.

Sorry, I don’t quite understand.

After running the program, if I right click on desktop and use “desktop property” to change the resolution from 800600 to 1024768, then error message will pop up.

Following is a simple program with this problem. Can you tell me what to do with it?

Thank you!

unit First1;

interface

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

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
GLContext: HGLRC;
glDC: HDC;
errorCode: GLenum;
openGLReady: boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

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}
glDC := getDC(handle);
FormatIndex := ChoosePixelFormat(glDC,@pfd);
if FormatIndex=0 then
raise Exception.Create('ChoosePixelFormat failed '+
IntToStr(GetLastError));

if not SetPixelFormat(glDC,FormatIndex,@pfd) then
raise Exception.Create('SetPixelFormat failed '+
IntToStr(GetLastError));

GLContext := wglCreateContext(glDC);
if GLContext=0 then
raise Exception.Create('wglCreateContext failed '+
IntToStr(GetLastError));

if not wglMakeCurrent(glDC,GLContext) then
raise Exception.Create('wglMakeCurrent failed '+
IntToStr(GetLastError));

OpenGLReady := true;
end; {FormCreate}

procedure TForm1.FormDestroy(Sender: TObject);
begin
OpenGLReady := false;
wglMakeCurrent(glDC,0);
wglDeleteContext(GLContext);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
if not openGLReady then
exit;
{background}
glClearColor(0.0,0.4,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);

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

end.

I never heard of a program that is able to track and still run when the user changes the visual properties.

however, you can eliminate the error message from popping up by not checking it . If your program still draws what it is supposed to, I guess it’s fine. Otherwise, I guess you must restart your program with a new rc, as the user could also change the color depth etc.

Jan

I don’t know delphi, but changing resolution usually destroys every openGL context so, I feel this is correct.

Users should not mess up their pc while playing.

Originally posted by Obli:
[b]I don’t know delphi, but changing resolution usually destroys every openGL context so, I feel this is correct.

Users should not mess up their pc while playing.[/b]

a well written application should handle such cases. if your app does not support a feature that the OS provides (ie.resoultion change during programm execution) then you have to make sure, that the user can’t mess up with this feature. it is very easy to say “the user shouldn’t do this anyway”, but actually its your fault, when your application crashes in such cases. Because you are the one who ignores the application-specs for win32-applications, not the user.
most comecial games are able to handle this. one method is to track the WM_SETFOCUS and WM_KILLFOCUS messages and to delete the gl context everytime the app loses focus and recreate the context when it gets the focus back. or simply block this messages, so apps can’t lose their focus at all.
some apps switch even form fullscreen-res to destop-res back when they lose their focus.

ofcourse it’s also a good idea not to ingnore the WM_DISPLAYCHANGE message, which is send everytime the resolution has changed(just guess why this message exists…).