Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6614597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:21:28+00:00 2026-05-25T20:21:28+00:00

I’m trying to set pixel by mouse click, but nothing happens when I click.

  • 0

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:21:29+00:00Added an answer on May 25, 2026 at 8:21 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.