View Full Version: Sample Windows Program

C++ Learning Community > C++ Tips > Sample Windows Program


Title: Sample Windows Program
Description: Comments included


Dragon - August 13, 2003 11:03 PM (GMT)
I haven't compiled and tested this code, so there may be errors. Please report errors if there are any. Compile as GUI.

CODE
//Sample Windows Program
//Written by Rui Li

#include <windows.h>

//Declare the callback function
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

//Store the Window class name as a global variable
char szClassName[] = "MainWnd";

//WinMain function, similar to that of the main function in standard C++
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
 HWND hwnd;
 MSG messages;
 WNDCLASSEX wincl;
 
 //The Window structure
 wincl.cbSize = sizeof(WNDCLASSEX);
 wincl.cbClsExtra = 0;
 wincl.cbWndExtra = 0;
 wincl.style = 0;
 wincl.hInstance = hInstance;
 wincl.lpszClassName = szClassName;
 wincl.lpszMenuName = NULL; //No menu
 wincl.lpfnWndProc = WindowProcedure;
 wincl.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); //Color of the window
 wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION); //EXE icon
 wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //Small program icon
 wincl.hCursor = LoadCursor(NULL, IDC_ARROW); //Cursor
 
 //Register the Window class, and if it fails quit the program
 if (!RegisterClassEx(&wincl))
   return 0;

 //Create the program
 hwnd = CreateWindowEx(0, //No extended window styles
        szClassName, //Class name
        "Sample Win32 Program", //Window caption
        WS_SYSMENU|WS_MINIMIZEBOX,
        CW_USEDEFAULT, CW_USEDEFAULT, //Let Windows decide the left and top positions of the window
        640, 480, //Width and height of the window,
        NULL, NULL, hInstance, NULL);

 //Make the window visible on the screen
 ShowWindow(hwnd, nCmdShow);
 
 //Run the message loop
 while (GetMessage(&messages, NULL, 0, 0))
 {
   TranslateMessage(&messages);
   DispatchMessage(&messages);
 }
 return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
   case WM_DESTROY:
     PostQuitMessage(0);
     break;
   default:
     return DefWindowProc(hwnd, message, wParam, lParam);
 }
 return 0;
}

TheHawgMaster - August 13, 2003 11:19 PM (GMT)
I havn't tested it either (I've writen code similar to that a million times). But I know it will not work unless you add:
CODE
#include <Windows.h>
To the top of the code.

Dragon - August 13, 2003 11:39 PM (GMT)
Hey thanks. You would think that after writing it so many times I would remember that. Oh well. :D

Sam Fisher vs Solid Snake - August 14, 2003 12:15 AM (GMT)
nice codeing Dragon

Dragon - August 14, 2003 12:36 AM (GMT)
There, I fixed it. Also, Sam Fisher, I haven't forgotten about the src for Dragon Text. I'll get to it soon! :)

ZerO - December 7, 2003 11:50 PM (GMT)
I got a error "CXX0017" when I tested it... I'm kinda new to c++ so i wonder what this means and how i fix this....

Dragon - December 8, 2003 12:01 AM (GMT)
I don't know what that error message means, but did you compile the source as a Win32 GUI application?

ih8censorship - December 8, 2003 02:16 AM (GMT)
QUOTE
You would think that after writing it so many times I would remember that.



not me... i just use the dev c++ template and then fill in stuff. but by now i know what everything does, its just i dont want to take the time to code everything by hand because its basicly the same for every program ya make. however i probly couldent make a window code on a first try, so good job even if it doesnt work! (ya dont hear guys say that often :lol: )

Dragon - December 8, 2003 05:48 AM (GMT)
ih8censorship, I remember things like that now. That post was 4 months ago. :D

ZerO - December 8, 2003 01:56 PM (GMT)
Ahh.. Thank you It works perfectly fine now... ;)

Dante Shamest - December 22, 2003 01:12 AM (GMT)
The piece of contributed code has one little problem with it.

The following line of code

CODE

while (GetMessage(&messages, NULL, 0, 0))


should be rewritten as

CODE

while (GetMessage(&messages, NULL, 0, 0)>0)


or

CODE

while (GetMessage(&messages, NULL, 0, 0)!=0)


This is because GetMessage can return -1 too, which causes a fatal error in rare circumstances. This is documented in MSDN.



Dragon - December 22, 2003 04:43 AM (GMT)
That only happens in rare circumstances and I have never encountered that problem before. It's still good to know about that, though.




* Hosted for free by InvisionFree