按鈕控制元件簡介 :
按鈕控制元是MicroWin 應用程式中最常用的控制元之一。按鈕控制元的型别比較豐富,其中主要有普通按鈕、單選按鈕、複選按鈕和組框按鈕等。
組合框 :
組框的外形為左上角包含文字的矩形框。組框是一種特殊的按鈕形式,雖然它屬於按鈕類控制元,但是它既不處理滑鼠和鍵盤的輸入,也不向其父視窗發送消息,其主要作用在於將控制元分隔成不同的組並加以說明。
程式 :
#include "windows.h"
#define IDB_BUTTON1 100
#define IDB_BUTTON2 101
#define IDB_GROUP1 102
#define IDB_GROUP2 103
#define IDS_STATIC1 104
#define IDS_STATIC2 105
#define IDB_BUTTON3 106
#define IDB_BUTTON4 107
#define IDB_GROUP3 108
#define IDB_GROUP4 109
#define IDS_STATIC3 110
#define IDS_STATIC4 111
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
TCHAR szAppName[] = "BUTTON_DEMO";
TCHAR szAppTitle[] = "Button Demo";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
MwRegisterButtonControl(NULL); //註冊按鈕
MwRegisterStaticControl(NULL);
wndclass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = (WNDPROC)WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = 0;
wndclass.hCursor = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
RegisterClass(&wndclass);
hwnd = CreateWindowEx( 0L,
szAppName, // 視窗類名
szAppTitle, // 視窗標題
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // 視窗樣式
0, // 視窗左上角坐標
0,
320, // 視窗大小
240,
NULL, // 父窗體句柄
NULL, // 主選單句柄
NULL, // 實例句柄
NULL); // 視窗參數
ShowWindow(hwnd, nCmdShow); // 設定視窗顯示模式
UpdateWindow(hwnd); // 立即繪製視窗
while (GetMessage(&msg, NULL, 0, 0)) // 消息循環
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//====================CALLBACK============================================================
//使用CreateWindow()函數創造BUTTON
// hWnd = CreateWindow(“BUTTON”, 預定義按鈕控制元類名
// NULL, 按鈕控制元一般無標題
// WS_CHILD | WS_VISIBLE | ..., 按鈕控制元樣式,樣式說明見表 2.9
// X, Y, 按鈕控制元在視窗中的位置
// nWidth, nHeight, 按鈕控制元的寬度和高度
// hWndParent, 擁有該按鈕控制元的視窗控制碼
// (HMENU)ID, 按鈕控制元標識
// hInstance, 應用程式當前實例控制碼,一般為0
// NULL); 按鈕控制元私有資料
//==================表2.9==================================================================
// BS_PUSHBUTTON 普通按鈕控制元
// BS_DEFPUSHBUTTON 預設的普通按鈕控制元
// BS_CHECKBOX 複選按鈕控制元
// BS_AUTOCHECKBOX 自動複選按鈕控制元
// BS_RADIOBUTTON 單選按鈕控制元
// BS_AUTORADIOBUTTON 自動單選按鈕控制元
// BS_GROUPBOX 組框控制元
// BS_TEXT 表示按鈕上存在文本
// BS_ICON 表示按鈕上帶有圖示(同BS_BITMAP)
// BS_BITMAP 表示按鈕上帶有位元映射(同BS_ICON)
// BS_CENTER 按鈕上的文本居中
// BS_LEFT 按鈕上的文本左對齊
// BS_RIGHT 按鈕上的文本右對齊
// BS_USERBUTTON 用戶定義按鈕
//注意: BS_PUSHBUTTON、BS_DEFPUSHBUTTON、BS_CHECKBOX、BS_AUTOCHECKBOX、
// BS_RADIOBUTTON、BS_AUTORADIOBUTTON 和BS_GROUPBOX 只能選擇其中一
// 種樣式,不能使用“|”選擇多個。
//=========================================================================================
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hGroup1, hGroup2, hGroup3, hGroup4;
switch(message)
{
case WM_CREATE:
CreateWindow("BUTTON", "Group1", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 10, 10, 50, 15, hWnd,(HMENU)IDB_BUTTON1, NULL, NULL);
CreateWindow("BUTTON", "Group2", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 70, 10, 50, 15, hWnd, (HMENU)IDB_BUTTON2, NULL, NULL);
hGroup1 = CreateWindow("BUTTON", "Group1", WS_CHILD | BS_GROUPBOX, 10, 30, 120, 60, hWnd, (HMENU)IDB_GROUP1,NULL,NULL); //創造組框1其父窗為hWnd
hGroup2 = CreateWindow("BUTTON", "Group2", WS_CHILD | BS_GROUPBOX, 140, 30, 120, 60, hWnd, (HMENU)IDB_GROUP2, NULL, NULL); //創造組框2其父窗為hWnd
CreateWindow("STATIC", "This is the first Group Box", WS_CHILD | WS_VISIBLE | SS_CENTER, 5, 20, 110, 30, hGroup1, (HMENU)IDS_STATIC1, NULL, NULL); //創造靜態控制元件其父窗為hGroup1
CreateWindow("STATIC", "This is the second Group Box", WS_CHILD | WS_VISIBLE | SS_CENTER, 5, 20, 110, 30, hGroup2, (HMENU)IDS_STATIC2, NULL, NULL); //創造靜態控制元件其父窗為hGroup2
CreateWindow("BUTTON", "Group3", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 10, 100, 50, 15, hWnd, (HMENU)IDB_BUTTON3, NULL, NULL);
CreateWindow("BUTTON", "Group4", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 70, 100, 50, 15, hWnd, (HMENU)IDB_BUTTON4, NULL, NULL);
hGroup3 = CreateWindow("BUTTON", "Group3", WS_CHILD | BS_GROUPBOX, 10, 120, 120, 60, hWnd, (HMENU)IDB_GROUP3,NULL, NULL);
hGroup4 = CreateWindow("BUTTON", "Group4", WS_CHILD | BS_GROUPBOX, 140, 120, 120, 60, hWnd, (HMENU)IDB_GROUP4, NULL, NULL);
CreateWindow("STATIC", "This is the third Group Box", WS_CHILD | WS_VISIBLE | SS_CENTER, 5, 20, 110, 30, hGroup3, (HMENU)IDS_STATIC1, NULL, NULL);
CreateWindow("STATIC", "This is the fourth Group Box", WS_CHILD | WS_VISIBLE | SS_CENTER, 5, 20, 110, 30, hGroup4, (HMENU)IDS_STATIC2, NULL, NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_BUTTON1:
ShowWindow(hGroup1, 1); //顯示hGroup1
ShowWindow(hGroup2, 0); //關閉hGroup2
break;
case IDB_BUTTON2:
ShowWindow(hGroup1, 0); //關閉hGroup1
ShowWindow(hGroup2, 1); //顯示hGroup2
break;
case IDB_BUTTON3:
ShowWindow(hGroup3, SendMessage((HWND)lParam,
BM_GETCHECK, 0, 0));
//顯示或隱藏hGroup3
break;
case IDB_BUTTON4:
ShowWindow(hGroup4, SendMessage((HWND)lParam,
BM_GETCHECK, 0, 0));
//顯示或隱藏hGroup4
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
運行結果 :