Thread: Learning win32API

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    Learning win32API

    Well im pretty excited, I just picked up Programming Windows Fifth Edition by Petzold a couple days ago and have been reading it since. But theres one consept I just cant seem to grasp, I tried adding some cout functions to a console to see what was going on, fyi because I compiled in a console, I couldnt use GetStockObject so there is no background. Can someone please help me understand how the message loop works. Or just explain this code for me. Thanks!

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
       static TCHAR szAppName[ ] = "TestProgram";
       HWND         hwnd;
       MSG          msg;
       WNDCLASS     wndclass;
       
       wndclass.style         = CS_HREDRAW | CS_VREDRAW;
       wndclass.lpfnWndProc   = WndProc;
       wndclass.cbClsExtra    = 0;
       wndclass.cbWndExtra    = 0;
       wndclass.hInstance     = hInstance;
       wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
       wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
       wndclass.hbrBackground = NULL;
       wndclass.lpszMenuName  = NULL;
       wndclass.lpszClassName = szAppName;
       
       if (!RegisterClass (&wndclass))
       {
          MessageBox (NULL, TEXT ("ERROR: Window could not be registered!"), 
                      szAppName, MB_ICONERROR);
          
          return 0;
       }
       
       hwnd = CreateWindow (szAppName, 
                            TEXT ("Josh's Test Program"),
                            WS_OVERLAPPEDWINDOW,
                            400,
                            400,
                            CW_USEDEFAULT,
                            CW_USEDEFAULT,
                            NULL,
                            NULL,
                            hInstance,
                            NULL);
                            
       ShowWindow (hwnd, iCmdShow);
       UpdateWindow (hwnd);
       
       while (GetMessage (&msg, NULL, 0, 0))
       {
          TranslateMessage (&msg);
          DispatchMessage (&msg);
          cout << "MEssage has been looooped\n";
       }
       
       return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       HDC           hdc;
       PAINTSTRUCT   ps;
       RECT          rect;
       cout << "Call to CALLBACK FUNCTION!\n";
       
       switch (message)
       {
          case WM_CREATE:
               cout << "CALL TO CREATE!\n";
               return MessageBox (hwnd, TEXT ("WINDOW CREATED!"), TEXT ("TEST"),
                                  MB_ICONEXCLAMATION);
          case WM_PAINT:
               hdc = BeginPaint (hwnd, &ps);
               GetClientRect (hwnd, &rect);
               DrawText (hdc, TEXT ("Hello!!!"), -1, &rect,
                         DT_SINGLELINE | DT_CENTER | DT_VCENTER);
               EndPaint (hwnd, &ps);
               cout << "CALL TO PAINT!\n";
               return 0;
          case WM_DESTROY:
               PostQuitMessage(0);
               cout << "CALL TO DESTROY\n";
               system("pause");
               return 0;
       }
       
       return DefWindowProc (hwnd, message, wParam, lParam);
    }
    Last edited by JoshR; 05-29-2005 at 10:21 PM.

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    double post...

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    I dont understand the WindProc function, when is it called, i dont see it called in the program, and how does the msg, created in WinMain (as a MSG type) get filled with queues from WindProc, if there is nothing using the definition how does it work???

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    nvm i went searching for a long time online, took me less time to find it then it did to get an answer, the callback function is called everytime something happens in the window and then the message loop uses the messages made by the callback function.and puts them onto the window.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    And what you have there is a program that should be compiled as a GUI application. If you want a console application then use the old main function instead of winamin.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    well ya but i just said that i was writing the code, and i quickly added the cout functions so i could see when the callback was used etc...

  7. #7
    Amateur Programmer
    Join Date
    Nov 2003
    Posts
    11
    isn't it sort of logical when it is used?

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    i'm currently studying the same book. just finished chapter #13 on using the printer. chapter #14 discusses bitmaps and bitblt.

    fun book.

    if you still want to use the dos console whilst in winmain, this tutorial will show you how to get a handle to the dos console.
    Last edited by The Brain; 05-30-2005 at 11:19 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    ya it really is, lots of history too. never thought a little history could be some fun lol. im on chapter 4 now WHOOP!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  2. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  3. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM
  4. Learning Rate Of C++
    By Krak in forum C++ Programming
    Replies: 27
    Last Post: 01-29-2003, 01:53 PM