×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Subclassing -> WM_PAINT of child window

Subclassing -> WM_PAINT of child window

Subclassing -> WM_PAINT of child window

(OP)
Hi there, I have problem with subclassing, with painting. Try my code, it looks ok, but doesnt work. What I am doing bad?

I need paint some on my buttons.

#include <windows.h>
#include <stdlib.h>
#include <wingdi.h>

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
HWND handleButton[25];
BOOL RedButton[25];
WNDPROC    PrevWindow[25] ;

char textt[255];

/* Declare Windows procedure */
LRESULT CALLBACK ButtonProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

void SetColor(HDC dc, int ID, bool RED)
{
    HBRUSH NewBrush;
    COLORREF CRED = RGB(255,0,0);
    COLORREF CGREEN = RGB(0,255,0);
    COLORREF DEF;
    
    if (RED==TRUE)
    DEF = CRED;
    else
    DEF = CGREEN;
    
     itoa(ID+1,textt,10);
     NewBrush = CreateSolidBrush(DEF);
     SelectObject(dc, NewBrush);
     Rectangle(dc,1,1,23,23);
     DeleteObject(NewBrush);
     SetBkMode(dc, TRANSPARENT);
     TextOut(dc,8/strlen(textt),4,textt,strlen(textt));
};

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_PARENTDC;                 /* Catch double-clicks */
    wincl.cbSize = sizeof(WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL; /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use light-gray as the background of the window */
    wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

    /* Register the window class, if fail quit the program */
    if(!RegisterClassEx(&wincl)) return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx(
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Game",         /* Title Text */
           WS_OVERLAPPEDWINDOW/*|WS_CLIPCHILDREN| WS_CLIPSIBLINGS*/, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           300,                 /* The programs width */
           315,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow(hwnd, nFunsterStil);
    UpdateWindow(hwnd);
    /* Run the message loop. It will run until GetMessage( ) returns 0 */
    while(GetMessage(&messages, NULL, 0, 0))
    {
           /* Translate virtual-key messages into character messages */
           TranslateMessage(&messages);
           /* Send message to WindowProcedure */
           DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage( ) gave */
    return messages.wParam;
}

LRESULT CALLBACK ButtonProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC dc;
    int id = GetDlgCtrlID(hwnd);

    switch (message)
    {
    
         case WM_LBUTTONDOWN:
         RedButton[id]=~RedButton[id];
         dc = GetDC(handleButton[id]);
         SetColor(dc,id,RedButton[id]);
         ReleaseDC(handleButton[id],dc);
         break;
         case WM_LBUTTONUP:
         InvalidateRect(hwnd,NULL,TRUE);
         break;
         case WM_PAINT:
         dc = GetDC(hwnd);
         SetColor(dc,id,RedButton[id]);
         ReleaseDC(hwnd,dc);
         break;

    }
    return CallWindowProc(PrevWindow[id], hwnd, message, wParam, lParam);
}

/* This function is called by the Windows function DispatchMessage( ) */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   
    switch (message)                  /* handle the messages */
    {
           case WM_CREATE:

                for (int a=0; a<25; a++)
                {
                itoa(a+1,textt,10);
                handleButton[a] = CreateWindow("button", textt, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON , (a%5)*54+20, (a/5)*54+20, 25, 25, hwnd, (HMENU) a, (HINSTANCE) GetWindowLong (hwnd,GWL_HINSTANCE), NULL);
                PrevWindow[a] = (WNDPROC) SetWindowLong(handleButton[a], GWL_WNDPROC,(LONG) ButtonProcedure) ;
                 }

            break;
           case WM_DESTROY:
           PostQuitMessage(0);        /* send a WM_QUIT to the message queue */
           break;
           default:                   /* for messages that we don't deal with */
           return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

RE: Subclassing -> WM_PAINT of child window

I could be wrong, but I don't think you can/should do any painting in response to the WM_LBUTTONDOWN message, like you're trying to do.  Also, for WM_PAINT try calling BeginPaint()...EndPaint() rather than GetDC()...ReleaseDC()

RE: Subclassing -> WM_PAINT of child window

(OP)
Yes I can work with WM_LBUTTONDOWN try something like this

WM_LBUTTONDOWN:
MessageBox(NULL,"Left Button!","Tittle Hello",MB_OK);
break;

BeginPaint() and EndPaint() is bad, because, when I will call it, the buttons will be really invisible just what I painted is visible, thats bad!

With GetDC and ReleaseDC is too something wrong, because, buttons are visible, but my paintintig is invisible. Is visible just when you stop cursor over Minimize button, the tooltip will show paint on button, but the paint have to be visible still!

Try my code, try find whats wrong please.

Thank You for reply.

RE: Subclassing -> WM_PAINT of child window

You need to process the coloring of the control through WM_CTLCOLORBTN message not the WM_Paint Message and you need to return a handle to the brush for the back ground color of the button

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close