GetDlgItem function

本文介绍如何使用GetDlgItem函数来获取指定对话框中控件的句柄。该函数适用于任何父子窗口对,不仅限于对话框。文章还提供了函数的参数说明、返回值解释及使用注意事项。

Retrieves a handle to a control in the specified dialog box.

功能:获得 指定对话框上面 某个控件的句柄

C++

HWND WINAPI GetDlgItem(
  _In_opt_  HWND hDlg,
  _In_      int nIDDlgItem
);

Parameters

参数

hDlg [in, optional]

Type: HWND

A handle to the dialog box that contains the control.

第一个参数:(控件所在的)对话框的句柄

 

nIDDlgItem [in]

Type: int

The identifier of the control to be retrieved.

第二个参数:需要检索的控件的ID号

Return value

返回值

Type: HWND

If the function succeeds, the return value is the window handle of the specified control.

如果函数执行成功,函数的返回值为指定控件的窗口句柄

If the function fails, the return value is NULL, indicating an invalid dialog box handle or a nonexistent control. To get extended error information, call GetLastError.

如果函数执行失败,返回值为NULL,表示无效的对话框句柄(即第一个参数值不对)或者控件不存在。若需要获得详细的错误信息,请调用函数GetLastError.

Remarks

备注

You can use the GetDlgItem function with any parent-child window pair, not just with dialog boxes. As long as the hDlg parameter specifies a parent window and the child window has a unique identifier (as specified by the hMenu parameter in the CreateWindow or CreateWindowEx function that created the child window), GetDlgItem returns a valid handle to the child window.

你不仅可以在对话框中使用GetDlgItem函数,也可以在 parent-child window pair(亲子窗口对?窗口中的窗口 )中使用此函数。只要hDlg参数(第一个参数)指定了父窗口,且子窗口有唯一的ID号(此子窗口通常是通过函数CreateWindow 或 CreateWindowEx而创建的,而此窗口的ID号通常也就是上述两个函数中的参数hMenu)(理解得对不?)。

Examples

For an example, see Initializing a Dialog Box.

Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll
那你把这段代码最关键的几个步骤给我单独列举出来,然后告诉我他们涉及到的知识点以及这样设计的目的:/* The codes below are generated by 'Element Studio'. */ #include <windows.h> #include "resource.h" #include <thread> #include <chrono> #include <functional> // declarations LRESULT __stdcall WndProcdure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); INT_PTR __stdcall DlgProcedure(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); void do_something(std::function<void(void)>); // function defines int __stdcall wWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, wchar_t* lpCmdLine, int iCmdShow)//ui线程 注册窗口类 { wchar_t szAppName[] = L"WinApp"; WNDCLASSEX wcex = { 0 }; wcex.cbSize = sizeof(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = &WndProcdure; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); wcex.lpszMenuName = NULL; wcex.lpszClassName = szAppName; wcex.hIconSm = wcex.hIcon; if (!RegisterClassEx(&wcex)) { return -1; } HWND hWnd = NULL; hWnd = CreateWindowEx(//创建主窗口 WS_EX_OVERLAPPEDWINDOW, szAppName, L"App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow);//显示窗口 UpdateWindow(hWnd); //创建对话框 HWND hDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProcedure); ShowWindow(hDlg, iCmdShow); UpdateWindow(hDlg); //消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0))//不断获取消息 { TranslateMessage(&msg);//转换键盘消息 DispatchMessage(&msg);//分发消息对应处理函数 } return msg.wParam; } LRESULT __stdcall WndProcdure(//主窗口消息处理 HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hDC = NULL; PAINTSTRUCT ps; switch (message) { case WM_CREATE: { return 0; } case WM_PAINT: { hDC = BeginPaint(hWnd, &ps); EndPaint(hWnd, &ps); return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hWnd, message, wParam, lParam); } constinit static HWND hStaticText{ nullptr }; void ui_call_back(void)//回调函数 通知ui更新状态(显示finish) { // switch to ui thread. ::SetWindowText(hStaticText, L"finish!");//更新静态文本显示“finish" } INT_PTR __stdcall//对话框消息处理 DlgProcedure(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: { hStaticText = ::GetDlgItem(hDlg, IDC_STATIC1);//获取静态文本控件 return false; } case WM_CLOSE: { EndDialog(hDlg, 0); return true; } case WM_COMMAND: { switch (LOWORD(wParam)) { case IDCANCEL: { ::PostQuitMessage(0); return true; } case IDOK: { // UI call do... do_something(ui_call_back);//调用后台任务函数 return true; } } return false; } } return false; } using namespace std::chrono_literals; void do_something(std::function<void(void)> cb)//后台线程 { std::jthread th([cb]() -> void//使用多线程来处理 创建一个新的后台线程 分离耗时任务和ui交互 { //模拟了一个耗时操作,如果不使用多线程会导致UI线程被卡住,窗口无法响应鼠标点击,无法刷新。 std::this_thread::sleep_for(5s); cb();//等价于调用 ui_call_back(); }); //通过接收一个std::function类型的cb(回调函数),do_something只需要在任务完成后调用cb()即可 //具体的 UI 更新逻辑(比如显示 “finish!”)由cb决定。 th.detach(); }
08-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值