Problem With Win32 Graphics Update
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Problem With Win32 Graphics Update

  1. #1
    Join Date
    Nov 2007
    Posts
    129

    Unhappy Problem With Win32 Graphics Update

    Hi I am compiling Win32 C++ code using Bloodshed DevC++.
    My program draws circles on the screen, and updates to new positions every second on a timer.
    I verified that the timer works and WM_PAINT is getting called, but the Window only updates when I minimize and restore the window. Why won't it redraw on it's own every second?

    Here's the code:

    Code:
    #include <windows.h>
    #include <math.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    RECT rect = {0,800,0,600};
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "Perimeter";
    
    bool flag = true;
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        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_HREDRAW | CS_VREDRAW;                 /* 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) CreateSolidBrush(RGB(255,255,255));
    
        /* 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 */
               "Perimeter",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               800,                 /* The programs width */
               600,                 /* 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, nFunsterStil);
    
        /* 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()  */
    
    struct COORDINATE
    {
         int x, y;       
    };
    COORDINATE ships[20];
    
    void DrawCircle(HDC hdc, int radius, int xCenter, int yCenter, COLORREF color)
    {
         HRGN hrgn;
         HBRUSH hbrush;
         int xLeft = xCenter - radius, xRight = xCenter + radius, yTop = yCenter - radius, yBottom = yCenter + radius;
         hrgn = CreateEllipticRgn(xLeft,yTop,xRight,yBottom);
         hbrush = CreateSolidBrush(color);
         FillRgn(hdc,hrgn,hbrush);
         DeleteObject(hrgn);
         DeleteObject(hbrush);
    }
    
    void DrawWindow(HDC hdc)
    {
         for(int i = 0; i < 5; i++)
         {
              DrawCircle(hdc, 20, ships[i].x, ships[i].y, RGB(255,0,0));
         }
    }
    
    
    
    void Init()
    {
         for(int i = 0; i < 20; i++) {ships[i].x=0; ships[i].y=0;}
         ships[0].x = 350;
         ships[0].y = 200;
         ships[1].x = 400;
         ships[1].y = 200;
         ships[2].x = 400;
         ships[2].y = 250;     
         ships[3].x = 450;
         ships[3].y = 250;
         ships[4].x = 450;
         ships[4].y = 300;
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         HDC hdc;
         PAINTSTRUCT ps;
         switch (message)
         {
            case WM_PAINT:
                 hdc = BeginPaint(hwnd,&ps);
                 DrawWindow(hdc);             
                 EndPaint(hwnd,&ps);
                 break;
            case WM_TIMER:
                 for(int i = 0; i < 5; i++) {ships[i].x+=10; ships[i].y+=10;}
                 UpdateWindow(hwnd);
                 break;
            case WM_CREATE:
                 Init();
                 SetTimer(hwnd, 1000,1000,NULL);
                 UpdateWindow(hwnd);
                 break;
            case WM_DESTROY:
                 PostQuitMessage (0);
                 break;
            default:
                 return DefWindowProc (hwnd, message, wParam, lParam);
         }
         return 0;
    }

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Problem With Win32 Graphics Update

    Windows maintains a invalid region for paint operations. The hdc that BeginPaint returns will be set with that invalid region. Any attempt to paint outside the invalid region will do nothing. When you call EndPaint, the region gets validated. So, the next time a paint is triggered, you need to make the region invalid too. The trick is to call InvalidateRect before UpdateWindow

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center