How to create 2 push buttons

//Iam not an expert in WIN32
// how can I create two push buttons? I got a source code but there is
// only one push button, I need two push buttons
// find here the code
#include <windows.h>
#define IDB_BUT 1001

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode)
{
WNDCLASSEX WinClass = { 0 };

WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.hInstance = hThisInst;
WinClass.lpszClassName = "BUTEXP";
WinClass.lpfnWndProc = WindowFunc;
WinClass.style = 0;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.lpszMenuName = NULL;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

if (!RegisterClassEx(&WinClass)) {
MessageBox(NULL, "Cannot register class", "Windows error", MB_OK);
return 1;
}

HWND hWnd;

if (!(hWnd = CreateWindow("BUTEXP", "Button Example", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, NULL, hThisInst, NULL))) {
MessageBox(NULL, "Cannot create main window", "Windows error", MB_OK);
return 2;

}

ShowWindow(hWnd, WinMode);
UpdateWindow(hWnd);

MSG Message;

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

return Message.wParam;
}

LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
CreateWindow("button", "Press Me!", WS_VISIBLE | WS_CHILD, 20, 100, 120, 30, hWnd, (HMENU) IDB_BUT, NULL, NULL);
break;

case WM_COMMAND:
switch (HIWORD(wParam))
{
case BN_CLICKED:
MessageBox(NULL, "Button pressed", "Windows", MB_OK);
break;

default:
break;
}
break;

case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, Message, wParam, lParam);
}
return 0;
}
You will have to define a new button, at the top, under the existing definition ie
#define IDB_BUT_2 1002

then create a new button in the WM_CREATE case.

CreateWindow("button", "Press Me!", WS_VISIBLE | WS_CHILD, 20, 200, 120, 30, hWnd, (HMENU) IDB_BUT_2, NULL, NULL);

Note the IDB_BUT_2 (- the new definition) and the 200 (- height adjustment) in the line above compared to your original button

//I tryed but I still have one push button
//Please help me
#include <windows.h>
#define IDB_BUT 1001
#define IDB_BUT_2 1002

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode)
{
WNDCLASSEX WinClass = { 0 };

WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.hInstance = hThisInst;
WinClass.lpszClassName = "BUTEXP";
WinClass.lpfnWndProc = WindowFunc;
WinClass.style = 0;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.lpszMenuName = NULL;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

if (!RegisterClassEx(&WinClass)) {
MessageBox(NULL, "Cannot register class", "Windows error", MB_OK);
return 1;
}

HWND hWnd;

if (!(hWnd = CreateWindow("BUTEXP", "Button Example", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, NULL, hThisInst, NULL))) {
MessageBox(NULL, "Cannot create main window", "Windows error", MB_OK);
return 2;
}

ShowWindow(hWnd, WinMode);
UpdateWindow(hWnd);

MSG Message;

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

return Message.wParam;
}

LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
CreateWindow("button", "Press Me!", WS_VISIBLE | WS_CHILD, 10, 100, 120, 30, hWnd, (HMENU) IDB_BUT, NULL, NULL);
break;

CreateWindow("button", "Press Me!", WS_VISIBLE | WS_CHILD, 80, 300, 300, 30, hWnd, (HMENU) IDB_BUT_2, NULL, NULL);
break;
case WM_COMMAND:
switch (HIWORD(wParam))
{
case BN_CLICKED:
MessageBox(NULL, "Button pressed", "Windows", MB_OK);
break;
break;
default:
break;
}
break;

case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, Message, wParam, lParam);
}
return 0;
}
Delete the break; on line 56
//I tryed and now I have two buttons because I remove break between
// the two creates
//Mithix thanks alot
#include <windows.h>
#define IDB_BUT 1001
#define IDB_BUT2 1002

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode)
{
WNDCLASSEX WinClass = { 0 };

WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.hInstance = hThisInst;
WinClass.lpszClassName = "BUTEXP";
WinClass.lpfnWndProc = WindowFunc;
WinClass.style = 0;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.lpszMenuName = NULL;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

if (!RegisterClassEx(&WinClass)) {
MessageBox(NULL, "Cannot register class", "Windows error", MB_OK);
return 1;
}

HWND hWnd;

if (!(hWnd = CreateWindow("BUTEXP", "Button Example", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, NULL, hThisInst, NULL))) {
MessageBox(NULL, "Cannot create main window", "Windows error", MB_OK);
return 2;
}

ShowWindow(hWnd, WinMode);
UpdateWindow(hWnd);

MSG Message;

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

return Message.wParam;
}

LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
CreateWindow("button", "Press Me1!", WS_VISIBLE | WS_CHILD, 10, 100, 120, 30, hWnd, (HMENU) IDB_BUT, NULL, NULL);
CreateWindow("button", "Press Me2!", WS_VISIBLE | WS_CHILD, 40, 200, 220, 60, hWnd, (HMENU) IDB_BUT2, NULL, NULL);
break;
case WM_COMMAND:
switch (HIWORD(wParam))
{
case BN_CLICKED:
MessageBox(NULL, "Button pressed", "Windows", MB_OK);
break;
break;
default:
break;
}
break;

case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, Message, wParam, lParam);
}
return 0;
}
//Thanks alot Mathix
//You have here a program that makes a difference between
//the two ID Thanks alot
//Go time
#include <windows.h>
#define IDB_BUT 1001
#define IDB_BUT2 1002

LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode)
{
WNDCLASSEX WinClass = { 0 };

WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.hInstance = hThisInst;
WinClass.lpszClassName = "BUTEXP";
WinClass.lpfnWndProc = WindowFunc;
WinClass.style = 0;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.lpszMenuName = NULL;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

if (!RegisterClassEx(&WinClass)) {
MessageBox(NULL, "Cannot register class", "Windows error", MB_OK);
return 1;
}

HWND hWnd;

if (!(hWnd = CreateWindow("BUTEXP", "Button Example", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, NULL, hThisInst, NULL))) {
MessageBox(NULL, "Cannot create main window", "Windows error", MB_OK);
return 2;
}

ShowWindow(hWnd, WinMode);
UpdateWindow(hWnd);

MSG Message;

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

return Message.wParam;
}

LRESULT CALLBACK WindowFunc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
CreateWindow("button", "Press Me1!", WS_VISIBLE | WS_CHILD, 10, 100, 120, 30, hWnd, (HMENU) IDB_BUT, NULL, NULL);
CreateWindow("button", "Press Me2!", WS_VISIBLE | WS_CHILD, 40, 200, 220, 60, hWnd, (HMENU) IDB_BUT2, NULL, NULL);
break;
case WM_COMMAND:
switch (HIWORD(wParam))
{
case BN_CLICKED:
if (wParam==IDB_BUT){MessageBox(NULL, "Button pressed1", "Windows", MB_OK);};
if (wParam==IDB_BUT2){MessageBox(NULL, "Button pressed2", "Windows", MB_OK);};
break;
break;
default:
break;
}
break;

case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, Message, wParam, lParam);
}
return 0;
}
Good Job! :)

now try getting a menu in..
http://www.winprog.org/tutorial/menus.html
closed account (E0p9LyTq)
if (wParam==IDB_BUT)

The button ID is extracted with LOWORD(wParam)

And PLEASE use code tags, it makes it easier to read your source code.

http://www.cplusplus.com/articles/jEywvCM9/

You can go back edit your posts to add code tags.
Topic archived. No new replies allowed.