首页 / 知识

关于C#:不使用资源文件的Windows API对话框

2023-04-15 06:51:00

Windows API dialogs without using resource files

我正在尝试使用C和Windows API创建对话框,但是我不希望在资源文件中定义对话框。我在网上找不到任何好的方法,而且我读过的所有示例似乎都没有以编程方式定义对话框。

我该怎么做?

一个简单的例子就可以了。我还没有做任何复杂的事情。


Raymond Chen写了一些有关对话框管理器的帖子:

  • 对话框管理器,第1部分:热身
  • 对话框管理器,第2部分:创建框架窗口
  • 对话框管理器,第3部分:创建控件
  • 对话框管理器,第4部分:对话框循环
  • 对话框管理器,第5部分:将非模式对话框转换为模式对话框
  • 对话框管理器,第6部分:消息循环中的微妙之处
  • 对话框管理器,第7部分:消息循环中的更多微妙之处
  • 对话框管理器,第8部分:对话框中的自定义导航
  • 对话框管理器,第9部分:对话框中的自定义加速器

如果只想显示一个带有控件的窗口,则可以创建一个不使用资源(.rc)文件/脚本的窗口。

这与对话框不同,但是它可能比以编程方式创建对话框要容易。

首先,有关此操作的一些注意事项:

  • 代替在rc文件中设计对话框,可以手动使用CreateWindow(或CreateWindowEx)创建主窗口的子窗口。 (对于.NET Windows Forms程序员,这些窗口就像Control s)。

  • 此过程完全不是图形化的(您需要手动输入每个窗口的位置和大小),但是我认为这可能是了解如何在后台创建对话框的好方法。

  • 不使用真实的对话框有一些缺点,即在控件之间切换时该选项卡将不起作用。

关于示例:

  • 此示例具有一个带有两个按钮的对话框,一个编辑框(.NET Windows Forms程序员将其视为TextBox)和一个复选框。

Screenshot

已在以下条件下进行了测试:

  • x86构建
  • x64构建
  • Unicode构建(定义了UNICODE_UNICODE)
  • 非Unicode构建(未定义UNICODE_UNICODE)
  • 使用Visual Studio的C编译器构建
  • 使用Visual Studio的C编译器构建
  • 操作系统:Windows 10 64 bit

现在查看代码:

请注意,已添加了大量注释以尝试记录Windows功能,我建议将其复制/粘贴到文本编辑器中,以获得最佳效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// This sample will work either with or without UNICODE, it looks like
// it's recommended now to use UNICODE for all new code, but I left
// the ANSI option in there just to get the absolute maximum amount
// of compatibility.
//
// Note that UNICODE and _UNICODE go together, unfortunately part
// of the Windows API uses _UNICODE, and part of it uses UNICODE.
//
// tchar.h, for example, makes heavy use of _UNICODE, and windows.h
// makes heavy use of UNICODE.
#define UNICODE
#define _UNICODE
//#undef UNICODE
//#undef _UNICODE

#include <windows.h>
#include <tchar.h>

// I made this struct to more conveniently store the
// positions / size of each window in the dialog
typedef struct SizeAndPos_s
{
    int x, y, width, height;
} SizeAndPos_t;

// Typically these would be #defines, but there
// is no reason to not make them constants
const WORD ID_btnHELLO = 1;
const WORD ID_btnQUIT = 2;
const WORD ID_CheckBox = 3;
const WORD ID_txtEdit = 4;
const WORD ID_btnShow = 5;

//                                    x,      y,      width,  height
const SizeAndPos_t mainWindow   =   { 150,    150,    300,    300 };

const SizeAndPos_t btnHello     =   { 20,     50,     80,     25 };
const SizeAndPos_t btnQuit      =   { 120,    50,     80,     25 };
const SizeAndPos_t chkCheck     =   { 20,     90,     185,    35 };

const SizeAndPos_t txtEdit      =   { 20,     150,    150,    20 };
const SizeAndPos_t btnShow      =   { 180,    150,    80,     25 };

HWND txtEditHandle = NULL;

// hwnd:    All window processes are passed the handle of the window
//         that they belong to in hwnd.
// msg:     Current message (e.g., WM_*) from the OS.
// wParam:  First message parameter, note that these are more or less
//          integers, but they are really just"data chunks" that
//          you are expected to memcpy as raw data to float, etc.
// lParam:  Second message parameter, same deal as above.
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch (msg)
    {

    case WM_CREATE:
        // Create the buttons
        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        // Note that the"parent window" is the dialog itself. Since we are
        // in the dialog's WndProc, the dialog's handle is passed into hwnd.
        //
        //CreateWindow( lpClassName,        lpWindowName,       dwStyle,                                x,          y,          nWidth,         nHeight,            hWndParent,     hMenu,              hInstance,      lpParam
        //CreateWindow( windowClassName,    initial text,       style (flags),                          xPos,       yPos,       width,          height,             parentHandle,   menuHandle,         instanceHandle, param);
        CreateWindow(   TEXT("Button"),     TEXT("Hello"),      WS_VISIBLE | WS_CHILD,                  btnHello.x, btnHello.y, btnHello.width, btnHello.height,    hwnd,           (HMENU)ID_btnHELLO, NULL,           NULL);

        CreateWindow(   TEXT("Button"),     TEXT("Quit"),       WS_VISIBLE | WS_CHILD,                  btnQuit.x,  btnQuit.y,  btnQuit.width,  btnQuit.height,     hwnd,           (HMENU)ID_btnQUIT,  NULL,           NULL);

        // Create a checkbox
        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        CreateWindow(  TEXT("button"),      TEXT("CheckBox"),   WS_VISIBLE | WS_CHILD | BS_CHECKBOX,    chkCheck.x, chkCheck.y, chkCheck.width, chkCheck.height,    hwnd,           (HMENU)ID_CheckBox, NULL,           NULL);

        // Create an edit box (single line text editing), and a button to show the text
        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //Handle        = CreateWindow(windowClassName,    windowName,           style,                              xPos,       yPos,       width,          height,             parentHandle,   menuHandle,         instanceHandle, param);
        txtEditHandle   = CreateWindow(TEXT("Edit"),       TEXT("Initial Text"), WS_CHILD | WS_VISIBLE | WS_BORDER,  txtEdit.x,  txtEdit.y,  txtEdit.width,  txtEdit.height,     hwnd,           (HMENU)ID_txtEdit,  NULL,           NULL);

        //CreateWindow( windowClassName,    windowName,         style,                                  xPos,      yPos,      width,          height,           parentHandle,   menuHandle,         instanceHandle, param);
        CreateWindow(   TEXT("Button"),     TEXT("Show"),       WS_VISIBLE | WS_CHILD,                  btnShow.x, btnShow.y, btnShow.width, btnShow.height,    hwnd,           (HMENU)ID_btnShow,  NULL,           NULL);

        // Create an Updown control. Note that this control will allow you to type in non-number characters, but it will not affect the state of the control

        break;

    // For more information about WM_COMMAND, see
    // https://msdn.microsoft.com/en-us/library/windows/desktop/ms647591(v=vs.85).aspx
    case WM_COMMAND:

        // The LOWORD of wParam identifies which control sent
        // the WM_COMMAND message. The WM_COMMAND message is
        // sent when the button has been clicked.
        if (LOWORD(wParam) == ID_btnHELLO)
        {
            MessageBox(hwnd, TEXT("Hello!"), TEXT("Hello"), MB_OK);
        }
        else if (LOWORD(wParam) == ID_btnQUIT)
        {
            PostQuitMessage(0);
        }
        else if (LOWORD(wParam) == ID_CheckBox)
        {
            UINT checked = IsDlgButtonChecked(hwnd, ID_CheckBox);

            if (checked)
            {
                CheckDlgButton(hwnd, ID_CheckBox, BST_UNCHECKED);
                MessageBox(hwnd, TEXT("The checkbox has been unchecked."), TEXT("CheckBox Event"), MB_OK);
            }
            else
            {
                CheckDlgButton(hwnd, ID_CheckBox, BST_CHECKED);
                MessageBox(hwnd, TEXT("The checkbox has been checked."), TEXT("CheckBox Event"), MB_OK);
            }
        }
        else if (LOWORD(wParam) == ID_btnShow)
        {
               int textLength_WithNUL = GetWindowTextLength(txtEditHandle) + 1;
               // WARNING: If you are compiling this for C, please remember to remove the (TCHAR*) cast.
               TCHAR* textBoxText = (TCHAR*) malloc(sizeof(TCHAR) * textLength_WithNUL);

               GetWindowText(txtEditHandle, textBoxText, textLength_WithNUL);

               MessageBox(hwnd, textBoxText, TEXT("Here's what you typed"), MB_OK);

               free(textBoxText);
        }
        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        break;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}


// hInstance: This handle refers to the running executable
// hPrevInstance: Not used. See https://blogs.msdn.microsoft.com/oldnewthing/20040615-00/?p=38873
// lpCmdLine: Command line arguments.
// nCmdShow: a flag that says whether the main application window
//           will be minimized, maximized, or shown normally.
//
// Note that it's necessary to use _tWinMain to make it
// so that command line arguments will work, both
// with and without UNICODE / _UNICODE defined.
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    MSG  msg;
    WNDCLASS mainWindowClass = { 0 };

    // You can set the main window name to anything, but
    // typically you should prefix custom window classes
    // with something that makes it unique.
    mainWindowClass.lpszClassName = TEXT("JRH.MainWindow");

    mainWindowClass.hInstance = hInstance;
    mainWindowClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    mainWindowClass.lpfnWndProc = WndProc;
    mainWindowClass.hCursor = LoadCursor(0, IDC_ARROW);

    RegisterClass(&mainWindowClass);

    // Notes:
    // - The classname identifies the TYPE of the window. Not a C type.
    //   This is a (TCHAR*) ID that Windows uses internally.
    // - The window name is really just the window text, this is
    //   commonly used for captions, including the title
    //   bar of the window itself.
    // - parentHandle is considered the"owner" of this
    //   window. MessageBoxes can use HWND_MESSAGE to
    //   free them of any window.
    // - menuHandle: hMenu specifies the child-window identifier,
    //               an integer value used by a dialog box
    //               control to notify its parent about events.
    //               The application determines the child-window
    //               identifier; it must be unique for all
    //               child windows with the same parent window.

    //CreateWindow( windowClassName,                windowName,             style,                            xPos,         yPos,       width,              height,            parentHandle,   menuHandle,  instanceHandle, param);
    CreateWindow(   mainWindowClass.lpszClassName,  TEXT("Main Window"),    WS_OVERLAPPEDWINDOW | WS_VISIBLE, mainWindow.x, mainWindow.y, mainWindow.width, mainWindow.height, NULL,           0,           hInstance,      NULL);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

// This code is based roughly on tutorial code present at  http://zetcode.com/gui/winapi/

进一步阅读

内置的窗口类集非常有限,因此您可能对如何使用Windows API定义自己的窗口类(" Control s")感到好奇,请参见以下文章:

  • Win32 API中的自定义控件:基础知识(代码项目)
  • WINE仿真器源代码很好地说明了如何实现Windows API,以及如何使自己的窗口类模仿内置类的行为。
  • Zetcode.com的教程

NOTE: I originally intended this post to cover the creation of dialogs programmatically. Due to a mistake on my part I didn't realize that you can't just"show" a window as a dialog. Unfortunately I wasn't able to get the setup mentioned by Raymond Chen working.


看看这个工具包,它描述了如何在没有资源文件的情况下创建对话框。

在WTL中。但是,我敢肯定,您可以直接使用Win32 API来拆分内部组件,以实现相同的目的。


在这里您可以找到如何在不使用资源文件的情况下使用Windows API对话框的方法。

Windows API(仅C Win32 API,无MFC)教程:

Windows API教程


尝试在MSDN中搜索"内存中的对话框模板"。

例如,请参见:对话框


对话框资源文件方法编程

最新内容

相关内容

猜你喜欢