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 8811907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:28:41+00:00 2026-06-14T03:28:41+00:00

I asked my question yesterday but could not get a correct answer. now ill

  • 0

I asked my question yesterday but could not get a correct answer. now ill ask it more clearly.
using win api I created a window and a button. infront of the button there is a circle drawn in green color. once the button is pressed the circle should turn to red color.

(I’m using C++, and mingw compiler.)

here is my code

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hDC;
 HBRUSH brusha;
 brusha=CreateSolidBrush(RGB(0,255,0));

 switch(msg)
{
    case WM_CREATE:
    {

        b=CreateWindowEx(WS_EX_CLIENTEDGE,
            "BUTTON",
            "red",
            WS_CHILD|WS_VISIBLE|
            BS_DEFPUSHBUTTON,
            350,
            100,
            100,
            40,
            hWnd,
            (HMENU)BUTTON,
            GetModuleHandle(NULL),
            NULL);
    }
    break;


    case WM_PAINT:
    {


    hDC=BeginPaint(hWnd,&ps);
    SelectObject(hDC,brusha);
    Ellipse(hDC, 20, 20, 100, 100);
    EndPaint(hWnd, &ps);

    }

    case WM_COMMAND:
      switch(LOWORD(wParam))
        {
            case BUTTON:
            {
            brusha=CreatSolideBrush(RGB(255,0,0));
            InvalidateRect( hWnd,0,false);

            }
        }
  break;


      case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
    break;
}

return DefWindowProc(hWnd,msg,wParam,lParam);

  }

this compiles without errors. but nothing happens on button click

  • 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-06-14T03:28:42+00:00Added an answer on June 14, 2026 at 3:28 am

    Well the problem is that you you always draw with a green brush. Look at your code

    HBRUSH brusha;
    brusha=CreateSolidBrush(RGB(0,255,0));
    
    switch(msg)
    {
        ...
        case WM_PAINT:
        {
        hDC=BeginPaint(hWnd,&ps);
        SelectObject(hDC,brusha);
        ...
    

    In this code brusha is always going to be a green brush when you go into WM_PAINT.

    You seem to think that just because you assign a red brush to the brusha variable in the WM_COMMAND part that somehow that is going to be remembered for the next paint, but that’s not true. Remember that in C++ variables are created afresh each time you enter a function, and destroyed each time you exit a function. So the way you have written the code cannot work.

    Probably the simplest way is to make the hbrusha variable static. Static variables are not created and destroyed each time you enter and exit a funciton. Something like this

    LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
        static HBRUSH brusha;
    
        switch(msg)
        {
        case WM_CREATE:
            brusha=CreateSolidBrush(RGB(0,255,0)); // set the brush green on create
    
            ...
    
            break;
    
    
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case BUTTON:
                brusha=CreatSolideBrush(RGB(255,0,0));
                InvalidateRect( hWnd,0,false);
            }
            break;
    
            ...
    
        }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a question yesterday which got answers but didnt answer the main point.
I asked this question yesterday, but I wrote it far more complicated than I
I asked this question yesterday got a pretty good answer but can't figure out
I asked this question yesterday. But now I realize it was the wrong question.
I asked this question yesterday but not sure if I made clear what I
I asked a similar question yesterday, but now I have a slight twist. Since
I asked a similar question yesterday that was specific to a technology, but now
This is similar to a question I asked yesterday but more specific to the
i asked this question yesterday but it doesnt seem to have asked properly so
I asked a similar question yesterday, but recognize that i need to rephase it

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.