Click here for a txt file of the source code below. Notes:
////////////////////////////////////// // A Simple Window Tutorial //////////////////////////// // By Jesse King // 11/13/99 //////////// ///////////// // #includes // You will use this for all of your Windows applications #include <windows.h> //////////// // #defines #ifndef WIN32_LEAN_AND_MEAN // This cleans out rarely used stuff #define WIN32_LEAN_AND_MEAN #endif #define MAX_LOADSTRING 100 /////////////////// // Global Variables HINSTANCE hInst; // the Instance TCHAR szWindowClass[] = "I'M A TITLE!!!" ; // Title Bar ////////////////////////////////////////////////// // Prototypes: beforehand declaration of functions ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /////////////////////////////////////// // WinMain() is what applies everthing int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; ///////////////////////// // Set up the windows class MyRegisterClass(hInstance); ///////////////////////// // Create the window. If it doesn't work, return FALSE. if (!InitInstance (hInstance, nCmdShow)) return FALSE; //////////////////////// // This is a good place to use accelerations and whatnot, // you should load them in the program from the resources (the whatever.rc) //////////////////////// //////////////////////// // The main message loop is run under here, // it pretty much explains itself. It just gets and handelse messages. while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, 0, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } ///////////////////// // the function MyRegisterClass() // this is used to create a WIN32 application, // if you have Microsoft Visual C++ and the documentation // it explains this really well. All the wc.xxxxxxx are variables in the // WNDCLASSEX structure, it took me a while to figure it out, just look at // help files and tutorials, it's easier then it seems. ATOM MyRegisterClass(HINSTANCE hInstance) { ////////////// // This allows you to use the structure // WNDCLASSEX as wc.whatevervariable WNDCLASSEX wc ; wc.cbSize = sizeof(WNDCLASSEX) ; ////////////// // Class Style described here. wc.style = CS_HREDRAW | CS_VREDRAW ; ////////////// // This is the functions that is called when a message is sent to the window (like a PAINT msg). wc.lpfnWndProc = (WNDPROC)WndProc ; /////////////// // This is a simple window, so this isn't needed. wc.cbClsExtra = 0 ; /////////////// // Same as above wc.cbWndExtra = 0 ; /////////////// // The instance of the window. wc.hInstance = hInstance ; /////////////// // A declaration of the icon, in the resources, or from file, but // this one is from the resources. wc.hIcon = 0 ; /////////////// // Loads the pointer, or cursor your window application will use wc.hCursor = 0 ; /////////////// // The background color can change through color_window+whatever wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ; /////////////// // Name of the menu system, but we don't have a menu here // because this is a simple window. wc.lpszMenuName = 0 ; /////////////// // The title of the used windows classes, or // the title of the program. wc.lpszClassName = szWindowClass ; /////////////// // This is the small icon, like when your windows options is to list all the // icons, then it will display the small icons, this is the declaration of the // icon used for this occasion wc.hIconSm = 0 ; /////////////// // This returns a registered structure, // which is the windows classes, to the window. return RegisterClassEx(&wc) ; } ///////////////////// // the function InitInstance(HANDLE, int) // creates the main window or parent window, and // describes the style (such as WS_OVERLAPPEDWINDOW, WS means // Windows Style) and many other characteristics of the // Parent Window. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd ; hInst = hInstance ; // Store instance handle on global hInst hWnd = CreateWindow( szWindowClass, 0, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInstance, 0) ; ///////////////////////// // If the window isn't made right // it won't work... if (!hWnd) { return FALSE ; } ////////////////////////// // expains itself, nCmdShow // means that its a small window // not maximized ShowWindow(hWnd, nCmdShow) ; UpdateWindow(hWnd) ; return TRUE ; } //////////////////////////////////////////////////// // The function WndProc(HWND, unsigned, WORD, LONG) // processes the window, it does whatever you want // the window to do. // // WndProc()'s purpose is processing messages for the main window. // // ********************* BELOW ************************ // // // case**WM_MESSAGE - Use the keyboard for the exiting the program, // I set it up as ESCAPE Exits the program (or VK_ESCAPE does). // // case**WM_PAINT - Paints the main window, you can blit bitmaps // and such, but this is a simple window, so I painted it like // that // // case**WM_DESTROY - Destroys the window when you push // the X on the upper right of the window //////////////////////////////////////////////////// LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; LoadString(hInst, 0, 0, MAX_LOADSTRING) ; ////////////////////////// // This is a nice simple loop // that lets you input and get // output to and from the window switch (message) { case WM_KEYDOWN: switch(wParam) { case VK_ESCAPE: PostQuitMessage(0) ; break ; } case WM_PAINT: hdc = BeginPaint(hWnd, &ps) ; RECT rt ; GetClientRect(hWnd, &rt) ; EndPaint(hWnd, &ps) ; break ; case WM_DESTROY: PostQuitMessage(1) ; break ; default: return DefWindowProc(hWnd, message, wParam, lParam) ; } //////////////////////////////// // Return so it doesn't complain return 0 ; } Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|