Thread: GDI Drawing Help

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    GDI Drawing Help

    Trying to make a function to draw a simple rectangle.. if successful, I would like to use a series of rectangles to draw a grid.. Just wondering if anyone could take a look at my DrawGrid( ) function and tell me what I missing/overlooking..

    Code:
    #include <windows.h>
    
    HINSTANCE hInstGlobal;
    HWND hButton, hEdit;
    
    void DrawGrid(HWND, int rows, int column);
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "Cable Splicer";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nCmdShow)
    
    {
       hInstGlobal = hThisInstance;
                        
                        
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
        
     
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Cable Splice Calculator",      /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nCmdShow);   
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE: 
                 
                 hButton = CreateWindow("BUTTON", "ADD", WS_CHILD|WS_VISIBLE|
                                        BS_DEFPUSHBUTTON, 150, 10, 140, 40, 
                                        hwnd, (HMENU) 1, hInstGlobal, NULL);
                                        
                 hEdit = CreateWindow("EDIT", NULL, WS_CHILD|WS_VISIBLE|
                                       WS_BORDER|ES_AUTOHSCROLL, 10, 30, 120, 20,
                                       hwnd, (HMENU) 2, hInstGlobal, NULL);  
                                       
                 DrawGrid(hwnd,1,1);     
                
                 break;
                 
            case WM_PAINT:
                 
                 HDC hdc;
                 PAINTSTRUCT ps;
                    
                 hdc = BeginPaint(hwnd, &ps);
                 TextOut(hdc, 10, 10, "Enter Cable Count:", 18);
                 EndPaint(hwnd, &ps);       
                 
                 break;           
               
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    
    
    
    //Function Definitions
    
    void DrawGrid(HWND hwnd, int rows, int column)
    {
                 
         HDC hdc;
         HPEN hPen;
         PAINTSTRUCT ps;
         
         hPen = CreatePen(PS_SOLID, 2, RGB(200,100,50));
         
         hdc = BeginPaint(hwnd, &ps);
         
         SelectObject(hdc, hPen);
         
         Rectangle(hdc, 10, 100, 40, 10);
         
         DeleteObject(hPen);
         
         EndPaint(hwnd, &ps);
    }

    using dev-cpp
    • "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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The problem is your DrawGrid() function is only called once. You need that function to be called from the WM_PAINT handler so it gets redrawn when it needs to be.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You can't call BeginPaint() except in response to a WM_PAINT msg and you can't call it more than once in a WM_PAINT msg.

    If the update area is empty then BeginPaint() won't work. Use GetUpdateRect() to check it BEFORE you call BeginPaint() or check the rect in the PAINTSTRUCT.

    Trigger a WM_PAINT msg with
    InvalidateRect() //use client rect
    UpdateWindow()


    You also have a GDI leak.
    use

    HPEN hOrigPen = SelectObject(hdc, hPen);

    //use pen

    //clean up
    SelectObject(hdc, hOrigPen );
    DeleteObject(hPen);


    I also suggest you 'clear' the entire area by Rectangle() with the client rect.
    That is,
    draw over the current screen with a solid colour,
    redraw the text and grid.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Got it to work.. you guys are awesome...

    thanks
    • "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] drawing: drag GDI shapes
    By pc2-brazil in forum Windows Programming
    Replies: 3
    Last Post: 10-05-2008, 02:06 PM
  2. GDI - Drawing to the screen correctly
    By Tonto in forum Windows Programming
    Replies: 1
    Last Post: 06-24-2006, 09:53 PM
  3. drawing sprites with the windows gdi
    By lambs4 in forum Game Programming
    Replies: 10
    Last Post: 08-14-2003, 09:06 AM
  4. GDI Drawing a stupid line!
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 05-08-2003, 09:00 PM
  5. Double buffering in GDI -- easy?
    By fusikon in forum Game Programming
    Replies: 17
    Last Post: 02-15-2003, 10:03 PM