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

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 */

void SetColor(HDC dc, int ID, bool RED)
    HBRUSH NewBrush;
    COLORREF CRED = RGB(255,0,0);
    COLORREF CGREEN = RGB(0,255,0);
    if (RED==TRUE)
    DEF = CRED;
     NewBrush = CreateSolidBrush(DEF);
     SelectObject(dc, NewBrush);
     SetBkMode(dc, TRANSPARENT);

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 */
           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);
    /* Run the message loop. It will run until GetMessage( ) returns 0 */
    while(GetMessage(&messages, NULL, 0, 0))
           /* Translate virtual-key messages into character messages */
           /* Send message to WindowProcedure */

    /* 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:
         dc = GetDC(handleButton[id]);
         case WM_LBUTTONUP:
         case WM_PAINT:
         dc = GetDC(hwnd);

    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++)
                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) ;

           case WM_DESTROY:
           PostQuitMessage(0);        /* send a WM_QUIT to the message queue */
           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

Yes I can work with WM_LBUTTONDOWN try something like this

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

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