minigui 自定義按鍵


自定義按鍵
本類按鍵是在靜態框基礎上增加了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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章