创建MiniGUI自定义控件
- include <STDIO.H>
- #include <MINIGUI common.h>
- #include <MINIGUI minigui.h>
- #include <MINIGUI gdi.h>
- #include <MINIGUI window.h>
- #include <MINIGUI control.h>
- #include <STRING.H>
- #define _FLAT_WINDOW_STYLE
- /***********************************************************************
- *** 函数原 型:static int ColorEditConProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam)
- static BOOL RegisterColorEditControl (void)
- static void UnregisterColorEditControl (void)
- *** 参数说明:
- *** 返回值 :
- *** 创建人 :王敏敏
- *** 最后修改:
- *** 描述 :"边框颜色可变编辑框"控件,这里利用Minigui中的自定 义控件实现。包括:
- 其回调函数(ColorEditConProc);
- 注册这个控件 (RegisterColorEditControl);
- 注销这个控件 (UnregisterColorEditControl)
- ************************************************************************/
- static int ColorEditConProc ( HWND hwnd, int message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- RECT rc; // rc 为文本矩形
- GetClientRect(hwnd, &rc); // 取 得控件的大小
- switch (message)
- {
- case MSG_CREATE:
- SetWindowBkColor(hwnd, COLOR_black);
- break ;
- case MSG_PAINT:
- hdc = BeginPaint (hwnd);
- //SetBkMode(hdc, BM_TRANSPARENT); // 让 文本框背景透明
- // 设置边框颜色-浅蓝色,并绘制边框
- SetPenColor(hdc, RGB2Pixel (hdc, 11, 244 , 244));
- Rectangle (hdc, rc.left, rc.top, rc.right-1, rc.bottom-1);
- // 设置文本颜色——浅蓝色,文本背景色 ——黑色
- SetTextColor(hdc, RGB2Pixel (hdc, 11, 244 , 244));
- SetBkColor(hdc, COLOR_black);
- rc.right = rc.right-2; // 文字到右边框留的距离为3
- // 输出文本内容,右对齐、上下居中显示
- DrawText(hdc, GetWindowCaption (hwnd), -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- EndPaint (hwnd, hdc);
- break ;
- case MSG_SETTEXT: // 此消息是为了使得SetDlgItemText()函数(此函数会发出MSG_SETTEXT消息)可用
- // 将设置的文本保存到caption 中
- SetWindowCaption (hwnd, ( char *)lParam);
- hdc = GetClientDC (hwnd);
- // 设置边框颜色-绿色,并绘制边 框
- SetPenColor(hdc, RGB2Pixel (hdc, 9, 225 , 24));
- Rectangle (hdc, rc.left, rc.top, rc.right, rc.bottom-1);
- // 设置文本颜色——浅蓝色,文本背景色 ——黑色
- SetTextColor(hdc, RGB2Pixel (hdc, 11, 244 , 244));
- SetBkColor(hdc, COLOR_black);
- rc.right = rc.right-3; // 文字到右边框留的距离为3
- // 用黑色刷新文字区域
- SetBrushColor(hdc, 0);
- FillBox (hdc, rc.left+1, rc.top+1, rc.right-rc.left-1, rc.bottom-rc.top-2); // 加1减1目的是为了防止把边框刷掉
- // 输出文本内容,右对齐、上下居中显示
- DrawText(hdc, ( char *)lParam, -1, &rc, DT_NOCLIP | DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- ReleaseDC (hdc);
- break ;
- }
- // DefaultControlProc()调用 DefaultMainWinProc(),其内包含对MSG_GETTEXT消息的处理
- return DefaultControlProc (hwnd, message, wParam, lParam);
- }
- static BOOL RegisterColorEditControl ( void )
- {
- WNDCLASS MyClass;
- MyClass.spClassName = "coloredit" ;
- MyClass.dwStyle = WS_NONE;
- MyClass.dwExStyle = WS_EX_NONE;
- MyClass.hCursor = GetSystemCursor (IDC_ARROW);
- MyClass.iBkColor = COLOR_black;
- MyClass.WinProc = ColorEditConProc;
- return RegisterWindowClass (&MyClass);
- }
- static void UnregisterColorEditControl ( void )
- {
- UnregisterWindowClass ( "coloredit" );
- }
- /***********************************************************************
- *** 函数原 型:static int MenuWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
- *** 参数说明:
- *** 返回值 :
- *** 创建人 :王敏敏
- *** 最后修改:
- *** 描述 :主窗口回调函数
- ************************************************************************/
- static int MenuWinProc( HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
- {
- HWND hw;
- switch (message) {
- case MSG_CREATE:
- CreateWindow ( "coloredit" , "呼吸率" ,
- WS_VISIBLE, 101,
- 80, 100, 62, 20,
- hWnd, 0);
- hw=CreateWindow (CTRL_TRACKBAR, "" ,
- WS_VISIBLE | TBS_NOTIFY | TBS_NOTICK,
- 102,
- 10,10,280,18,hWnd,0);
- SendMessage(hw,TBM_SETRANGE,1,100);
- break ;
- case MSG_CLOSE:
- DestroyMainWindow (hWnd);
- // 注销自定义控件
- UnregisterColorEditControl ();
- PostQuitMessage (hWnd);
- break ;
- }
- return DefaultMainWinProc(hWnd, message, wParam, lParam);
- }
- /***********************************************************************
- *** 函数原 型:int MiniGUIMain (int argc, const char* argv[])
- *** 参数说明:
- *** 返回值 :
- *** 创建人 :王敏敏
- *** 最后修改:
- *** 描述 :主程序入口函数
- ************************************************************************/
- int MiniGUIMain ( int argc, const char * argv[])
- {
- MSG Msg;
- HWND hMainWnd;
- MAINWINCREATE CreateInfo;
- #ifdef _LITE_VERSION
- SetDesktopRect(0, 0, 578, 555);
- #endif
- // 注册自定义控件
- RegisterColorEditControl();
- CreateInfo.dwStyle = WS_VISIBLE ;
- CreateInfo.dwExStyle = WS_EX_NONE;
- CreateInfo.spCaption = "" ;
- CreateInfo.hMenu = 0;
- CreateInfo.hCursor = GetSystemCursor(0);
- CreateInfo.hIcon = 0;
- CreateInfo.MainWindowProc = MenuWinProc;
- CreateInfo.lx = 0;
- CreateInfo.ty = 0;
- CreateInfo.rx = 578;
- CreateInfo.by = 555;
- CreateInfo.iBkColor = COLOR_black;
- CreateInfo.dwAddData = 0;
- CreateInfo.hHosting = HWND_DESKTOP;
- hMainWnd = CreateMainWindow (&CreateInfo);
- if (hMainWnd == HWND_INVALID)
- return -1;
- ShowWindow(hMainWnd, SW_SHOWNORMAL);
- while (GetMessage(&Msg, hMainWnd)) {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- MainWindowThreadCleanup (hMainWnd);
- return 0;
- }
- #ifndef _LITE_VERSION
- #include <MINIGUI dti.c>
- #endif
学习MiniGUI之控件篇
对每个控件而言,MiniGUI都为其定义了几个重要的数据结构:
1. WNDCLASS:窗口类信息,主要定义了控件的外观,风格等参数,用来注册控件时使用
typedef struct _WNDCLASS
{
/** 类名,比如ListExCtrl */
char* spClassName;
/** internal field, operation type */
DWORD opMask;
/** 窗口的风格 */
DWORD dwStyle;
/** 扩展风格 */
DWORD dwExStyle;
/** 光标句柄 */
HCURSOR hCursor;
/** 背景色的像素值 */
int iBkColor;
/** 窗口消息回调处理函数 */
int (*WinProc) (HWND, int, WPARAM, LPARAM);
/** 私有数据区 */
DWORD dwAddData;
} WNDCLASS;
typedef WNDCLASS* PWNDCLASS;
1. WNDCLASS:窗口类信息,主要定义了控件的外观,风格等参数,用来注册控件时使用
typedef struct _WNDCLASS
{
/** 类名,比如ListExCtrl */
char* spClassName;
/** internal field, operation type */
DWORD opMask;
/** 窗口的风格 */
DWORD dwStyle;
/** 扩展风格 */
DWORD dwExStyle;
/** 光标句柄 */
HCURSOR hCursor;
/** 背景色的像素值 */
int iBkColor;
/** 窗口消息回调处理函数 */
int (*WinProc) (HWND, int, WPARAM, LPARAM);
/** 私有数据区 */
DWORD dwAddData;
} WNDCLASS;
typedef WNDCLASS* PWNDCLASS;
2. CTRLCLASSINFO:以上类信息的控制结构,内部使用
typedef struct _CTRLCLASSINFO
{
char name[MAXLEN_CLASSNAME + 1];
DWORD dwStyle; // Default control styles.
DWORD dwExStyle; // Default control extended styles.
HCURSOR hCursor; // control cursor
int iBkColor; // control background color.
int (*ControlProc)(HWND, int, WPARAM, LPARAM);
// control procedure.
DWORD dwAddData; // the additional data.
int nUseCount; // use count.
struct _CTRLCLASSINFO* next;
// next class info
}CTRLCLASSINFO;
typedef CTRLCLASSINFO* PCTRLCLASSINFO;
typedef struct _CTRLCLASSINFO
{
char name[MAXLEN_CLASSNAME + 1];
DWORD dwStyle; // Default control styles.
DWORD dwExStyle; // Default control extended styles.
HCURSOR hCursor; // control cursor
int iBkColor; // control background color.
int (*ControlProc)(HWND, int, WPARAM, LPARAM);
// control procedure.
DWORD dwAddData; // the additional data.
int nUseCount; // use count.
struct _CTRLCLASSINFO* next;
// next class info
}CTRLCLASSINFO;
typedef CTRLCLASSINFO* PCTRLCLASSINFO;
3. CONTROL:定义了一个具体控件的形状,外观,风格,背景色等参数
typedef struct tagCONTROL
{
short DataType; // the data type
short WinType; // the window type
int left, top; // the position of control in main window's
int right, bottom; // client area.
int cl, ct; // the positio of control client in main window's
int cr, cb; // client area.
DWORD dwStyle; // the styles of child window.
DWORD dwExStyle; // the extended styles of child window.
int iBkColor; // the background color.
HMENU hMenu; // handle of menu.
HACCEL hAccel; // handle of accelerator table.
HCURSOR hCursor; // handle of cursor.
HICON hIcon; // handle of icon.
HMENU hSysMenu; // handle of system menu.
PLOGFONT pLogFont; // pointer to logical font.
HDC privCDC; // the private client DC.
INVRGN InvRgn; // the invalid region of this control.
PGCRINFO pGCRInfo; // pointer to global clip region info struct.
PZORDERNODE pZOrderNode;
// the Z order node,
// only for control with WS_EX_CTRLASMAINWIN.
PCARETINFO pCaretInfo; // pointer to system caret info struct.
DWORD dwAddData; // the additional data.
DWORD dwAddData2; // the second addtional data.
int (*ControlProc) (HWND, int, WPARAM, LPARAM);
char* spCaption; // the caption of control.
int id; // the identifier of child window.
SCROLLBARINFO vscroll; // the vertical scroll bar information.
SCROLLBARINFO hscroll; // the horizital scroll bar information.
PMAINWIN pMainWin; // the main window that contains this control.
struct tagCONTROL* pParent;// the parent of this control.
/*
* Child windows.
*/
struct tagCONTROL* children;
// the first child control.
struct tagCONTROL* active;
// the active child control.
struct tagCONTROL* old_under_pointer;
// the old under pointer child control.
struct tagCONTROL* primitive;
// the primitive child of mouse event.
NOTIFPROC notif_proc; // the notification callback procedure.
/*
* window element data.
*/
struct _wnd_element_data* wed;
/*
* The following members are only implemented for control.
*/
struct tagCONTROL* next; // the next sibling control.
struct tagCONTROL* prev; // the prev sibling control.
PCTRLCLASSINFO pcci; // pointer to Control Class Info struct.
}CONTROL;
typedef CONTROL* PCONTROL;
typedef struct tagCONTROL
{
short DataType; // the data type
short WinType; // the window type
int left, top; // the position of control in main window's
int right, bottom; // client area.
int cl, ct; // the positio of control client in main window's
int cr, cb; // client area.
DWORD dwStyle; // the styles of child window.
DWORD dwExStyle; // the extended styles of child window.
int iBkColor; // the background color.
HMENU hMenu; // handle of menu.
HACCEL hAccel; // handle of accelerator table.
HCURSOR hCursor; // handle of cursor.
HICON hIcon; // handle of icon.
HMENU hSysMenu; // handle of system menu.
PLOGFONT pLogFont; // pointer to logical font.
HDC privCDC; // the private client DC.
INVRGN InvRgn; // the invalid region of this control.
PGCRINFO pGCRInfo; // pointer to global clip region info struct.
PZORDERNODE pZOrderNode;
// the Z order node,
// only for control with WS_EX_CTRLASMAINWIN.
PCARETINFO pCaretInfo; // pointer to system caret info struct.
DWORD dwAddData; // the additional data.
DWORD dwAddData2; // the second addtional data.
int (*ControlProc) (HWND, int, WPARAM, LPARAM);
char* spCaption; // the caption of control.
int id; // the identifier of child window.
SCROLLBARINFO vscroll; // the vertical scroll bar information.
SCROLLBARINFO hscroll; // the horizital scroll bar information.
PMAINWIN pMainWin; // the main window that contains this control.
struct tagCONTROL* pParent;// the parent of this control.
/*
* Child windows.
*/
struct tagCONTROL* children;
// the first child control.
struct tagCONTROL* active;
// the active child control.
struct tagCONTROL* old_under_pointer;
// the old under pointer child control.
struct tagCONTROL* primitive;
// the primitive child of mouse event.
NOTIFPROC notif_proc; // the notification callback procedure.
/*
* window element data.
*/
struct _wnd_element_data* wed;
/*
* The following members are only implemented for control.
*/
struct tagCONTROL* next; // the next sibling control.
struct tagCONTROL* prev; // the prev sibling control.
PCTRLCLASSINFO pcci; // pointer to Control Class Info struct.
}CONTROL;
typedef CONTROL* PCONTROL;
控件注册:
控件在可以被使用之前需要调用RegisterWindowClass函数,在这个函数里 向HWND_DESKTOP窗口 管理器发送MSG_REGISTERWNDCLASS消息。这里可以看到传入的参数是一个pWndClass的指针
return SendMessage (HWND_DESKTOP,
MSG_REGISTERWNDCLASS, 0, (LPARAM)pWndClass) == ERR_OK;
控件在可以被使用之前需要调用RegisterWindowClass函数,在这个函数里 向HWND_DESKTOP窗口 管理器发送MSG_REGISTERWNDCLASS消息。这里可以看到传入的参数是一个pWndClass的指针
return SendMessage (HWND_DESKTOP,
MSG_REGISTERWNDCLASS, 0, (LPARAM)pWndClass) == ERR_OK;
HWND_DESKTOP窗口管理器在它的消息处理函数DesktopWinProc函数中如何处理MSG_REGISTERWNDCLASS
消息?
case MSG_REGISTERWNDCLASS:
return AddNewControlClass ((PWNDCLASS)lParam);
调用了AddNewControlClass函数,该函数将控 件类信息添加到MiniGUI维护的一张保存所有控件的CTRLCLASSINFO的表中去,该表名为ccitable,按字母分成26个链 表,AddNewControlClass函数为待添加控件生成一个CTRLCLASSINFO实例,根据传入的PWNDCLASS结构的实例,填充相应 的数据,并加入到相应的ccitable链表中。
case MSG_REGISTERWNDCLASS:
return AddNewControlClass ((PWNDCLASS)lParam);
调用了AddNewControlClass函数,该函数将控 件类信息添加到MiniGUI维护的一张保存所有控件的CTRLCLASSINFO的表中去,该表名为ccitable,按字母分成26个链 表,AddNewControlClass函数为待添加控件生成一个CTRLCLASSINFO实例,根据传入的PWNDCLASS结构的实例,填充相应 的数据,并加入到相应的ccitable链表中。
创建控件:
注册完控件就可以使用CreateWindow创建一个控件。CreateWindow是CreateWindowEx的一 个宏。
#define CreateWindow(class_name, caption, style, /
id, x, y, w, h, parent, add_data) /
CreateWindowEx(class_name, caption, style, 0, /
id, x, y, w, h, parent, add_data)
CreateWindow首先向HWND_DESKTOP窗口管理器发送 一个MSG_GETCTRLCLASSINFO消息,获取CTRLCLASSINFO信息,然后定义一个PCONTROL变量,对控件进行初始化,然后向 HWND_DESKTOP窗口管理器发送一个MSG_NEWCTRLINSTANCE消息
SendMessage (HWND_DESKTOP,
MSG_NEWCTRLINSTANCE, (WPARAM)hParentWnd, (LPARAM)pNewCtrl);
接着向新创建的控件本身发送MSG_CREATE消息:
if (SendMessage ((HWND)pNewCtrl, MSG_CREATE, (WPARAM)hParentWnd, (LPARAM)dwAddData)) {
SendMessage (HWND_DESKTOP,
MSG_REMOVECTRLINSTANCE, (WPARAM)hParentWnd, (LPARAM)pNewCtrl);
goto error;
}
注册完控件就可以使用CreateWindow创建一个控件。CreateWindow是CreateWindowEx的一 个宏。
#define CreateWindow(class_name, caption, style, /
id, x, y, w, h, parent, add_data) /
CreateWindowEx(class_name, caption, style, 0, /
id, x, y, w, h, parent, add_data)
CreateWindow首先向HWND_DESKTOP窗口管理器发送 一个MSG_GETCTRLCLASSINFO消息,获取CTRLCLASSINFO信息,然后定义一个PCONTROL变量,对控件进行初始化,然后向 HWND_DESKTOP窗口管理器发送一个MSG_NEWCTRLINSTANCE消息
SendMessage (HWND_DESKTOP,
MSG_NEWCTRLINSTANCE, (WPARAM)hParentWnd, (LPARAM)pNewCtrl);
接着向新创建的控件本身发送MSG_CREATE消息:
if (SendMessage ((HWND)pNewCtrl, MSG_CREATE, (WPARAM)hParentWnd, (LPARAM)dwAddData)) {
SendMessage (HWND_DESKTOP,
MSG_REMOVECTRLINSTANCE, (WPARAM)hParentWnd, (LPARAM)pNewCtrl);
goto error;
}
本文介绍如何在MiniGUI中创建一个名为'coloredit'的自定义控件,该控件允许用户设置边框颜色并具有文本编辑功能。通过ColorEditConProc回调函数、RegisterColorEditControl注册及UnregisterColorEditControl注销来实现该控件的创建、操作和销毁。示例代码展示了控件的使用,包括设置边框颜色、文本颜色、以及响应MSG_SETTEXT消息更新文本。
2916

被折叠的 条评论
为什么被折叠?



