How to change the button color(appearance) when mouse over(not using MFC)

Posted on 2004-10-19
Medium Priority
Last Modified: 2010-04-01

I want to change a button appearance (color or shape; but color will do) when the user move the mouse over the button.
Or let say the static text also. Unfortunately, I try not to use MFC...

Hope someone got some clue...thanx
Question by:ajib
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

OnegaZhang earned 345 total points
ID: 12355673
 * A basic example of Win32 programmiwng in C.
 * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.
 * Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
 modified by onega(www.fruitfruit.com) to show WM_CTLCOLOR message handling

#include <windows.h>
#include <string.h>
#include <iostream>

int message_count;
HBRUSH hbrRed, hbrBlue, hbrWhite;
static HWND hwndButton = 0;
 * This is the window function for the main window. Whenever a message is
 * dispatched using DispatchMessage (or sent with SendMessage) this function
 * gets called with the contents of the message.
MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
/* The window handle for the "Click Me" button. */

static int cx, cy;/* Height and width of our button. */

HDC hdc;/* A device context used for drawing */
PAINTSTRUCT ps;/* Also used during window drawing */
RECT rc;/* A rectangle used during drawing */
 * Perform processing based on what kind of message we got.
switch (nMsg)
/* The window is being created. Create our button
 * window now. */
            hbrRed = (HBRUSH)CreateSolidBrush(RGB(255, 0, 0));
            hbrBlue = (HBRUSH)CreateSolidBrush(RGB(0, 0, 255));
            hbrWhite = (HBRUSH)GetStockObject(WHITE_BRUSH);
/* First we use the system fixed font size to choose
 * a nice button size. */
hdc = GetDC (hwnd);
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
GetTextMetrics (hdc, &tm);
cx = tm.tmAveCharWidth * 30;
cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
ReleaseDC (hwnd, hdc);
message_count = 0;
/* Now create the button */
hwndButton = CreateWindow (
"button",/* Builtin button class */
"Click Here",
0, 0, cx, cy,
hwnd,/* Parent is this window. */
(HMENU) 1,/* Control ID: 1 */
((LPCREATESTRUCT) lParam)->hInstance,

return 0;

/* The window is being destroyed, close the application
 * (the child button gets destroyed automatically). */
PostQuitMessage (0);
return 0;

case WM_PAINT:
        char buf[256];
        POINT pt,pttmp;
        RECT rc;
        GetCursorPos(&pt); //in screen coordinates
        wsprintf(buf,"pt:%d,%d, rect:%d,%d,%d,%d  message_count:%d",
/* The window needs to be painted (redrawn). */
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rc);

/* Draw "Hello, World" in the middle of the upper
 * half of the window. */
rc.bottom = rc.bottom / 2;
DrawText (hdc, buf, -1, &rc,

EndPaint (hwnd, &ps);
return 0;

case WM_SIZE:
/* The window size is changing. If the button exists
 * then place it in the center of the bottom half of
 * the window. */
if (hwndButton &&
 wParam == SIZENORMAL)
rc.left = (LOWORD(lParam) - cx) / 2;
rc.top = HIWORD(lParam) * 3 / 4 - cy / 2;
MoveWindow (
rc.left, rc.top, cx, cy, TRUE);
        POINT pt,pttmp;
        RECT rc;
        GetCursorPos(&pt); //in screen coordinates
                return (LONG)hbrRed;
                return (LONG)hbrBlue;
case WM_MOUSEMOVE:    
/* Check the control ID, notification code and
 * control handle to see if this is a button click
 * message from our child button. */
if (LOWORD(wParam) == 1 &&
    HIWORD(wParam) == BN_CLICKED &&
    (HWND) lParam == hwndButton)
/* Our button was clicked. Close the window. */
DestroyWindow (hwnd);
return 0;

/* If we don't handle a message completely we hand it to the system
 * provided default window function. */
return DefWindowProc (hwnd, nMsg, wParam, lParam);

WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
HWND hwndMain;/* Handle for the main window. */
MSG msg;/* A Win32 message structure. */
WNDCLASSEX wndclass;/* A window class structure. */
char*szMainWndClass = "WinTestWin";
/* The name of the main window class */

 * First we create a window class for our main window.

/* Initialize the entire structure to zero. */
memset (&wndclass, 0, sizeof(WNDCLASSEX));

/* This class is called WinTestWin */
wndclass.lpszClassName = szMainWndClass;

/* cbSize gives the size of the structure for extensibility. */
wndclass.cbSize = sizeof(WNDCLASSEX);

/* All windows of this class redraw when resized. */
wndclass.style = CS_HREDRAW | CS_VREDRAW;

/* All windows of this class use the MainWndProc window function. */
wndclass.lpfnWndProc = MainWndProc;

/* This class is used with the current program instance. */
wndclass.hInstance = hInst;

/* Use standard application icon and arrow cursor provided by the OS */
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);

/* Color the background white */
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

 * Now register the window class for use.
RegisterClassEx (&wndclass);

 * Create our main window using that window class.
hwndMain = CreateWindow (
szMainWndClass,/* Class name */
"Hello",/* Caption */
CW_USEDEFAULT,/* Initial x (use default) */
CW_USEDEFAULT,/* Initial y (use default) */
CW_USEDEFAULT,/* Initial x size (use default) */
CW_USEDEFAULT,/* Initial y size (use default) */
NULL,/* No parent window */
NULL,/* No menu */
hInst,/* This program instance */
NULL/* Creation parameters */

 * Display the window which we just created (using the nShow
 * passed by the OS, which allows for start minimized and that
 * sort of thing).
ShowWindow (hwndMain, nShow);
UpdateWindow (hwndMain);

 * The main message loop. All messages being sent to the windows
 * of the application (or at least the primary thread) are retrieved
 * by the GetMessage call, then translated (mainly for keyboard
 * messages) and dispatched to the appropriate window procedure.
 * This is the simplest kind of message loop. More complex loops
 * are required for idle processing or handling modeless dialog
 * boxes. When one of the windows calls PostQuitMessage GetMessage
 * will return zero and the wParam of the message will be filled
 * with the argument to PostQuitMessage. The loop will end and
 * the application will close.
while (GetMessage (&msg, NULL, 0, 0))
    if(msg.hwnd == hwndButton && msg.message==WM_MOUSEMOVE)
        msg.hwnd = hwndMain;   //modify the message for that I have no window Proc for button
TranslateMessage (&msg);
DispatchMessage (&msg);
return msg.wParam;

Author Comment

ID: 12365458
Thnax OnegaZhang. That code did work but actually I want to change the text on the button. Sorry for unclear question. But i'll try to work it out from here with your example. Here's your points. Thanx again.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question