自定义按键
本类按键是在静态框基础上增加了MSG_MOUSEMOVEIN(鼠标移入和移出窗口)和MSG_LBUTTONUP(鼠标在窗口松手)两个消息,通过发送NotifyParent消息给控件实现自定义控件消息,类似于静态框的超类。同样可以修改源码调用源码API将其他消息交给原控件完成,实现控件的超类。
1、头文件
zk_my_btn.h
#ifndef __ZK_MY_BUTTON_H__
#define __ZK_MY_BUTTON_H__
#define CTRL_MYBUTTON ("my button") //定义控件类名
#define STN_HILITE 0x2000 //鼠标移入按键
#define STN_UNHILITE 0x2001 //鼠标移出按键
#define STN_UNPUSHED 0x2002 //鼠标在按键上松手
BOOL RegisterMyButtonControl (void);
#endif
源文件
zk_my_btn.c
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "zk_my_btn.h"
//按键消息处理
static int MyButtonCtrlProc (HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam)
{
RECT rcClient;
HDC hdc;
const char* spCaption;
UINT uFormat = DT_TOP;
DWORD dwStyle;
BOOL needShowCaption = TRUE;
int id = GetDlgCtrlID(hwnd);
DWORD dwAddData2;
switch (uMsg) {
/*****************自定义消息*********************/
case MSG_MOUSEMOVEIN:
{
if(wParam)
NotifyParent (hwnd, id, STN_HILITE);
else
NotifyParent (hwnd, id, STN_UNHILITE);
break;
}
case MSG_LBUTTONUP:
{
NotifyParent (hwnd, id, STN_UNPUSHED);
break;
}
/*****************原静态框消息*********************/
case MSG_CREATE:
SetWindowBkColor (hwnd, GetWindowElementPixel (hwnd, WE_MAINC_THREED_BODY));
SetWindowAdditionalData2(hwnd, GetWindowAdditionalData(hwnd));
SetWindowBkColor (hwnd, GetWindowElementPixel (hwnd, WE_MAINC_THREED_BODY));
return 0;
case STM_GETIMAGE:
return (int)(GetWindowAdditionalData2(hwnd));
case STM_SETIMAGE:
{
int pOldValue;
pOldValue = (int)(GetWindowAdditionalData2(hwnd));
SetWindowAdditionalData2(hwnd, (DWORD)wParam);
InvalidateRect (hwnd, NULL,
(GetWindowStyle (hwnd) & SS_TYPEMASK) == SS_ICON);
return pOldValue;
}
case MSG_GETDLGCODE:
return DLGC_STATIC;
case MSG_PAINT:
hdc = BeginPaint (hwnd);
SelectFont (hdc, GetWindowFont(hwnd));
GetClientRect (hwnd, &rcClient);
dwStyle = GetWindowStyle (hwnd);
dwAddData2 = GetWindowAdditionalData2(hwnd);
if (dwStyle & WS_DISABLED)
SetTextColor (hdc, GetWindowElementPixelEx (hwnd, hdc, WE_FGC_DISABLED_ITEM));
else
SetTextColor (hdc, GetWindowElementPixelEx (hwnd, hdc, WE_FGC_WINDOW));
// DK[11/01/2010]: For fix bug 4336
switch (dwStyle & SS_TYPEMASK)
{
case SS_SIMPLE:
spCaption = GetWindowCaption (hwnd);
if (spCaption && !(dwStyle & SS_ALIGNMASK)) {
SetBkMode (hdc, BM_TRANSPARENT);
TextOut (hdc, 0, 0, spCaption);
needShowCaption = FALSE;
}
break;
case SS_GRAYRECT:
SetBrushColor (hdc, PIXEL_lightgray);
FillBox (hdc, 0, 0, rcClient.right, rcClient.bottom);
break;
case SS_BLACKRECT:
SetBrushColor (hdc, PIXEL_black);
FillBox (hdc, 0, 0, rcClient.right, rcClient.bottom);
break;
case SS_WHITERECT:
SetBrushColor (hdc, PIXEL_lightwhite);
FillBox (hdc, 0, 0, rcClient.right, rcClient.bottom);
break;
case SS_BLACKFRAME:
SetPenColor (hdc, PIXEL_black);
Rectangle (hdc, 0, 0, rcClient.right - 1, rcClient.bottom - 1);
break;
case SS_GRAYFRAME:
SetPenColor (hdc, PIXEL_lightgray);
Rectangle (hdc, 0, 0, rcClient.right - 1, rcClient.bottom - 1);
break;
case SS_WHITEFRAME:
SetPenColor (hdc, PIXEL_lightwhite);
Rectangle (hdc, 0, 0, rcClient.right - 1, rcClient.bottom - 1);
break;
case SS_GROUPBOX:
{
WINDOWINFO *info;
DWORD color;
spCaption = GetWindowCaption (hwnd);
int FontSize = GetFontHeight (hdc);
if (spCaption)
{
SIZE size;
RECT rect;
if (GetWindowExStyle (hwnd) & WS_EX_TRANSPARENT)
SetBkMode(hdc, BM_TRANSPARENT);
else
SetBkMode (hdc, BM_OPAQUE);
SetBkColor(hdc, GetWindowBkColor (hwnd));
TextOut (hdc, FontSize, 2, spCaption);
GetTextExtent (hdc, spCaption, -1, &size);
rect.left = FontSize - 1;
rect.right = rect.left + size.cx + 2;
rect.top = 0;
rect.bottom = size.cy;
ExcludeClipRect (hdc, &rect);
needShowCaption = FALSE;
}
info = (WINDOWINFO*)GetWindowInfo (hwnd);
color = GetWindowElementAttr (hwnd, WE_MAINC_THREED_BODY);
rcClient.top += (FontSize >> 1);
info->we_rdr->draw_3dbox (hdc,
&rcClient, color, LFRDR_BTN_STATUS_NORMAL);
}
break;
case SS_BITMAP:
if (dwAddData2) {
int x = 0, y = 0, w, h;
PBITMAP bmp = (PBITMAP)(dwAddData2);
if (dwStyle & SS_REALSIZEIMAGE) {
w = bmp->bmWidth;
h = bmp->bmHeight;
if (dwStyle & SS_CENTERIMAGE) {
x = (rcClient.right - w) >> 1;
y = (rcClient.bottom - h) >> 1;
}
}
else {
x = y = 0;
w = RECTW (rcClient);
h = RECTH (rcClient);
}
FillBoxWithBitmap(hdc, x, y, w, h, bmp);
needShowCaption = FALSE;
}
break;
case SS_ICON:
if (dwAddData2) {
int x = 0, y = 0, w, h;
HICON hIcon = (HICON)(dwAddData2);
if (dwStyle & SS_REALSIZEIMAGE) {
GetIconSize (hIcon, &w, &h);
if (dwStyle & SS_CENTERIMAGE) {
x = (rcClient.right - w) >> 1;
y = (rcClient.bottom - h) >> 1;
}
}
else {
x = y = 0;
w = RECTW (rcClient);
h = RECTH (rcClient);
}
DrawIcon (hdc, x, y, w, h, hIcon);
needShowCaption = FALSE;
}
break;
default:
break;
}
if (needShowCaption) {
uFormat = DT_TOP;
switch (dwStyle & SS_ALIGNMASK) {
case SS_LEFT:
uFormat |= DT_LEFT;
break;
case SS_CENTER:
uFormat |= DT_CENTER;
break;
case SS_RIGHT:
uFormat |= DT_RIGHT;
break;
}
if (dwStyle & SS_LEFTNOWORDWRAP) {
uFormat |= DT_SINGLELINE | DT_EXPANDTABS;
}
else {
uFormat |= DT_WORDBREAK;
}
if (dwStyle & SS_NOPREFIX)
uFormat |= DT_NOPREFIX;
spCaption = GetWindowCaption (hwnd);
if (spCaption) {
SetBkMode (hdc, BM_TRANSPARENT);
DrawText (hdc, spCaption, -1, &rcClient, uFormat);
}
}
EndPaint (hwnd, hdc);
return 0;
case MSG_LBUTTONDBLCLK:
if (GetWindowStyle (hwnd) & SS_NOTIFY)
NotifyParent (hwnd, id, STN_DBLCLK);
break;
case MSG_LBUTTONDOWN:
if (GetWindowStyle (hwnd) & SS_NOTIFY)
NotifyParent (hwnd, id, STN_CLICKED);
break;
case MSG_NCLBUTTONDBLCLK:
break;
case MSG_NCLBUTTONDOWN:
break;
case MSG_HITTEST:
dwStyle = GetWindowStyle (hwnd);
if ((dwStyle & SS_TYPEMASK) == SS_GROUPBOX)
return HT_TRANSPARENT;
if (GetWindowStyle (hwnd) & SS_NOTIFY)
return HT_CLIENT;
else
return HT_OUT;
break;
case MSG_FONTCHANGED:
InvalidateRect (hwnd, NULL, TRUE);
return 0;
case MSG_SETTEXT:
SetWindowCaption (hwnd, (char*)lParam);
InvalidateRect (hwnd, NULL, TRUE);
return 0;
default:
break;
}
return DefaultControlProc (hwnd, uMsg, wParam, lParam);
}
//注册按键
BOOL RegisterMyButtonControl (void)
{
WNDCLASS WndClass;
WndClass.spClassName = CTRL_MYBUTTON;
WndClass.dwStyle = WS_NONE;
WndClass.dwExStyle = WS_EX_NONE;
WndClass.hCursor = GetSystemCursor (IDC_ARROW);
WndClass.iBkColor = GetWindowElementPixel (HWND_NULL,WE_MAINC_THREED_BODY);;
WndClass.WinProc = MyButtonCtrlProc;
return AddNewControlClass (&WndClass) == ERR_OK;
}