使用 Windows API 用 C 语言编程:如何绘制命令按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5619909/
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
Programming in C With Windows API: How To Draw A Command Button
提问by Ishan Sharma
Well, I am building a college project in C. GUI has not been taught yet but I want my program to be better, so I am learning Windows API.
嗯,我正在用 C 构建一个大学项目。 GUI 还没有被教授,但我希望我的程序更好,所以我正在学习 Windows API。
I am following this tutorial here: http://www.winprog.org/tutorial/start.htmland it is quite good. It explains lot of things but I am not able to find one thing(even searched Google but everything is oriented towards C++ or C#):
我在这里关注本教程:http: //www.winprog.org/tutorial/start.html,它非常好。它解释了很多事情,但我找不到一件事(甚至搜索过谷歌,但一切都面向 C++ 或 C#):
How do I draw a command button inside the drawn window(which I have learned) and how to accept events for it?
如何在绘制的窗口内绘制命令按钮(我已经学会了)以及如何为其接受事件?
Can you please answer or point me to a good page that explains how I can create a command button using ONLY Windows API and C. No C++ please.
您能否回答或指向一个很好的页面,该页面解释了我如何仅使用 Windows API 和 C 创建命令按钮。请不要使用 C++。
Thanks for your time! :)
谢谢你的时间!:)
回答by
Thisis a tutorial I highly recommend on the Win32 API user interface functions. It's excellent. Roughly speaking, in your callback function (LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
you have several options you can catch:
这是我强烈推荐的关于 Win32 API 用户界面功能的教程。它很棒。粗略地说,在你的回调函数中(LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
你有几个可以捕捉的选项:
switch(msg)
{
case WM_CREATE:
break;
case WM_COMMAND:
break;
/* .. */
}
What you need to do on WM_CREATE
is something like this:
你需要做的WM_CREATE
是这样的:
HWND hWnd_button = CreateWindow(TEXT("button"), TEXT("Quit"),
WS_VISIBLE | WS_CHILD ,
20, 50, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
The reason I've stored the HWND
of that button is that if you want to alter the button at a later date, you'll need that Handle as an argument for SendMessage()
. Now, next up, catching a click. When the button is clicked, it sends WM_COMMAND
to the parent window with the HMENU
casted argument (1 in this case) in wParam
. This works for every control you create (menus, checkboxes etc - if they post more complicated options they may be present in lParam
). So:
我存储该HWND
按钮的原因是,如果您想在以后更改该按钮,您将需要该 Handle 作为SendMessage()
. 现在,接下来,点击一下。当点击该按钮时,将其发送WM_COMMAND
到与所述父窗口HMENU
中(在这种情况下1)铸造参数wParam
。这适用于您创建的每个控件(菜单、复选框等 - 如果它们发布更复杂的选项,它们可能会出现在 中lParam
)。所以:
case WM_COMMAND:
if (LOWORD(wParam) == 1) {
DestroyWindow();
/* or SendMessage(hwnd, WM_CLOSE,0,0); see commments */
}
break;
Catches that particular option. Inside the if
handles that button event.
抓住那个特定的选项。里面if
处理那个按钮事件。
回答by Rune Aamodt
Simply use CreateWindow
with class name "BUTTON"
, style BS_PUSHBUTTON
and parent window as your existing drawn window. The x and y coordinates select the top-left button position in the window. The window name is the text on the button. Also, remember to call ShowWindow
on the returned handle.
只需将CreateWindow
类名"BUTTON"
、样式BS_PUSHBUTTON
和父窗口用作您现有的绘制窗口。x 和 y 坐标选择窗口中左上角的按钮位置。窗口名称是按钮上的文本。另外,请记住调用ShowWindow
返回的句柄。
edit: To accept events for it, first define an ID value like:
编辑:要接受它的事件,首先定义一个 ID 值,如:
#define ID_MYBUTTON 1
#define ID_MYBUTTON 1
Then pass that into the menu-parameter of the CreateWindow call. In your main windows message proc you can now find message by testing for:
然后将其传递到 CreateWindow 调用的菜单参数中。在您的主窗口消息过程中,您现在可以通过测试以下内容来查找消息:
if(message == WM_COMMAND && HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ID_MYBUTTON) { /* button was clicked */ }
if(message == WM_COMMAND && HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ID_MYBUTTON) { /* button was clicked */ }