windows C++ Win32api,创建一个没有资源的对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2270196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:56:25  来源:igfitidea点击:

C++ Win32api, creating a dialog box without resource

c++windowswinapidialog

提问by huff

I'm new to win32api programming. I would like to know how to create a dialog box within a non-gui program (without any resource created).

我是 win32api 编程的新手。我想知道如何在非 gui 程序中创建一个对话框(没有创建任何资源)。

I've seen some examples with that CreateIndirectfunction. Is it the best method? Any other way?

我已经看到了一些具有该CreateIndirect功能的示例。这是最好的方法吗?还有什么办法吗?

Thanks!

谢谢!

回答by John Knoeller

You would use DialogBoxIndirectParamor CreateDialogIndirectParam

您将使用DialogBoxIndirectParamCreateDialogIndirectParam

With these functions, its a bit more work, but it's possible to imbed a simple dialog template as an initialized static struct in your code. The format of a dialog template has some inplace variable sized arrays, so you have to have a struct declaration that is specific to a particular dialog, but this works OK for debug code.

使用这些函数,它需要做更多的工作,但是可以在代码中嵌入一个简单的对话框模板作为初始化的静态结构。对话框模板的格式有一些就地可变大小的数组,因此您必须有一个特定于特定对话框的结构声明,但这对于调试代码是可行的。

Something like this

像这样的东西

#define DLGTITLE  L"Debug"
#define DLGFONT   L"MS Sans Serif"
#define DLGAPPLY  L"&Apply"
#define DLGCANCEL L"&Cancel"
#define NUMCHARS(aa) (sizeof(aa)/sizeof((aa)[0]))
#define IDC_BITMAP 99

#pragma pack(push, 4)                 
static struct { // dltt 

    DWORD  style; 
    DWORD  dwExtendedStyle; 
    WORD   ccontrols; 
    short  x; 
    short  y; 
    short  cx; 
    short  cy; 
    WORD   menu;         // name or ordinal of a menu resource
    WORD   windowClass;  // name or ordinal of a window class
    WCHAR  wszTitle[NUMCHARS(DLGTITLE)]; // title string of the dialog box
    short  pointsize;       // only if DS_SETFONT flag is set
    WCHAR  wszFont[NUMCHARS(DLGFONT)];   // typeface name, if DS_SETFONT is set

    // control info
    //
    struct {
       DWORD  style; 
       DWORD  exStyle; 
       short  x; 
       short  y; 
       short  cx; 
       short  cy; 
       WORD   id; 
       WORD   sysClass;       // 0xFFFF identifies a system window class
       WORD   idClass;        // ordinal of a system window class
       WCHAR  wszTitle[NUMCHARS(DLGAPPLY)];
       WORD   cbCreationData; // bytes of following creation data
//       WORD   wAlign;         // align next control to DWORD boundry.
    } apply;

    struct {
       DWORD  style; 
       DWORD  exStyle; 
       short  x; 
       short  y; 
       short  cx; 
       short  cy; 
       WORD   id; 
       WORD   sysClass;       // 0xFFFF identifies a system window class
       WORD   idClass;        // ordinal of a system window class
       WCHAR  wszTitle[NUMCHARS(DLGCANCEL)];
       WORD   cbCreationData; // bytes of following creation data
    } cancel;

    struct {
       DWORD  style; 
       DWORD  exStyle; 
       short  x; 
       short  y; 
       short  cx; 
       short  cy; 
       WORD   id; 
       WORD   sysClass;       // 0xFFFF identifies a system window class
       WORD   idClass;        // ordinal of a system window class
       WCHAR  wszTitle[1];    // title string or ordinal of a resource
       WORD   cbCreationData; // bytes of following creation data
    } bitmap;

   } g_DebugDlgTemplate = {

   WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU  // style  0x94c800c4
   | DS_MODALFRAME | DS_3DLOOK 
   | DS_SETFONT,
   0x0,        // exStyle;
   3,          // ccontrols
   0, 0, 300, 180,
   0,                       // menu: none
   0,                       // window class: none
   DLGTITLE,                // Window caption
   8,                       // font pointsize
   DLGFONT,

      {
      WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP | BS_DEFPUSHBUTTON,   // 0x50030001
      WS_EX_NOPARENTNOTIFY, // 0x4
      190,160,50,14,
      IDOK,
      0xFFFF, 0x0080, // button
      DLGAPPLY, 0,
      },

      {
      WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,    // 0x50010000
      WS_EX_NOPARENTNOTIFY, // 0x4
      244,160,50,14,
      IDCANCEL,
      0xFFFF, 0x0080, // button
      DLGCANCEL, 0,
      },

      {
      WS_CHILD | WS_VISIBLE | WS_GROUP | SS_LEFT,    // 0x50020000
      WS_EX_NOPARENTNOTIFY, // 0x4
      6,6,288,8,
      IDC_BITMAP,
      0xFFFF, 0x0082, // static
      L"", 0,
      },
   };

#pragma pack(pop)

INT_PTR CALLBACK Debug_DlgProc (
    HWND   hwnd,
    UINT   uMsg,
    WPARAM wParam,
    LPARAM lParam)
{
    switch (uMsg)
       {
       case WM_INITDIALOG:
           {
           }
           break;

       case WM_COMMAND:
           {
           UINT wId = LOWORD(wParam);
           if (wId == IDOK || wId == IDCANCEL)
              {
              EndDialog (hwnd, wId);
              }
           }
           break;

       case WM_CLOSE:
           EndDialog(hwnd, IDCANCEL);
           break;
       }

    return FALSE;
}


LRESULT DoDebugDialog(HWND hwndApp, LPVOID pvData)
{
   HINSTANCE hinst = hwndApp ? (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hwndApp, GWLP_HINSTANCE) 
                             : (HINSTANCE)GetModuleHandle(NULL);

   return DialogBoxIndirectParamW (hinst, (LPCDLGTEMPLATEW)&g_DebugDlgTemplate, hwndApp,
                                  Debug_DlgProc, (LPARAM)pvData);
}

A more sophisticated solution would be to build up a dialog template structure in memory, but this works well for debug code where the dialog itself doesn't change much.

一个更复杂的解决方案是在内存中建立一个对话框模板结构,但这对于对话框本身没有太大变化的调试代码很有效。