简体   繁体   English

如何在Win32 API的对话框中显示自定义消息?

[英]How to show custom messages in a dialog box in Win32 API?

How to show custom messages using a dialog box in Win32 API rather than show them in a default MessageBox function? 如何使用Win32 API中的对话框显示自定义消息,而不是在默认的MessageBox函数中显示它们?

I made a function as follows: 我做了一个函数,如下所示:

void DialogBox_Custom (HWND hWndParent, LPSTR contentToShow)
{   
HWND hDialog = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hWndParent, DialogProc);
if (!IsWindowVisible(hDialog))
{
    ShowWindow(hDialog, SW_SHOW);
}
SetDlgItemText(hDialog, IDC_EDIT1, contentToShow);
}

But when I call this function, the dialog box is appearing like millions of times per second and never ending until I close the program by force. 但是,当我调用此函数时,对话框每秒显示数百万次,并且直到我强行关闭程序后才结束。

Please kindly someone help me make a custom dialog box where I can show some content sent from the parent window to an EDIT control window in the dialog box. 请有人帮助我制作一个自定义对话框,在其中可以显示从父窗口发送到对话框中的EDIT控制窗口的某些内容。

Use the DialogBoxParam function to create the modal (suspend execution until the window is closed) dialog box. 使用DialogBoxParam函数创建模式对话框(挂起执行直到关闭窗口)对话框。

DialogBoxParam(instance, MAKEINTRESOURCE(IDD_YOURDIALOG), hWndParent, YourWndProc, (LPARAM)contentToShow);

You then have to create a function YourWndProc to handle the messages to paint and offer a mechanism to close the window, to allow execution to continue after your DialogBox() call. 然后,您必须创建一个函数YourWndProc来处理要绘制的消息,并提供一种关闭窗口的机制,以允许在DialogBox()调用之后继续执行。

INT_PTR CALLBACK YourWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_INITDIALOG:
        SetDlgItemText(hDlg, IDC_EDIT1, (LPSTR)lParam);
        return (INT_PTR)TRUE;
    case WM_CLOSE:
        EndDialog(hDlg, LOWORD(wParam));
        break;
    }
    return DefWindowProc(hDlg, message, wParam, lParam);
}

Check out this tutorial on winprog.org. 在winprog.org上查看本教程 The steps are: 这些步骤是:

  1. Create a resource file for your custom dialog. 为您的自定义对话框创建资源文件。 Add a CTEXT or LTEXT control, which will contain the message. 添加一个CTEXT或LTEXT控件,其中将包含消息。
  2. Write a dialog procedure for it. 为此编写一个对话过程。 You can set the message, that is, the text of the CTEXT control, using SetDlgItemText in WM_INITDIALOG. 您可以使用WM_INITDIALOG中的SetDlgItemText设置消息,即CTEXT控件的文本。
  3. Open the dialog by calling DialogBox. 通过调用DialogBox打开对话框。

Modal dialogs are similar to MessageBox : your code gets control back when the dialog is closed. 模态对话框类似于MessageBox :关闭对话框时,代码将重新获得控制权。 API: DialogBox , DialogBoxIndirect . API: DialogBoxDialogBoxIndirect

Modeless dialogs are similar to windows: you create them with the help of dialog templates and you get the control back immediately, they live powered by message dispatch. 无模式对话框类似于Windows:您可以在对话框模板的帮助下创建它们,然后立即获得控件,它们由消息分发驱动。 This is what you do but you expect them to act as if they were modal. 这是您要做的,但是您希望他们像模态那样行事。 API: CreateDialog , CreateDialogIndirect . API: CreateDialogCreateDialogIndirect

With both modal and modeless dialog you control the dialog with your own DialogProc and you create the dialog with resource dialog template, which automatically creates controls (and you, of course, can add control in code). 使用模式对话框和无模式对话框,您都可以使用自己的DialogProc控制对话框,并使用资源对话框模板创建对话框,该模板会自动创建控件(当然,您也可以在代码中添加控件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM