Thread: GDI problems... SetPixel()

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    9

    GDI problems... SetPixel()

    Ok... I've searched this on the forum to see whether anyone had the same problem, but I couldn't find it... so here it is:

    I am using setpixel to draw on the screen(well, I'm still learning GDI...) and it compiles and works with no problems at all... but well, it doesn't completely work... here's what happens...
    it does draw on parts of the screen but not on other parts... does it make sense?

    Well, here is part of the code...
    Code:
    RECT rect;
        hdc = GetDC(hwnd);
        GetClientRect(hwnd, &rect);
        
        while(TRUE){
                    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
                                         if(msg.message == WM_QUIT){
                                                        break;
                                                        }
                                         }
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                    
                    static int ax=0;
                    while(ax<=200000){
                                      SetPixel(hdc, rand()%rect.right, rand()%rect.bottom, RGB(rand()%255, rand()%255, rand()%255));
                                      ax++;
                                      }
    
              
    
            }
        ReleaseDC(hwnd, hdc);
        return 0;
    what happens is that the middle section of the window is not painted...

    let me show it: http://img44.imageshack.us/img44/2552/flagy.jpg

    I mean... I really can't figure out what's wrong :S...
    I tried writing the loop on different parts of the code, such as in the WM_PAINT and WM_CREATE...

    The same problem happens if I try to paint it with the mouse, using WM_MOUSEMOVE and SetPixel to where the mouse is... it will only paint those parts, but not the middle...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Is the function recieving pixel coords that in the blank area and not drawing them, or is it just not receiving them.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    BTW, SetPixel must never be used.
    Use DIBs insteads.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Alex31 View Post
    BTW, SetPixel must never be used.
    Use DIBs insteads.
    Would you care to elaborate on that statement?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That same code worked fine for me. I would guess you have some other control on your window that covers that area, and your window may have the clip-children flag set.

    You'll have to post the code for the whole app somewhere if you want anyone to find the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    That's the whole code...
    There are loads of things I'm experimenting on it as well...


    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpArgz, int nCmdShow){
        
        HWND hwnd;
        MSG msg;
        WNDCLASSEX WinClass;
        HDC hdc;
        
        WinClass.lpszMenuName = NULL;
        WinClass.lpszClassName = "WinClass1";
        WinClass.cbSize = sizeof(WinClass);
        WinClass.cbClsExtra = 0;
        WinClass.cbWndExtra = 0;
        /////////////////////////
        WinClass.hInstance = hInstance;
        WinClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
        WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        ///////////////////////////
        WinClass.style = CS_OWNDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        WinClass.lpfnWndProc = WinProc;
        
        RegisterClassEx(&WinClass);
        
        if(!(hwnd = CreateWindowEx((DWORD) NULL,
                                  "WinClass1",
                                  "Window Title",
                                  WS_VISIBLE | WS_OVERLAPPEDWINDOW,
                                  CW_USEDEFAULT, CW_USEDEFAULT,
                                  CW_USEDEFAULT, CW_USEDEFAULT,
                                  NULL, NULL,
                                  hInstance,
                                  NULL))){ return 0; }
        RECT rect;
        hdc = GetDC(hwnd);
        static int ax=0;
        GetClientRect(hwnd, &rect);   
        while(TRUE){
                    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
                                         if(msg.message == WM_QUIT){
                                                        break;
                                                        }
                    
                                         }
    
                    
                    
    
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                    while(ax<=200000){
                                      SetPixel(hdc, rand()%rect.right, rand()%rect.bottom, RGB(rand()%255, rand()%255, rand()%255));
                                      ax++;
                                      }
                   
              
    
            }
        ReleaseDC(hwnd, hdc);
        return 0;
        
    }
    
    LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
            
            PAINTSTRUCT ps;
            HDC hdc;
            RECT rect;
            HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
            DeleteObject(hPen);
            GetClientRect(hwnd, &rect);
            
            
            switch(msg){
                        case WM_PAINT:
                             GetClientRect(hwnd, &rect);
                             InvalidateRect(hwnd, &rect, FALSE);
                             if(wParam==1){
                                           InvalidateRect(hwnd, &rect, TRUE);
                                           wParam = 0;
                                           }
                             hdc = BeginPaint(hwnd, &ps);
                             SetBkColor(hdc, RGB(255, 255, 255));
                             SetTextColor(hdc, RGB(0, 0, 0));
                             SetBkMode(hdc, TRANSPARENT);
                             TextOut(hdc, 0, 0, "Hello", strlen("Hello"));
                             EndPaint(hwnd, &ps);
                             return 0;
                             break;
                        case WM_CREATE:
                             return 0;
                             break;
                        case WM_DESTROY:
                             PostQuitMessage(0);
                             return 0;
                             break;
                        case WM_CLOSE:
                             PostQuitMessage(0);
                             return 0;
                             break;
                        case WM_MOUSEMOVE:
                             {
                             int x_ = LOWORD(lParam), y_ = HIWORD(lParam);
                             int stat = (int) wParam;
                             if(stat & MK_LBUTTON){
                                     hdc = GetDC(hwnd);
                                     TextOut(hdc, x_, y_, "O", 1);
                                     ReleaseDC(hwnd, hdc);
                                     }             
                             }break;
                        case WM_LBUTTONDOWN:
                             {
                             int x_ = LOWORD(lParam), y_ = HIWORD(lParam);
                             int stat = (int) wParam;
                             if(stat & MK_LBUTTON){
                                     hdc = GetDC(hwnd);
                                     TextOut(hdc, x_, y_, "O", 1);
                                     SendMessage(hwnd, WM_PAINT, 0, 0);
                                     ReleaseDC(hwnd, hdc);
                                     }
                                                  
                             }break;
                        case WM_RBUTTONDOWN:
                             SendMessage(hwnd, WM_USER, 0, 0);
                             break;
                        case WM_USER:
                             InvalidateRect(hwnd, &rect, TRUE);
                             SendMessage(hwnd, WM_PAINT, 1, 0);
                             break;
                        default: break;
                      }
    
    return (DefWindowProc(hwnd, msg, wParam, lParam));
    }

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    Same problem, but only in Vista

    I have been having the same exact problem, but it only occurs in Vista. 2000 and XP work fine.

    I did a google search on "clip-children" and got no matches related to windows, etc.

    I also looked at the "Cookbook" and there are no references to SetPixel()

    iMaic reports that his system works fine. Is this a Vista machine?

    Alex31 says to use DIBs instead. Alex, any chance you could post a small, concise function that would do the same as SetPixel? I (unfortunately) are forced to use MFC for my application so I cannot use the standard windows method of WM_PAINT, etc.

    Many thanks in advance for ideas and help

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Check out my link. It may be relevant, because it mentions that Vista introduced the API change which caused a problem. At least from what I was able to make out of it.

    Here it is again: The Windows Vista and Windows "Longhorn" Server Developer Story: Application Compatibility Cookbook
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    Thanks dwks.. I've read that link... it seems that the problem is with windows vista, but it doesn't really tells how to solve that problem... I guess I just won't be able to have the enjoyment of drawing pixel by pixel on the screen anymore, how sad

    Well... thanks everyone

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    lol... Actually, I just found the bug...
    If you're using windows vista, you gotta have the aero turned on...
    that's how it worked here...
    I found the answer here
    SetPixel bug in 64-bit Vista for 64-bit applications

    thanks to dwks

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by BobSweetan View Post
    iMaic reports that his system works fine. Is this a Vista machine?
    As per your suspicions, I am in fact running XP.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Windows GDI vs. Java Graphics
    By Perspective in forum Windows Programming
    Replies: 7
    Last Post: 05-07-2007, 10:05 AM
  3. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 05:26 AM
  4. Whats Peoples Problem With the GDI?
    By MicroFiend in forum Game Programming
    Replies: 6
    Last Post: 07-28-2003, 07:52 PM
  5. Double buffering in GDI -- easy?
    By fusikon in forum Game Programming
    Replies: 17
    Last Post: 02-15-2003, 10:03 PM