/* my_button.h */
#ifndef _MY_BUTTON_H_
#define _MY_BUTTON_H_
#define BUTTON_POSE_DOWN -1
#define BUTTON_POSE_NORMAL 0
#define BUTTON_POSE_HOVER 1
typedef struct button {
RECT rect;
int state;
WNDPROC old_wndproc;
void *old_userdata;
} button_t;
int button_attach_win (button_t *button, HWND hwnd);
#endif
#ifndef _MY_BUTTON_H_
#define _MY_BUTTON_H_
#define BUTTON_POSE_DOWN -1
#define BUTTON_POSE_NORMAL 0
#define BUTTON_POSE_HOVER 1
typedef struct button {
RECT rect;
int state;
WNDPROC old_wndproc;
void *old_userdata;
} button_t;
int button_attach_win (button_t *button, HWND hwnd);
#endif
my_button.c
#include <windows.h>
#include "my_button.h"
#pragma warning(disable: 4312)
#pragma warning(disable: 4311)
#define WM_ATTACH_WIN(_obj,_hwnd,_new_userdata,_new_wndproc) /
(_obj)->old_userdata = (void *)GetWindowLong ( (_hwnd), GWL_USERDATA); /
(_obj)->old_wndproc = (WNDPROC)GetWindowLong ( (_hwnd), GWL_WNDPROC); /
SetWindowLong ( (_hwnd), GWL_USERDATA, (LONG)_new_userdata); /
SetWindowLong ( (_hwnd), GWL_WNDPROC, (LONG)_new_wndproc)
#define WM_DETACH_WIN(_obj,_hwnd) /
SetWindowLong ( (_hwnd), GWL_USERDATA, (LONG) (_obj)->old_userdata); /
SetWindowLong ( (_hwnd), GWL_WNDPROC, (LONG) (_obj)->old_wndproc)
LRESULT CALLBACK button_wndproc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
button_t *button;
PAINTSTRUCT ps;
HDC hdc;
button = (button_t *)GetWindowLong (hwnd, GWL_USERDATA);
switch (message) {
case WM_KEYDOWN:
case WM_LBUTTONDOWN:
button->state = BUTTON_POSE_DOWN;
InvalidateRect (hwnd, NULL, FALSE);
return 0;
case WM_KEYUP:
case WM_LBUTTONUP:
// MessageBox (NULL, L"Clicked", NULL, 0);
button->state = BUTTON_POSE_NORMAL;
InvalidateRect (hwnd, NULL, FALSE);
return 0;
case WM_MOUSEMOVE:
button->state = BUTTON_POSE_HOVER;
InvalidateRect (hwnd, NULL, FALSE);
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
switch (button->state) {
case BUTTON_POSE_HOVER:
FillRect (hdc, &button->rect, (HBRUSH)GetStockObject (LTGRAY_BRUSH));
break;
case BUTTON_POSE_NORMAL:
FillRect (hdc, &button->rect, (HBRUSH)GetStockObject (DKGRAY_BRUSH));
break;
case BUTTON_POSE_DOWN:
FillRect (hdc, &button->rect, (HBRUSH)GetStockObject (BLACK_BRUSH));
break;
default:
break;
}
EndPaint (hwnd, &ps);
break;
default:
break;
}
return button->old_wndproc (hwnd, message, wparam, lparam);
}
int button_attach_win (button_t *button, HWND hwnd)
{
WM_ATTACH_WIN (button, hwnd, button, button_wndproc);
return 1;
}
#include "my_button.h"
#pragma warning(disable: 4312)
#pragma warning(disable: 4311)
#define WM_ATTACH_WIN(_obj,_hwnd,_new_userdata,_new_wndproc) /
(_obj)->old_userdata = (void *)GetWindowLong ( (_hwnd), GWL_USERDATA); /
(_obj)->old_wndproc = (WNDPROC)GetWindowLong ( (_hwnd), GWL_WNDPROC); /
SetWindowLong ( (_hwnd), GWL_USERDATA, (LONG)_new_userdata); /
SetWindowLong ( (_hwnd), GWL_WNDPROC, (LONG)_new_wndproc)
#define WM_DETACH_WIN(_obj,_hwnd) /
SetWindowLong ( (_hwnd), GWL_USERDATA, (LONG) (_obj)->old_userdata); /
SetWindowLong ( (_hwnd), GWL_WNDPROC, (LONG) (_obj)->old_wndproc)
LRESULT CALLBACK button_wndproc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
button_t *button;
PAINTSTRUCT ps;
HDC hdc;
button = (button_t *)GetWindowLong (hwnd, GWL_USERDATA);
switch (message) {
case WM_KEYDOWN:
case WM_LBUTTONDOWN:
button->state = BUTTON_POSE_DOWN;
InvalidateRect (hwnd, NULL, FALSE);
return 0;
case WM_KEYUP:
case WM_LBUTTONUP:
// MessageBox (NULL, L"Clicked", NULL, 0);
button->state = BUTTON_POSE_NORMAL;
InvalidateRect (hwnd, NULL, FALSE);
return 0;
case WM_MOUSEMOVE:
button->state = BUTTON_POSE_HOVER;
InvalidateRect (hwnd, NULL, FALSE);
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
switch (button->state) {
case BUTTON_POSE_HOVER:
FillRect (hdc, &button->rect, (HBRUSH)GetStockObject (LTGRAY_BRUSH));
break;
case BUTTON_POSE_NORMAL:
FillRect (hdc, &button->rect, (HBRUSH)GetStockObject (DKGRAY_BRUSH));
break;
case BUTTON_POSE_DOWN:
FillRect (hdc, &button->rect, (HBRUSH)GetStockObject (BLACK_BRUSH));
break;
default:
break;
}
EndPaint (hwnd, &ps);
break;
default:
break;
}
return button->old_wndproc (hwnd, message, wparam, lparam);
}
int button_attach_win (button_t *button, HWND hwnd)
{
WM_ATTACH_WIN (button, hwnd, button, button_wndproc);
return 1;
}
extern "C" {
#include "my_button.h"
}
button_t button;
void CAboutDlg::OnBnClickedButton1()
{
button.rect.left = 5;
button.rect.top = 5;
button.rect.right = 100;
button.rect.bottom = 100;
button_attach_win (&button, m_hWnd);
Invalidate ();
}
#include "my_button.h"
}
button_t button;
void CAboutDlg::OnBnClickedButton1()
{
button.rect.left = 5;
button.rect.top = 5;
button.rect.right = 100;
button.rect.bottom = 100;
button_attach_win (&button, m_hWnd);
Invalidate ();
}