Win32:如何使用CreateWindowExW()函数创建ListBox控件?
我浏览过多个站点、文档和教程,他们都说相同,即任何控件都只不过是 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 - 这种情况下的最佳实践/方法是什么?
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.
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.
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
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);
}
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 - Any clue on how to add a ListBox
control to the main window in code?
I thought about creating it while handling the WM_CREATE message.
2 - Is this a good idea?
3 - What is the best practice/approach in this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了在 Win32 中动态创建控件,您需要以下代码:
In order to dynamically create a control in Win32's you need the following code: