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

前端之家收集整理的这篇文章主要介绍了C Win32api,创建一个没有资源的对话框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是win32api编程的新手.我想知道如何在非gui程序中创建一个对话框(没有任何资源创建).

我看到有一些CreateIndirect函数的例子.这是最好的方法吗?有什么办法吗

谢谢!

解决方法

您将使用 DialogBoxIndirectParamCreateDialogIndirectParam

通过这些功能,它有更多的工作,但是可以在代码中嵌入一个简单的对话框模板作为一个初始化的静态结构体.对话框模板的格式有一些内置可变大小的数组,因此您必须具有特定于特定对话框的结构声明,但这对调试代码起作用.

这样的东西

  1. #define DLGTITLE L"Debug"
  2. #define DLGFONT L"MS Sans Serif"
  3. #define DLGAPPLY L"&Apply"
  4. #define DLGCANCEL L"&Cancel"
  5. #define NUMCHARS(aa) (sizeof(aa)/sizeof((aa)[0]))
  6. #define IDC_BITMAP 99
  7.  
  8. #pragma pack(push,4)
  9. static struct { // dltt
  10.  
  11. DWORD style;
  12. DWORD dwExtendedStyle;
  13. WORD ccontrols;
  14. short x;
  15. short y;
  16. short cx;
  17. short cy;
  18. WORD menu; // name or ordinal of a menu resource
  19. WORD windowClass; // name or ordinal of a window class
  20. WCHAR wszTitle[NUMCHARS(DLGTITLE)]; // title string of the dialog Box
  21. short pointsize; // only if DS_SETFONT flag is set
  22. WCHAR wszFont[NUMCHARS(DLGFONT)]; // typeface name,if DS_SETFONT is set
  23.  
  24. // control info
  25. //
  26. struct {
  27. DWORD style;
  28. DWORD exStyle;
  29. short x;
  30. short y;
  31. short cx;
  32. short cy;
  33. WORD id;
  34. WORD sysClass; // 0xFFFF identifies a system window class
  35. WORD idClass; // ordinal of a system window class
  36. WCHAR wszTitle[NUMCHARS(DLGAPPLY)];
  37. WORD cbCreationData; // bytes of following creation data
  38. // WORD wAlign; // align next control to DWORD boundry.
  39. } apply;
  40.  
  41. struct {
  42. DWORD style;
  43. DWORD exStyle;
  44. short x;
  45. short y;
  46. short cx;
  47. short cy;
  48. WORD id;
  49. WORD sysClass; // 0xFFFF identifies a system window class
  50. WORD idClass; // ordinal of a system window class
  51. WCHAR wszTitle[NUMCHARS(DLGCANCEL)];
  52. WORD cbCreationData; // bytes of following creation data
  53. } cancel;
  54.  
  55. struct {
  56. DWORD style;
  57. DWORD exStyle;
  58. short x;
  59. short y;
  60. short cx;
  61. short cy;
  62. WORD id;
  63. WORD sysClass; // 0xFFFF identifies a system window class
  64. WORD idClass; // ordinal of a system window class
  65. WCHAR wszTitle[1]; // title string or ordinal of a resource
  66. WORD cbCreationData; // bytes of following creation data
  67. } bitmap;
  68.  
  69. } g_DebugDlgTemplate = {
  70.  
  71. WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU // style 0x94c800c4
  72. | DS_MODALFRAME | DS_3DLOOK
  73. | DS_SETFONT,0x0,// exStyle;
  74. 3,// ccontrols
  75. 0,300,180,// menu: none
  76. 0,// window class: none
  77. DLGTITLE,// Window caption
  78. 8,// font pointsize
  79. DLGFONT,{
  80. WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_GROUP | BS_DEFPUSHBUTTON,// 0x50030001
  81. WS_EX_NOPARENTNOTIFY,// 0x4
  82. 190,160,50,14,IDOK,0xFFFF,0x0080,// button
  83. DLGAPPLY,},{
  84. WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,// 0x50010000
  85. WS_EX_NOPARENTNOTIFY,// 0x4
  86. 244,IDCANCEL,// button
  87. DLGCANCEL,{
  88. WS_CHILD | WS_VISIBLE | WS_GROUP | SS_LEFT,// 0x50020000
  89. WS_EX_NOPARENTNOTIFY,// 0x4
  90. 6,6,288,8,IDC_BITMAP,0x0082,// static
  91. L"",};
  92.  
  93. #pragma pack(pop)
  94.  
  95. INT_PTR CALLBACK Debug_DlgProc (
  96. HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  97. {
  98. switch (uMsg)
  99. {
  100. case WM_INITDIALOG:
  101. {
  102. }
  103. break;
  104.  
  105. case WM_COMMAND:
  106. {
  107. UINT wId = LOWORD(wParam);
  108. if (wId == IDOK || wId == IDCANCEL)
  109. {
  110. EndDialog (hwnd,wId);
  111. }
  112. }
  113. break;
  114.  
  115. case WM_CLOSE:
  116. EndDialog(hwnd,IDCANCEL);
  117. break;
  118. }
  119.  
  120. return FALSE;
  121. }
  122.  
  123.  
  124. LRESULT DoDebugDialog(HWND hwndApp,LPVOID pvData)
  125. {
  126. HINSTANCE hinst = hwndApp ? (HINSTANCE)(LONG_PTR)GetWindowLongPtr(hwndApp,GWLP_HINSTANCE)
  127. : (HINSTANCE)GetModuleHandle(NULL);
  128.  
  129. return DialogBoxIndirectParamW (hinst,(LPCDLGTEMPLATEW)&g_DebugDlgTemplate,hwndApp,Debug_DlgProc,(LPARAM)pvData);
  130. }

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

猜你在找的C&C++相关文章