Win32的:如何创建使用CreateWindowExW()函数ListBox控件? [英] Win32: How to create a ListBox control using the CreateWindowExW() function?
问题描述
我经历过多个网站,文档和教程,他们都说相同的,那就是,任何控制无非就是一个窗口,更在Win32中的API,因此,一种是能够使用 CreateWindowExW ()
函数来创建在主应用程序窗口的ListBox
控制/窗口。
I've been through multiple sites, documents and tutorials and they all say the same, that is, any control is nothing more than a window in Win32's API, hence one is able to use the CreateWindowExW()
function to create a ListBox
control/window over the main application window.
虽然我得到的所有的控制是与不同的窗口概念的 dwStyle 的,我也很难找出如何实例,说出这样的话,在的ListBox
控制。
Though I get the concepts of all controls being windows with different dwStyle, I have a hard time finding out how to instantiate, to say so, the ListBox
control.
我遇到过一个对话框写入有一个 LISTBOX
在其声明中指定如下教程:
I encountered a tutorial where a dialog is written to have a LISTBOX
specified in its declaration as follows:
// 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程序中使用它,像这样:
And using it in his C program like so:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}
现在,这个我能做到充分理解的概念。除此之外,我希望能够创建和设计我的主应用程序窗口添加一个的ListBox
控件。本教程示例不使用 CreateWindowExW()
函数来创建控制,相反,它创建了一个对话框,实际上是应用程序主窗口。
Now, this I am able to do and fully understand the concepts. Aside, I would like to be able to create and design my main application window to add a ListBox
control to. This tutorial example doesn't use the CreateWindowExW()
function to create the control, instead, it creates a dialog that will actually be the main application window.
1? - 如何在code一的ListBox
控件添加到主窗口任何线索
1 - Any clue on how to add a ListBox
control to the main window in code?
我想过创造它,而处理的 WM_CREATE 消息。
I thought about creating it while handling the WM_CREATE message.
2? - 这是一个好主意
3? - 什么是此方案中的最佳实践/方法
推荐答案
为了动态地创建在Win32中的一个控制你需要以下code:
In order to dynamically create a control in Win32's you need the following code:
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.
}
这篇关于Win32的:如何创建使用CreateWindowExW()函数ListBox控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!