#
define
_WIN32_WINNT
0x0500
#
include
<windows.h>
typedef
struct
_MYAPP {
HWND hCtl;
}
MYAPP;
MYAPP MyApp;
WNDPROC DefButtonProc;
#
define
BUTTON_WIDTH
100
#
define
BUTTON_HEIGHT
24
#
define
BUTTON_TEXT
"
QUIT
"
LRESULT CALLBACK WndProc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void
OnCreate
(HWND hwnd);
LRESULT CALLBACK ButtonProc
(HWND hButton, UINT message, WPARAM wParam, LPARAM lParam);
void
OnDrawItem
(HWND hwnd, LPDRAWITEMSTRUCT lpdis);
void
OnCommand
(HWND hwnd, int
CtrlID, int
Event);
int
WINAPI WinMain
(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int
nCmdShow)
{
WNDCLASS wc;
HWND hWnd;
MSG msg;
wc.cbClsExtra =
0
;
wc.cbWndExtra =
0
;
wc.hbrBackground =
CreateSolidBrush
(RGB
(0x99
, 0xCC
, 0xFF
));
wc.hCursor =
LoadCursor
(NULL
, IDC_ARROW);
wc.hIcon =
LoadIcon
(NULL
, IDI_APPLICATION);
wc.hInstance =
hInstance;
wc.lpfnWndProc =
WndProc;
wc.lpszClassName =
"
Classe
1
"
;
wc.lpszMenuName =
NULL
;
wc.style =
CS_HREDRAW |
CS_VREDRAW;
RegisterClass
(&
wc);
hWnd =
CreateWindow
( "
Classe
1
"
, "
Owner-Draw
Button
"
,
WS_POPUP |
WS_BORDER |
WS_CAPTION |
WS_SYSMENU |
WS_MINIMIZEBOX,
100
, 100
, 200
, 100
, NULL
, NULL
, hInstance, NULL
);
ShowWindow
(hWnd, nCmdShow);
while
(GetMessage
(&
msg, NULL
, 0
, 0
))
{
TranslateMessage
(&
msg);
DispatchMessage
(&
msg);
}
return
(int
)msg.wParam;
}
LRESULT CALLBACK WndProc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch
(message)
{
case
WM_CREATE:
OnCreate
(hwnd);
break
;
case
WM_DRAWITEM:
OnDrawItem
(hwnd, (LPDRAWITEMSTRUCT)lParam);
break
;
case
WM_COMMAND:
OnCommand
(hwnd, LOWORD
(wParam), HIWORD
(wParam));
break
;
case
WM_DESTROY:
PostQuitMessage
(0
);
break
;
default
:
return
DefWindowProc
(hwnd, message, wParam, lParam);
}
return
0L
;
}
void
OnCreate
(HWND hwnd)
{
RECT r;
int
x, y;
HWND hButton;
GetClientRect
(hwnd, &
r);
x =
(r.right -
BUTTON_WIDTH) /
2
;
y =
(r.bottom -
BUTTON_HEIGHT) /
2
;
hButton =
CreateWindow
( "
BUTTON
"
, "
"
, WS_CHILD |
WS_VISIBLE |
BS_OWNERDRAW,
x, y, BUTTON_WIDTH, BUTTON_HEIGHT, hwnd, (HMENU)1
, NULL
, NULL
);
DefButtonProc =
(WNDPROC)SetWindowLongPtr
(hButton, GWLP_WNDPROC, (LONG_PTR)ButtonProc);
}
LRESULT CALLBACK ButtonProc
(HWND hButton, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT ret =
0L
;
switch
(message)
{
case
WM_MOUSEMOVE:
if
(MyApp.hCtl !
=
hButton)
{
TRACKMOUSEEVENT tme;
MyApp.hCtl =
hButton;
tme.cbSize =
sizeof
(tme);
tme.dwFlags =
TME_LEAVE;
tme.hwndTrack =
hButton;
TrackMouseEvent
(&
tme);
InvalidateRect
(hButton, NULL
, TRUE);
}
ret =
CallWindowProc
(DefButtonProc, hButton, message, wParam, lParam);
break
;
case
WM_MOUSELEAVE:
MyApp.hCtl =
NULL
;
InvalidateRect
(hButton, NULL
, TRUE);
ret =
CallWindowProc
(DefButtonProc, hButton, message, wParam, lParam);
break
;
default
:
ret =
CallWindowProc
(DefButtonProc, hButton, message, wParam, lParam);
}
return
ret;
}
void
OnDrawItem
(HWND hwnd, LPDRAWITEMSTRUCT lpdis)
{
if
(lpdis-
>
CtlID =
=
1
)
{
HDC hDC =
lpdis-
>
hDC;
LPRECT lprcItem =
&
lpdis-
>
rcItem;
COLORREF Color;
HBRUSH hBrush;
if
(lpdis-
>
itemState &
ODS_SELECTED)
Color =
RGB
(0x33
, 0x33
, 0xCC
);
else
if
(MyApp.hCtl =
=
lpdis-
>
hwndItem)
Color =
RGB
(0x66
, 0x99
, 0xFF
);
else
Color =
RGB
(0x66
, 0xCC
, 0xFF
);
hBrush =
CreateSolidBrush
(Color);
SelectObject
(hDC, hBrush);
SetBkMode
(hDC, TRANSPARENT);
Rectangle
(hDC, lprcItem-
>
left, lprcItem-
>
top, lprcItem-
>
right, lprcItem-
>
bottom);
DrawText
(hDC, BUTTON_TEXT, (int
)strlen
(BUTTON_TEXT), lprcItem, DT_CENTER |
DT_VCENTER |
DT_SINGLELINE);
DeleteObject
(hBrush);
}
}
void
OnCommand
(HWND hwnd, int
CtrlID, int
Event)
{
if
(CtrlID =
=
1
&
&
Event =
=
BN_CLICKED)
SendMessage
(hwnd, WM_CLOSE, 0
, 0
);
}