/* Author : Willie Tang * Compiler : MS Visual C++ 6.0 * Created : December 24, 2001 * Modified : January 03, 2002 * Website : http://nomad.openglforums.com * * Notes : The code below works as a Win32 Application. * To run the code, create a new project (File->New). * In the Projects Tab, select "Win32 Application", * give the project a name them click OK. Then select * the "An Empty Project" radio button, click Finish. * * To insert the code to the project. Click on the * Project menu->Add to Project->New. On the Files * Tab, select "C++ Source File", give any name to * the file, then click OK. After that, copy and paste * the code below to the editor. ************************************************************/ #include #include #include //--[ I know, I know, global variables are bad...but hey, //--[ my goal is to let you know how to use these stuffs. //--[ Besides, it's actually OK to use global variables //--[ for small programs...;). HWND hWindow; // handle to window HDC hDC; // window's device context char stringState[30]; // used to draw text to window char stringSize[30]; // used to draw text to window char stringCoord[30]; // used to draw text to window int xsize, ysize; // stores window's x/y size int xcoord, ycoord; // stores window's x/y coordinate LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_CREATE: // window is created sprintf(stringSize, "User has not resized window"); sprintf(stringState,"Window has not changed state"); sprintf(stringCoord,"User has not moved window"); break; case WM_DESTROY: // window is destroyed PostQuitMessage(0); // add "quit" to msg queue break; case WM_SIZE: // window is resized xsize = LOWORD(lParam); // get window's x-size ysize = HIWORD(lParam); // get window's y-size sprintf(stringSize,"X-size: %d | Y-size %d", xsize, ysize); if(wParam == SIZE_MAXIMIZED) sprintf(stringState,"Maximized State"); else if(wParam == SIZE_RESTORED) sprintf(stringState,"Restored State"); //--[ Another possible state for wParam is SIZE_MINIMIZED. //--[ But since you won't be able to read stringState on //--[ screen when the window is minimized, I didn't add //--[ the sprintf() for SIZE_MINIMIZED here...:) break; case WM_MOVE: // window is moved xcoord = LOWORD(lParam); // get window's x-coordinate (upper left) ycoord = HIWORD(lParam); // get window's y-coordinate (upper left) sprintf(stringCoord,"X-coord: %d | Y-coord %d", xcoord, ycoord); break; case WM_PAINT: // draw-to-window message hDC = GetDC(hWindow); // get device context TextOut(hDC, 5,15, stringState, strlen(stringState)); TextOut(hDC, 5,35, stringCoord, strlen(stringCoord)); TextOut(hDC, 5,55, stringSize, strlen(stringSize)); SetPixel(hDC, // device context rand() % xsize, // random x-coordinate rand() % ysize, // random y-coordinate RGB(rand() % 255, // random red intensity rand() % 255, // random green intensity rand() % 255)); // random blue intensity ReleaseDC(hWindow, hDC); // release device context break; default: return(DefWindowProc(hWnd, message, wParam, lParam)); } return(0L); // return a zero } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASS wndclass; MSG msg; // Initialize values of wndclass' fields wndclass.style = CS_HREDRAW | CS_VREDRAW; // window's style wndclass.lpfnWndProc = (WNDPROC)WndProc; // pointer to WndProc wndclass.cbClsExtra = 0; // disregard... wndclass.cbWndExtra = 0; // disregard... wndclass.hInstance = hInstance; // copy from WinMain() wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // window icon // IDI_ASTERISK // IDI_EXCLAMATION // IDI_HAND // IDI_QUESTION // IDI_WINLOGO wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); // window's mouse cursor // IDC_CROSS // a...'cross'...:) // IDC_NO // slashed circle // IDC_WAIT // hourglass // IDC_HELP // arrow w/question mark // IDC_UPARROW // arrow pointing up wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,0,0)); wndclass.lpszMenuName = NULL; // window's menu name wndclass.lpszClassName = "MyWindowName"; // window's name // Register wndclass if(RegisterClass(&wndclass) == 0) return FALSE; // Create our window and get its handle hWindow = CreateWindow ("MyWindowName", // from wndclass.lpszClassName "My Title Bar", // this will show on title bar WS_OVERLAPPEDWINDOW, // border, title, min-max-close button // WS_OVERLAPPED // border, title // WS_POPUP // no title, no button // WS_VISIBLE // window is visible from start // WS_VSCROLL // window has vertical scroll bar // WS_HSCROLL // window has horizontal scroll bar // WS_MAXIMIZE // create window in maximized state // WS_MINIMIZE // create window in minimized state 0, // starting x-coord (upper left) 0, // starting y-coord (upper left) CW_USEDEFAULT, // window's x-size CW_USEDEFAULT, // window's y-size NULL, // parent window's handle NULL, // handle to a menu hInstance, // copy from WinMain()'s parameter NULL); // disregard... // Check if window was created if(hWindow == NULL) return FALSE; // Show our window ShowWindow(hWindow, SW_SHOW); // show created window // SW_HIDE // hide created window // SW_RESTORE // show window in restored state // SW_MAXIMIZE // show window in maximized state // SW_MINIMIZE // show window in minimized state // Keep on grabbing messages while(GetMessage(&msg, hWindow, 0,0)) // keep on getting msgs { TranslateMessage(&msg); // convert to "known" msg DispatchMessage(&msg); // send to msg queue } return 0; }