i know this has probably been asked a million times before..but how do i make a simple 'push button' in win32 programming?
This is a discussion on making a standard button in win32 within the Windows Programming forums, part of the Platform Specific Boards category; i know this has probably been asked a million times before..but how do i make a simple 'push button' in ...
i know this has probably been asked a million times before..but how do i make a simple 'push button' in win32 programming?
Hello psychopath,
I know of a few implementation to get the job done:
Code 1.1: Setting up variables/functions for push buttonCode:#include <windows.h> // Make sure this is setup in your Win32 initialization and stuff... HINSTANCE hInstance; // Default font for button void SetDefaultFont( HWND hWnd, int identifier ){ SendDlgItemMessage(hWnd, identifier, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0)); } // Push button HWND CreateButtonEx( HWND hWnd, int identifier, int x, int y, int width, int height, char* tempText, unsigned long WS_CUSTOM ) { HWND hButtonTemp; hButtonTemp = CreateWindowEx(0, "BUTTON", tempText, WS_CHILD | WS_CUSTOM | WS_VISIBLE, x, y, width, height, hWnd, (HMENU)identifier, hInstance, NULL); SetDefaultFont(hWnd, identifier); return hButtonTemp; }
Now on WM_CREATE all you have to do is:
Now to explain all of this, I'll sum it up. IDBTN will be a handle to your button in case you cant use the HWND handle BTN. Also, the last argument in CreateButtonEx is there in case you want to add some other Window Styles that arent in the functions. I used WS_VISIBLE even though it was already in the implementation because I didn't need any special styles.Code:#define IDBTN 10 HWND BTN; BTN = CreateButtonEx( hWnd, IDBTN, 0, 0, 80, 23, "My Push Btton", WS_VISIBLE );
Now hWnd, is your window (dialog) handle. Which you will see in your Procedure, example:
Of course, if your dialog procedure is not hWnd in your code, just change CreateButtonEx's first argument to the corresponding variable name. Now if you'd like, I can set up a whole code to display all of this in use instead of throwing little tid bits here and there.LRESULT CALLBACK DlgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
In fact here is a link to a code I just wrote: Win32 Button Control
If you have any further questions, please let me know.
Hope this helps,
Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
ok thanks....but how would i identify between multiple buttons?
Well you would just make more handles, example:
Hope that helps,Code:#define IDBTN0 10 #define IDBTN1 11 #define IDBTN2 12 #define IDBTN3 13 #define IDBTN4 14 #define IDBTN5 15 // etc... HWND buttons[5]; // for 6 buttons buttons[0] = CreateButtonEx( hWnd, IDBTN0, 0, 0, 80, 23, "My Push Button", WS_VISIBLE ); buttons[1] = CreateButtonEx( hWnd, IDBTN1, 40, 0, 80, 23, "My Second Push Button", WS_VISIBLE ); buttons[2] = CreateButtonEx( hWnd, IDBTN3, 80, 0, 80, 23, "My Third Push Button", WS_VISIBLE ); // etc...
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
ahhh...ok, thanks
-psychopath