Programming in C With Windows API: How To Draw A Command Button
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.
edit: To accept events for it, first define an ID value like:
#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:
if(message == WM_COMMAND && HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ID_MYBUTTON) { /* button was clicked */ }
Ishan Sharma
Full Stack Engineer using WordPress & JS. Next steps iOS apps using Swift and design using Sketch. Apart from programming, I can be found writing, gaming and reading.
Updated on June 04, 2022Comments
-
Ishan Sharma almost 2 years
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.
I am following this tutorial here: http://www.winprog.org/tutorial/start.html and 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#):
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.
Thanks for your time! :)
-
Ishan Sharma about 13 yearsSo I have to create a new window for a button? Well, that's something new. I am going to try it. Thanks for quick answer.