Windows Programming Tidbits

This is a small bonus section with some little tidbits that I discovered while programming with Windows which you might find useful.

Assume that m_hWnd is the handle to the window for the application.

Unless otherwise specified, all functions here are part of the standard Win32 API.
Also assume that there is only ONE window in the application. Everything within that window is a form control from the Forms game engine component.

Minimizing the application programmatically

wp.length = sizeof(wp);
GetWindowPlacement(m_hWnd, &wp);
wp.showCmd = SW_MINIMIZE;
SetWindowPlacement(m_hWnd, &wp);

Detecting program restoration

Restoration refers to the program window no longer being minimized.

If the application is ever minimized, set a flag (such as m_minimized) for this state and hold on to that value.

If at any time in your idle loop this flag is set, and IsIconic(m_hWnd) returns false, the application is back in the foreground again, and you should call your restore routines, along with setting the m_minimized flag to false.

Detecting lost focus

When another program gains the focus, I prefer to minimize the game application. The idle loop still runs, so the game will continue to execute.

To detect the change in program focus, just call GetActiveWindow() and compare it to m_hWnd. If they are not equal, call the minimize routine.

Changing the screen resolution for OpenGL

ChangeDisplaySettings is the better choice for managing your screen resolution when you are using the OpenGL rendering API.

It is recommended you retrieve the beginning resolution for your application so that you can restore it explicitly upon program exit.

Retrieve the current resolution and bit depth and store them in variables like this:

xSize = GetSystemMetrics(SM_CXSCREEN);
ySize = GetSystemMetrics(SM_CYSCREEN);

The beginning bit depth can be retrieved using DirectX - this example comes from DirectX 7:
bitDepth = ddsd2.ddpfPixelFormat.dwRGBBitCount;

Set any new resolution like this:

dv.dmPelsHeight = height of screen;
dv.dmPelsWidth = width of screen;
dv.dmBitsPerPel = bit depth;

dv.dmSize = sizeof(dv);

ChangeDisplaySettings(&dv, CDS_FULLSCREEN);

Making OpenGL fullscreen

After setting the screen resolution using ChangeDisplaySettings, call the following code:

SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, width of screen, height of screen, 0);
ShowWindow(m_hWnd, SW_SHOWNORMAL);

Now you can create your OpenGL instance and tag it to the program window, and it will be full screen.

Generating random numbers

This macro doesn't require Windows programming, I just hate having to remember it, so I put it here.

#define getrandom(min, max) ((rand()%(int)(((max) + 1)-(min)))+ (min))

Using libraries

Sick of having to re-include all your shared sources when you create a new project? Just include all those shared sources in a LIB project, and link the lib into your existing project.

Downside: Intellisense won't work until you include the header files in your project. I don't know how to get around that yet.