Thread: Redrawing

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Redrawing

    I want my program to draw a rectangle on the screen(HWND_DESKTOP), but when I make it redraw it in a while:
    Code:
    while(1==1){
    Rectangle(hdc,48,48,102,77);
    }
    With this code the borders like flash a bit. But how I can make it redraw my rectangle on the screen only when it is necessary(if something overdraws it).

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    put your drawing code in a WM_PAINT handler, if you are doing the paint handler for a window in another process, you may need to write a windows hook DLL and insert it into that process, I assume that's what you mean when you say you are drawing on HWND_DESKTOP.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well, I don't really know how to make a windows hook DLL...
    And it flashes even worse, when I use it in a normal window in a wm_paint:
    Code:
    #include <windows.h>
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    HDC hdc;
    RECT rc;
    char szClassName[ ] = "WindowsApp";
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;
        MSG messages;
        WNDCLASSEX wincl;
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        if (!RegisterClassEx (&wincl))
            return 0;
    
        hwnd = CreateWindowEx (
               0,
               szClassName,
               "Windows App",
               WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT,
               CW_USEDEFAULT,
               544,
               375, 
               HWND_DESKTOP,
               NULL,
               hThisInstance,
               NULL
               );
    
        ShowWindow (hwnd, nFunsterStil);
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_PAINT:
                hdc = GetDC(hwnd);
                rc.left = 51;
                rc.top = 53;
                rc.right = 137;
                rc.bottom = 76;
                Rectangle(hdc,48,48,140,77);
                DrawText(hdc,"Garfield rules",14,&rc,DT_CENTER|DT_NOCLIP);
                ReleaseDC(hwnd, hdc);
                break;
            case WM_DESTROY:
                PostQuitMessage (0);
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    My actual code was:
    Code:
    #include <windows.h>
    HDC hdc;
    RECT rc;
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
    hdc = GetDC(HWND_DESKTOP);
    rc.left = 51;
    rc.top = 53;
    rc.right = 137;
    rc.bottom = 76;
    while(1==1){
    Rectangle(hdc,48,48,140,77);
    DrawText(hdc,"Garfield rules",14,&rc,DT_CENTER|DT_NOCLIP);
    Sleep(1);
    }
    ReleaseDC(HWND_DESKTOP, hdc);
    return 0;
    }
    Last edited by maxorator; 10-28-2005 at 12:38 AM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Change code to:

    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)
    	{
    	case WM_PAINT:
    	{
    		PAINTSTRUCT ps;
    		hdc = BeginPaint(hwnd,&ps);
    		rc.left = 51;
    		rc.top = 53;
    		rc.right = 137;
    		rc.bottom = 76;
    		Rectangle(hdc,48,48,140,77);
    		DrawText(hdc,"Garfield rules",14,&rc,DT_CENTER|DT_NOCLIP);
    		EndPaint(hwnd,&ps);
    		break;
    	}
    	// ...

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    But does anybody have a good link for a hook dll example?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Quote Originally Posted by maxorator
    But does anybody have a good link for a hook dll example?
    This looks like a good (simple) tutorial, although I have not run the code.

    Tutorial - Keyboard Hook
    http://www.ragestorm.net/tutorial?id=10

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. redrawing multiple glut windows
    By ichijoji in forum Game Programming
    Replies: 2
    Last Post: 11-27-2005, 05:04 PM
  2. How redrawing works
    By ChadJohnson in forum Windows Programming
    Replies: 5
    Last Post: 07-14-2005, 11:01 PM
  3. Automatically redrawing window contents
    By bennyandthejets in forum Windows Programming
    Replies: 4
    Last Post: 08-25-2003, 03:15 AM
  4. Redrawing the Screen.
    By Ranedhel in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2003, 04:57 PM
  5. Redrawing window blank
    By unanimous in forum Windows Programming
    Replies: 1
    Last Post: 04-06-2002, 09:12 PM