如何在 Win32 中使用对话框资源?

发布于 2024-12-16 13:04:03 字数 394 浏览 0 评论 0原文

如果没有资源,我可以使用 CreateWindow()CreateWindowEx() 以及 WndProc() 的复杂数组创建 UI 来处理我的事件。

我注意到,如果我在资源视图中右键单击并单击“添加资源”,我可以绘制一个包含所有控件的对话框。如果我可以像通常使用 C# 那样绘制界面,这将为我节省大量时间。

使用资源编辑器绘制界面后,如何从代码创建窗口?有人可以提供一个非常简单的按钮示例,并展示如何处理该按钮上的 WM_COMMAND 事件吗?

另外,这通常是人们创建 GUI 的方式吗?这样做会不会造成灵活性上的损失?即使在 C# 中,我也经常需要用我自己的代码生成的 UI 来补充设计器生成的 UI,但大多数时候我很乐意使用设计器。

Without resources I can create my UI with a complex array of CreateWindow() and CreateWindowEx(), and WndProc() to process my events.

I noticed if I right-click in the resource view and click "add resource", I can draw a dialog box with all the controls. This would save me a huge amount of time if I could draw the interface like I normally do with C#.

After I've drawn the interface with the resource editor, how do I then create the window from code? Can someone provide a very simple example with a button, and show how to handle a WM_COMMAND event on that button please?

Also, is this generally how people create the GUI? Is there any loss in flexible to do this way? Even in C# I often have to supplement designer-generated UI with my own code-generated UI, but the majority of the time I'm quite happy to use designer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2024-12-23 13:04:03

在资源编辑器中创建对话框后,调用 CreateDialog(非模式对话框;您需要像使用 CreateWindow 时一样手动调度消息)或 DialogBox(模式对话框;该函数在您关闭对话框之前不会返回。它会为您进行调度)以使对话框显示。就像将窗口过程传递给 RegisterClass 一样,将对话框过程传递给这些函数以进行对话框回调。 DialogProc 的示例如下所示:

BOOL DialogProc( HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam ){
    switch( iMessage ){
    case WM_COMMAND:
        switch( LOWORD( wParam ) ){
        case BTOK:
            MessageBox( hDlg, "Hello, World!", NULL, NULL );
            return TRUE;
            break;
        }
        break;
    }
    return FALSE;
}

这是创建对话框的基本方法。更复杂的方法通常涉及 OOP,通常将每个资源(按钮、窗口等)包装为 C++ 对象或使用 MFC。

After creating the dialog in the resource editor, call CreateDialog(modeless dialog;you need to dispatch the messages manually just like when you use CreateWindow) or DialogBox(modal dialog; the function does not return until you close the dialog. it does the dispatching for you) to make the dialog show up. Just like you pass in the window proc to RegisterClass, you pass the dialog proc to those functions for the dialog call back. An example of DialogProc looks likes this:

BOOL DialogProc( HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam ){
    switch( iMessage ){
    case WM_COMMAND:
        switch( LOWORD( wParam ) ){
        case BTOK:
            MessageBox( hDlg, "Hello, World!", NULL, NULL );
            return TRUE;
            break;
        }
        break;
    }
    return FALSE;
}

This is a basic way of creating a dialog. More sophisticated method would normally involve OOP, usually wrapping each resource( button, window, etc) as a C++ object or using MFC.

情绪操控生活 2024-12-23 13:04:03

如果您已将按钮或任何控件放置在某个对话框上,则该控件已处于创建状态。为了处理该对话框上这些子控件的消息,您必须重写实现对话框的类中的 OnCommand 方法。

例如:

//CDialog_ControlDlg is my Dialog class derived from CDialog

//IDC_BUTTON_SAMPLE is the ID of the button which was palced on the dialog in the resource Editor..

BOOL CDialog_ControlDlg::OnCommand(WPARAM wParam,LPARAM lparam){
      int iNotiFicationMsg=HIWORD(wParam);//This is thenotification Msg from the child control
      int iCommandId=LOWORD(wParam);//And Control ID of the Child control which caused that Msg
      BOOL result=FALSE;
      switch(iCommandId){
    case IDC_BUTTON_SAMPLE:
        if(iNotiFicationMsg==BN_CLICKED)
        {
         //Your Code for handling this type of Msg for this control..

        }
        break;
    default:
    {
        //Specific Code;

    }

    return result;
}

}

If you have placed your button or any control on some dialog, that control is already in created state. For handling the messages of these child controls on this dialog , you have to override OnCommand Method in the class which is implementing your dialog.

For Example:

//CDialog_ControlDlg is my Dialog class derived from CDialog

//IDC_BUTTON_SAMPLE is the ID of the button which was palced on the dialog in the resource Editor..

BOOL CDialog_ControlDlg::OnCommand(WPARAM wParam,LPARAM lparam){
      int iNotiFicationMsg=HIWORD(wParam);//This is thenotification Msg from the child control
      int iCommandId=LOWORD(wParam);//And Control ID of the Child control which caused that Msg
      BOOL result=FALSE;
      switch(iCommandId){
    case IDC_BUTTON_SAMPLE:
        if(iNotiFicationMsg==BN_CLICKED)
        {
         //Your Code for handling this type of Msg for this control..

        }
        break;
    default:
    {
        //Specific Code;

    }

    return result;
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文