[英]Win32: How to create a ListBox control using the CreateWindowExW() function?
我經歷過多個站點,文檔和教程,他們都說同樣的,也就是說,任何控件只不過是Win32 API中的一個窗口,因此可以使用CreateWindowExW()
函數創建一個ListBox
控件/窗口在主應用程序窗口上。
雖然我將所有控件的概念都視為具有不同dwStyle的窗口,但我很難找到如何實例化ListBox
控件。
我遇到了一個教程,其中編寫了一個對話框,在其聲明中指定了LISTBOX
,如下所示:
// resource.h
#define IDD_MAIN 101
#define IDC_TEXT 1000
#define IDC_NUMBER 1001
#define IDC_LIST 1002
#define IDC_ADD 1003
#define IDC_CLEAR 1004
#define IDC_REMOVE 1005
#define IDC_SHOWCOUNT 1006
// .rc resource file
IDD_MAIN DIALOG DISCARDABLE 0, 0, 207, 156
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Controls One"
FONT 8, "MS Sans Serif"
BEGIN
LTEXT "Add",IDC_STATIC,7,10,14,8
EDITTEXT IDC_TEXT,25,7,120,14,ES_AUTOHSCROLL
EDITTEXT IDC_NUMBER,150,7,21,14,ES_NUMBER
LTEXT "times.",IDC_STATIC,177,10,23,8
LISTBOX IDC_LIST,7,25,138,106,LBS_NOINTEGRALHEIGHT |
LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
PUSHBUTTON "&Add",IDC_ADD,150,30,50,14
PUSHBUTTON "&Remove",IDC_REMOVE,150,47,50,14
PUSHBUTTON "&Clear",IDC_CLEAR,150,63,50,14
LTEXT "This item was added",IDC_STATIC,7,141,66,8
CTEXT "-",IDC_SHOWCOUNT,77,141,32,8
LTEXT "times",IDC_STATIC,114,141,17,8
END
並在他的C程序中使用它如下:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}
現在,我能夠做到並完全理解這些概念。 另外,我希望能夠創建和設計我的主應用程序窗口以添加ListBox
控件。 本教程示例不使用CreateWindowExW()
函數來創建控件,而是創建一個實際上是主應用程序窗口的對話框。
1 - 關於如何在代碼中向主窗口添加ListBox
控件的任何線索?
我想在處理WM_CREATE消息時創建它。
2 - 這是個好主意嗎?
3 - 在這種情況下,最佳做法/方法是什么?
要在Win32中動態創建控件,您需要以下代碼:
HWND hBtn
, hLabel
, hListbox
, hTextBox;
void InitializeComponent(HWND hWnd) {
HINSTANCE hInstance = GetModuleHandle(NULL);
// Adding a Button.
hBtn = CreateWindowExW(WS_EX_APPWINDOW
, L"BUTTON", NULL
, WS_CHILD | WS_VISIBLE
, 327, 7, 70, 21
, hWnd, NULL, hInstance, NULL);
SetWindowTextW(hBtn, L"&Button");
// Adding a Label.
hLabel = CreateWindowExW(WS_EX_CLIENTEDGE
, L"STATIC", NULL
, WS_CHILD | WS_VISIBLE
, 7, 7, 50, 21
, hWnd, NULL, hInstance, NULL);
SetWindowTextW(hLabel, L"Label:");
// Adding a ListBox.
hListBox = CreateWindowExW(WS_EX_CLIENTEDGE
, L"LISTBOX", NULL
, WS_CHILD | WS_VISIBLE | ES_VSCROLL | ES_AUTOVSCROLL
, 7, 35, 300, 200
, hWnd, NULL, hInstance, NULL);
// Adding a TextBox.
hTextBox = CreateWindowExW(WS_EX_CLIENTEDGE
, L"EDIT", NULL
, WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL
, 62, 7, 245, 21
, hWnd, NULL, hInstance, NULL);
SetWindowTextW(hTextBox, L"Input text here...");
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_CREATE:
InitializeComponent(hWnd);
break;
default:
return DefWindowProcW(hWnd, Msg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
// Declaring, defining, registering and creating window here...
// Note that each Window/Control has to have its own Message handling function.
}
HWND hListBox; // Handle for list box control
hListBox = CreateWindowEx(
WS_EX_CLIENTEDGE, // extended window styles
"LISTBOX", // list box window class name
NULL,
WS_CHILD | WS_VISIBLE, // window styles
7, // horizontal position
35, // vertical position
300, // width
200, // height
hWnd,
NULL,
hInstance,
NULL
);
if (!hListBox){
// failed to create list box window - take actions ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.