Advertisement

Win32

Started by January 21, 2001 02:09 PM
5 comments, last by grahamhinchly 24 years ago
Am I just being stupid, because I don''t know how to program WIndows? I have read the Programming genesis Part 1, but do not know how to actually put it all together as i does not have a full working example. If someone could post a well commented template that just displays a window with some text in it, I would be very grateful. Comments on it would help too, but i don''t want to be too nitpicky because any help would be greatly appreciated. Thank you The Stupid Beginner who can''t even begin to begin.
***We were all beginners once, don't forget that...***
I hope there are enuff comments

  // Prog1_1 - A Hello World program written by hand//           without the aid of modern convieniences//           like AppWizard#include <windows.h>// prototype declarationsint WINAPI WinMain(    HINSTANCE hInstance,     // handle of current instance    HINSTANCE hPrevInstance, // handle of previous instance    LPSTR     pszCmdLine,    // command line    int       nCmdShow       // show state of window    );LRESULT CALLBACK WindowProc(    HWND   hWnd,   // handle of window    UINT   uMsgId, // message identifier    WPARAM wParam, // first message parameter    LPARAM lParam  // second message parameter    );// WinMain is always the first function to execute in a WinApp.int WINAPI WinMain(    HINSTANCE hInstance,     // handle of current instance                             // is really a handle of the                             // program. Can be used to                             // access information about                             // the program''s satus.    HINSTANCE hPrevInstance, // handle of previous instance                             // in windows 95 this is always                             // 0 and is not used.    LPSTR     pszCmdLine,    // command line                             // is the command line that was                             // used to call the application.                             // ex: if you typed the DOS                             // command WORDPAD MYFILE.TXT                             // pszCmdLine would point to                             // MYFILE.TXT. This is also the                             // case when you drag and drop                             // or double click on a file.    int       nCmdShow       // show state of window                             // this is how the initial                             // window will be displayed.                             // some values include:                             // SW_SHOW-NORMAL                             // SW_SHOWMINIMIZED                             // SW_SHOWMAXIMIZED    ){    static char szAppName[] = "Prog1";    HWND hWnd;    MSG msg;    WNDCLASS wndClass;    // fist register the window class    // registering is important, and more than one window    // can be created from a single window class.    wndClass.style         = 0;      // The style controls certain redraw characteristics      // of the window. Constants carrying the prefix CS_      // are #defined in windows.h for this purpose. For      // example, setting style to CS_NOCLOSE before      // registering the class disables the Close box on      // the window frame and under the system menu icon.      // each of these CS_flags is defined as a constant      // with a single bit set to allow these properties      // to be combined.      // ex: wndClass.style = CS_DBLCLICK | CS_HREDRAW | CS_VREDRAW;      // this assignment tells Windows to redraw the      // entire window whenever the window is resized      // in either the horizontal or vertical dimension      // and to send double-click messages to the window.    wndClass.lpfnWndProc   = WindowProc; // callback fn      // is a pointer to a functions that is associated      // with any window created with the newly registered      // window class    wndClass.cbClsExtra    = 0;    wndClass.cbWndExtra    = 0;      // define the class and window extensions. Because      // c++ provides a much better way to store this type      // of information, set these values to zero.    wndClass.hInstance     = hInstance;      // is set to the instance handle of the current      // application passed to you as the first argument      // to WinMain(); this member enables Windows to      //associate the window class with the proper program.    wndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);      // the handle of the icon that is displayed when the      // application is minimized or when the program is      // examined in its folder. You can make your own or      // use the default Windows ones. Some default ones      // are IDI_ASTERISK, IDI_EXCLAMATION, IDI_HAND, and      // IDI_QUESTION.    wndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);      // the handle of the bitmap that will be used for      // cursor. Some defaults are: IDC_CROSS (a crosshair),      // IDC_IBEAM, IDC_NO (a slashed circle), and IDC_WAIT      // (an hourglass).    wndClass.hbrBackground = (HBRUSH__*)GetStockObject(WHITE_BRUSH);      // a brush which will paint the background of the      // window. Can also be paterns.    wndClass.lpszMenuName  = NULL;      // points to the name of the menu class to be used      // with this window class.    wndClass.lpszClassName = szAppName;      // is set to the name to be used for the window class.    if (RegisterClass(&wndClass) == 0) // register class...    {       return 0;     // ...quit if it doesn''t work    }    // now create the app''s one window    hWnd = CreateWindow(             szAppName,           // the window class name             szAppName,           // the window caption                                  // appears in title bar                                  // when window is displayed.             WS_OVERLAPPEDWINDOW, // window syle             CW_USEDEFAULT,       // horizontal position             CW_USEDEFAULT,       // vertical position             CW_USEDEFAULT,       // width             CW_USEDEFAULT,       // height             NULL,                // handle of parent or                                  // owner window             NULL,                // menu handle             hInstance,           // program instance handle             NULL);               // address of window                                  // creation data                                  // is always 0.    if (hWnd == 0)    {       return 0;    }    ShowWindow(hWnd, nCmdShow);      // the arguments are as follows: 1st-window to show.      // 2nd-how to display it (minimized,normal or maximized).    UpdateWindow(hWnd);      // this is what in fact caused the hello world text to      // be displayed. It causes a WM_PAINT message to be sent      // to the application to ask it to repaint the window.    // this is the message loop. it is like the macintosh    // event loop. GetMessage will return true as long as    // it receives messages other than WM_QUIT. Unless    // specified otherwise, this will receive messages for    // all windows for the window class.        while (GetMessage(&msg, NULL, 0,0))    {       TranslateMessage(&msg);         // this function converts some keyboard messages,         // but for most message types has no effect.       DispatchMessage(&msg);         // asks Windows to pass the message to the message         // handler for the window specified in the MSG         // structure. (WindowProc)    }    return msg.wParam;}// WindowProc -- this function gets called//               to process Windows messagesLRESULT CALLBACK WindowProc(    HWND    hWnd,   // handle of window    UINT    uMsgId, // message identifier    WPARAM  wParam, // first message parameter    LPARAM  lParam  // second message parameter    ){    static char *pszHello = "Hello, world";    // decide what to do with each different message    // type    switch (uMsgId)    {      // handle the window paint message by dispalying      // the string "Hello, world" in the upper-left      // corner (coord 0,0) of the window      case WM_PAINT:        // to process a WM_PAINT message, you must make        // a call to make windows believe that the window        // has been restored. In this case it is BeginPaint.        // If you don''t call this, windows will keep calling        // the WM_PAINT message in an infinite loop, trying        // to get the window validated.        HDC hDC;        PAINTSTRUCT paintStruct;        hDC = BeginPaint(hWnd, &paintStruct);          // other functions to get the device context          // include GetDC() to obtain a handle, and          // ReleaseDC() when you are done. This funciton          // pair does not validate the window, and it          // sets the clipping region in the device          // context to be the entire client area, not          // just the invalid region.                    // this should only be called in response to          // a WM_PAINT message. BeginPaint() sets the          // clipping region of the device context to          // be the same as the invalid region of the          // window. That is, even if the application          // attempts to repaint the entire window,          // somewhere within Windows this attempt is          // short-curcuited so thta only the parts          // of the window that need to be updated          // are then updated.        // uses the current font        TextOut(hDC,                // the device context to use                0, 0,               // x,y location                pszHello,           // string to write                lstrlen(pszHello)); // string length                                    // lstrlen is the windows                                    // equivilent of strlen, and                                    // functions quite the same                                    // way, but accomodates 16-bit                                    // Oriental character sets when                                    // necessary.        EndPaint(hWnd, &paintStruct);          // destroy the device context to free up the          // resources used (memory etc...). Note, you          // cannot store the hDC, as it may become          // invalid, and thus not work.        return 0;      // handle the window destroy message by quitting      // the application      case WM_DESTROY:        // the destroy message is given when a window is        // closed. In this case, since the app has only        // 1 window, we will make it quit.        PostQuitMessage(0);          // this causes a WM_QUIT message to be generated.          // this will make the program exit.        return 0;      default:        // let Windows handle all other message types        return DefWindowProc(hWnd,uMsgId,wParam,lParam);    }}  
Advertisement
Oh yeah, forgot to mention, that is code for a "Hello World" application. It pops up a window, and draws the string in it.

Compile it, and experiment with adding and removing code from areas. I usually find experimentation and ''hands on'' to be the best approach to learning new stuff.
Here's a tutorial I wrote, you can download the full source with executable at the top of the page, and the whole program is listed in it's entirety at the bottom.



"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka

Edited by - BitBlt on January 21, 2001 5:20:35 PM

Edited by - BitBlt on January 21, 2001 5:43:00 PM
FrigidHelix, I hope that was just specially commented code for graham. I''d dread to see that in production code j/k.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Frigid Helix, thank you for the code. It is a great help, but i need some more help. This isn''t about the code, it''s just when I copy and paste it, it ends up all in one vertical line. Oh well, i spose i''ll have to type it all...
thank you anyaway
Graham Hinchly
Advertisement
www.winprog.org/tutorial

This topic is closed to new replies.

Advertisement