开发者

How to set pixel on mouse click in C++ using WinApi (GDI) in GUI window?

开发者 https://www.devze.com 2023-04-07 11:38 出处:网络
I\'m trying to set pixel by mouse click, but nothing happens when I click. Here is part of my code. First, I control window size changing in WM_SIZE.

I'm trying to set pixel by mouse click, but nothing happens when I click. Here is part of my code.

First, I control window size changing in WM_SIZE. Than, at first time when I want to set pixel by mouse I get window's width and height, then copy window's content to memory HDC and HBITMAP (in Store Window) (HBITMAP size equal to (width,height)). In fact, I copy to memory only clear window.

And than in any case I set pixel to memory DC. In next WM_PAINT message handling I'm drawing memory DC to screen.

.....
case WM_SIZE:
    {
        CheckWidthHeight();
        break;
    }
    case WM_MBUTTONDOWN:
    {
        if (firstTimeDraw)
        {
            CheckWidthHeight();
            StoreWindow();
            firstTimeDraw = false;
        }
        SetPixel(memoryDC, LOWORD(lParam), HIWORD(lParam), RGB(0,0,0));
        break;
    }
    case WM_PAINT:
    {
        RestoreWindow();
        break;
    }
.....

where my functions and variables is:

HDC memoryDC;
HBITMAP memoryBitmap;
int width = 0, height = 0;
bool firstTimeDraw = true;

void CheckWidthHeight()
{
   RECT clientRect;
   GetClientRect(hwnd, &clientRect);
   width = clientRect.right - clientRect.left;
   height = clientRect.bottom - clientRect.top;
}

//Copy real window content to memory window
void StoreWindow()
{
   HDC hDC = GetDC(hwnd);
   memoryDC = CreateCompatibleDC(hDC);
   memoryBitmap = CreateCompatibleBitmap(hDC, width, height);
   SelectObject(memoryDC, memoryBitmap);
   BitBlt(memoryDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY);
   ReleaseDC(hwnd, hDC);
}

//Copy 开发者_高级运维memory windows content to real window at the screen
void RestoreWindow()
{
   PAINTSTRUCT ps;
   HDC hDC = BeginPaint(hwnd, &ps);
   memoryDC = CreateCompatibleDC(hDC);
   SelectObject(memoryDC, memoryBitmap);
   BitBlt(hDC, 0, 0, width, height, memoryDC, 0, 0, SRCCOPY);
   EndPaint(hwnd, &ps);
}

What I'm doing wrong?

UPD:

A shot in the dark: You're handling the middle button click. Are you by any chance clicking on the left or right mouse buttons? :)

Ok. Now I use WM_LBUTTONUP or WM_LBUTTONDOWN. Nothing happens again.

UPD2:

  1. When you change the memory DC, you'll also want to invalidate the part of the window that is affected so that Windows will generate a WM_PAINT message for it. InvalidateRect would be a good place to start.

I placed this code

RECT rect;
GetClientRect(hwnd, &rect);
InvalidateRect(hwnd, &rect, true);

before EndPaint. Nothing. Than I move it after EndPaint. Nothing.

  1. In the WM_PAINT handler, you need to use a DC provided by BeginPaint and call EndPaint when you're done with it.

I do it in RestoreWindow().

I don't know yet what's the problem...

UPD3:

InvalidateRect() needs to happen in the WM_?BUTTONDOWN handler after the SetPixel (not in RestoreWindow())- it's what tells windows that you want to get a WM_PAINT in the first place.

Ok. I've done it before you wrote this message. Still don't work.

UPD4:

Thank you a lot, Remy! Thank you to all the rest. Now all right!!


Two things.

  1. When you change the memory DC, you'll also want to invalidate the part of the window that is affected so that Windows will generate a WM_PAINT message for it. InvalidateRect would be a good place to start.

  2. In the WM_PAINT handler, you need to use a DC provided by BeginPaint and call EndPaint when you're done with it.


When you call RestoreWindow() to draw the bitmap onscreen, you are wiping out your memoryDC variable that you used to draw the pixels with. The bitmap is still selected into the original HDC that you have now lost, and a bitmap cannot be selected into multiple HDCs at the same time (the MSDN documentation for SelectObject() says as much). So you are not actually drawing the bitmap onscreen at all.

There is no need to call CreateCompatibleDC() or SelectObject() inside of RestoreWindow() because you already have the bitmap and memory HDC set up inside of StoreWindow(), so they use them as-is instead.

Try this:

HDC memoryDC = NULL;
HBITMAP memoryBitmap = NULL;
int width = 0, height = 0;

void CheckWidthHeight() 
{
    RECT clientRect;
    GetClientRect(hwnd, &clientRect);
    width = clientRect.right - clientRect.left;
    height = clientRect.bottom - clientRect.top;
}

void StoreWindow()
{
    HDC hDC = GetDC(hwnd);
    memoryDC = CreateCompatibleDC(hDC);
    memoryBitmap = CreateCompatibleBitmap(hDC, width, height);
    SelectObject(memoryDC, memoryBitmap);
    BitBlt(memoryDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY);
    ReleaseDC(hwnd, hDC);
}

void RestoreWindow()
{
    PAINTSTRUCT ps;
    HDC hDC = BeginPaint(hwnd, &ps);
    if (memoryDC)
        BitBlt(hDC, 0, 0, width, height, memoryDC, 0, 0, SRCCOPY);
    EndPaint(hwnd, &ps);
} 

...
case WM_SIZE:
{
    CheckWidthHeight();
    break;
}

case WM_LBUTTONDOWN:
{
    if (!memoryDC)
        StoreWindow();

    if (memoryDC)
    {
        SetPixel(memoryDC, LOWORD(lParam), HIWORD(lParam), RGB(0,0,0));

        RECT rect;
        rect.left = LOWORD(lParam);
        rect.top = HIWORD(lParam);
        rect.right = rect.left + 1;
        rect.bottom = rect.top + 1;
        InvalidateRect(hwnd, &rect, TRUE);
    }

    break;
}

case WM_PAINT:
{
    RestoreWindow();
    break;
}
...
0

精彩评论

暂无评论...
验证码 换一张
取 消