在Win32 SDK環境下,怎麼來建立經常使用的那些基本控件呢?咱們知道若是用MFC,簡單的拖放便可完成大多數控件的建立,可是咱們既然是用Windows SDK API編程,固然是從根上解決這個問題,實際上MFC的下層也是經過這些API完成的。程序員
實際上控件也是窗口,只不過是被微軟預先建立好的一種窗口類,好比button就是一個類名爲「BUTTON」的類。既然控件其實是窗口,固然也是用CreateWindow或者CreateWindowEx這個函數來完成的,第二個函數除了多一個擴展風格以外,其餘徹底同樣,下面以主要以CreateWindow爲例說明,該函數原型以下(實際上在第一篇已經講過了):編程
HWND CreateWindow(LPCTSTR lpClassName,windows
LPCTSTR lpWindowName,微信
DWORD dwStyle,微信公衆平臺
int x, int y, int nWidth, int nHeight,函數
HWND hWndParent,ui
HMENU hMenu,spa
HANDLE hInstance,.net
PVOID lpParam );code
lpClassName:控件的類名
lpWindowName:控件的名字或者文本
dwStyle:控件風格
x,y,nWidth,nHeight:控件的座標和寬高
hWndParent:控件的父窗口
hMenu:控件的ID
hInstance:控件設置爲空
lpParam:控件設爲空
那麼具體怎麼實現各個基本的控件呢?下面咱們來先依次舉例。
建立標籤/靜態文本
CreateWindow(TEXT("STATIC"), TEXT("Test String"), WS_CHILD|WS_VISIBLE, 10, 10, 80, 20, hWnd, (HMENU)IDC_STATIC1, NULL, NULL);
建立button
CreateWindow(TEXT("BUTTON"), TEXT("Click Me"), WS_CHILD|WS_VISIBLE, 10, 40, 80, 20, hWnd, (HMENU)IDC_BUTTON1, NULL, NULL);
建立編輯框
CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), NULL, WS_CHILD|WS_VISIBLE|WS_BORDER, 10, 70, 80, 20, hWnd, (HMENU)IDC_EDIT1, NULL, NULL);
建立Groupbox
CreateWindow(TEXT("BUTTON"), TEXT("frame"), WS_CHILD|WS_VISIBLE|BS_GROUPBOX, 120, 10, 130, 50, hWnd, (HMENU)IDC_FRAME1, NULL, NULL);
建立單選框
CreateWindow(TEXT("BUTTON"), TEXT("radio button"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON, 134, 30, 40, 20, hWnd, (HMENU)IDC_MALE, NULL, NULL);
建立複選框
CreateWindow(TEXT("BUTTON"), TEXT("紅色"), WS_CHILD|WS_VISIBLE|BS_CHECKBOX|BS_AUTOCHECKBOX, 120, 70, 54, 20, hWnd, (HMENU)IDC_CKBRED, NULL, NULL);
建立列表框
CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("LISTBOX"), NULL, WS_CHILD|WS_VISIBLE|LBS_STANDARD, 10, 100, 80, 90, hWnd, (HMENU)IDC_LISTBOX1, NULL, NULL);
建立組合框
CreateWindow(TEXT("COMBOBOX"), NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_AUTOHSCROLL|CBS_DROPDOWNLIST, 120, 100, 100, 90, hWnd, (HMENU)IDC_CB1, NULL, NULL);
建立滾動條
CreateWindow(TEXT("SCROLLBAR"), NULL, WS_CHILD|WS_VISIBLE|SBS_HORZ, 10, 200, 200, 20, hWnd, (HMENU)IDC_SCB1, NULL, NULL);
以上建立各個控件都不好傻瓜化,你能夠複製下來改改座標、文本、風格、ID等就能夠按照你的須要建立了。
下面咱們就來實戰一下,把這些經常使用的控件在程序中實現,下面咱們來實現一個完整的windows程序,程序中包含了這些經常使用的控件和更多的用法。
/* 經常使用控件實例 */
#include <windows.h>
#define IDC_STATIC1 1001
#define IDC_BUTTON1 1002
#define IDC_EDIT1 1003
#define IDC_FRAME1 1004
#define IDC_MALE 2001
#define IDC_FEMALE 2002
#define IDC_LISTBOX1 1005
#define IDC_CB1 1006
#define IDC_CKBRED 1007
#define IDC_CKBGRN 1008
#define IDC_CKBBLU 1009
#define IDC_SCB1 1010
#define IDC_SCB2 1011static TCHAR szAppName[] = TEXT("HelloWin32");
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass;wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;if (!RegisterClass(&wndclass))
{
MessageBox (NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}hWnd = CreateWindow(szAppName, // window class name
szAppName, // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
400, // initial x size
300, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parametersShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}return msg.wParam;
}static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hCtrl;switch (message)
{
case WM_CREATE:
CreateWindow(TEXT("STATIC"), TEXT("Test String"), WS_CHILD|WS_VISIBLE, 10, 10, 80, 20,
hWnd, (HMENU)IDC_STATIC1, NULL, NULL);CreateWindow(TEXT("BUTTON"), TEXT("Click Me"), WS_CHILD|WS_VISIBLE, 10, 40, 80, 20,
hWnd, (HMENU)IDC_BUTTON1, NULL, NULL);
CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), NULL, WS_CHILD|WS_VISIBLE|WS_BORDER, 10, 70, 80, 20,
hWnd, (HMENU)IDC_EDIT1, NULL, NULL);CreateWindow(TEXT("BUTTON"), TEXT("性別"), WS_CHILD|WS_VISIBLE|BS_GROUPBOX, 120, 10, 130, 50,
hWnd, (HMENU)IDC_FRAME1, NULL, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("男"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON, 134, 30, 40, 20,
hWnd, (HMENU)IDC_MALE, NULL, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("女"), WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON, 194, 30, 40, 20,
hWnd, (HMENU)IDC_FEMALE, NULL, NULL);CreateWindow(TEXT("BUTTON"), TEXT("紅色"), WS_CHILD|WS_VISIBLE|BS_CHECKBOX|BS_AUTOCHECKBOX, 120, 70, 54, 20,
hWnd, (HMENU)IDC_CKBRED, NULL, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("綠色"), WS_CHILD|WS_VISIBLE|BS_CHECKBOX|BS_AUTOCHECKBOX, 180, 70, 54, 20,
hWnd, (HMENU)IDC_CKBGRN, NULL, NULL);
CreateWindow(TEXT("BUTTON"), TEXT("藍色"), WS_CHILD|WS_VISIBLE|BS_CHECKBOX|BS_AUTOCHECKBOX, 240, 70, 54, 20,
hWnd, (HMENU)IDC_CKBBLU, NULL, NULL);hCtrl = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("LISTBOX"), NULL, WS_CHILD|WS_VISIBLE|LBS_STANDARD, 10, 100, 80, 90,
hWnd, (HMENU)IDC_LISTBOX1, NULL, NULL);
SendMessage(hCtrl, LB_ADDSTRING, 0, (LPARAM)TEXT("list str1"));
SendMessage(hCtrl, LB_ADDSTRING, 0, (LPARAM)TEXT("list str2"));
SendMessage(hCtrl, LB_ADDSTRING, 0, (LPARAM)TEXT("list str3"));
SendMessage(hCtrl, LB_ADDSTRING, 0, (LPARAM)TEXT("list str4"));hCtrl = CreateWindow(TEXT("COMBOBOX"), NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_AUTOHSCROLL|CBS_DROPDOWNLIST,
120, 100, 100, 90, hWnd, (HMENU)IDC_CB1, NULL, NULL);
SendMessage(hCtrl, CB_ADDSTRING, 0, (LPARAM)TEXT("comb str1"));
SendMessage(hCtrl, CB_ADDSTRING, 0, (LPARAM)TEXT("comb str2"));
SendMessage(hCtrl, CB_ADDSTRING, 0, (LPARAM)TEXT("comb str3"));
SendMessage(hCtrl, CB_ADDSTRING, 0, (LPARAM)TEXT("comb str4"));
SendMessage(hCtrl, CB_SETCURSEL, 1, 0);CreateWindow(TEXT("SCROLLBAR"), NULL, WS_CHILD|WS_VISIBLE|SBS_HORZ, 10, 200, 200, 20,
hWnd, (HMENU)IDC_SCB1, NULL, NULL);
CreateWindow(TEXT("SCROLLBAR"), NULL, WS_CHILD|WS_VISIBLE|SBS_VERT, 240, 100, 20, 120,
hWnd, (HMENU)IDC_SCB2, NULL, NULL);
return 0;case WM_COMMAND:
{
HWND hwndTmp;
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDC_BUTTON1:
hwndTmp = GetDlgItem(hWnd, IDC_STATIC1);
SetWindowText(hwndTmp, TEXT("New Text"));
MessageBox(hWnd, TEXT("Text Changed!"), TEXT("info"), MB_OK);
break;
}
}
return 0;case WM_CTLCOLORSTATIC:
{
HDC hdc = (HDC)wParam;
SetTextColor(hdc, RGB(0xFF, 0x00, 0x00));
}
return (BOOL)((HBRUSH)GetStockObject(NULL_BRUSH));case WM_DESTROY:
PostQuitMessage(0);
return 0;
}return DefWindowProc (hWnd, message, wParam, lParam);
}
編譯一下運行,結果以下:
怎麼樣,是否是很簡單,雖然咱們的控件建立比MFC麻煩一下,可是咱們是從「根」上來解決問題的,感受是不同的!有了這些基本的用法,你就能夠構建更加複製的Windows界面了。趕快去試試吧。
轉載請註明原地址:http://www.coderonline.net/?p=680
身爲一名IT技術人員磨練本身的技術是必不可少的,歡迎加入QQ羣:程序員互動聯盟(254241126),能夠與大牛在線隨時討論本身感興趣的話題,大牛天天爲你奉獻一篇技術原創,讓本身用最少的時間學到不少東西,我在程序員互動聯盟期待您的加入:)
若是你們想更深刻了解更多的編程知識,請關注咱們的公衆號,及時溝通,這裏有各類大牛幫你解決問題。微信公衆平臺:程序員互動聯盟(coder_online)。
歡迎加入咱們的QQ羣:程序員互動聯盟(254241126)