First of all you must create your button ID and HWND globaly like so
Cod: Selectaţi tot
#define BUTTON1 100 // I used 100 as an example you can use any number
HWND BTN1;
You could put it under WM_CREATE, But I chose not to because why would you wait for the loop to start to create the button? I guess its just a minor performance thing.
Here is how you create it. (View source at bottom of post for placement)
Cod: Selectaţi tot
BTN1 = CreateWindowExW(NULL,
L"BUTTON",
L"Click me!",
WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
100,/*x*/
100,/*y*/
100,/*width*/
24,/*height*/
hWnd,
(HMENU)BUTTON1,//Button ID
GetModuleHandle(NULL),
NULL);
Here is our MSG loop found the the other thread.
Cod: Selectaţi tot
case WM_DESTROY: // When user exists app
{
PostQuitMessage(0);
return 0;
}
break;
}
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
Cod: Selectaţi tot
switch(LOWORD(wParam))
{
default:{break;}
}
break;
Cod: Selectaţi tot
switch(LOWORD(wParam))
{
case BUTTON1: {
//Do Something here.
MessageBoxA(NULL,"You Clicked the Button!", "Hey LC!",MB_ICONINFORMATION|MB_OK);
}
default:{break;}
}
break;
FULL CODE:
Cod: Selectaţi tot
#include <windows>
#include <iostream>
//Windows Theme/////////
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
///////////////////////
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
LPCWSTR Form_Title = L"BlueMelon"; // Form Title
#define BUTTON1 100
HWND BTN1;
using namespace std;
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEXW wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEXW));
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEXW);
wClass.cbWndExtra = NULL;
wClass.hbrBackground =(HBRUSH)COLOR_WINDOW;
wClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wClass.hInstance = hInst;
wClass.lpfnWndProc = (WNDPROC)WinProc;
wClass.lpszClassName = L"Window Class";
wClass.lpszMenuName = NULL;
wClass.style = CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBoxA(NULL,"Window class creation failed","Window Class Failed",MB_ICONERROR);
}
HWND hWnd = CreateWindowExW(NULL, // Creating your window
L"Window Class",
Form_Title, // Form Title (As declared earlier)
WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,/*x*/ //Default
CW_USEDEFAULT,/*y*/ //Default
300,/*width*/
300,/*height*/
NULL,
NULL,
hInst,
NULL);
if(!hWnd)
{
MessageBoxA(NULL,"Window creation failed","Window Creation Failed",MB_ICONERROR);
}
BTN1 = CreateWindowExW(NULL,
L"BUTTON",
L"Click me!",
WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
100,/*x*/
100,/*y*/
100,/*width*/
24,/*height*/
hWnd,
(HMENU)BUTTON1,//Button ID
GetModuleHandle(NULL),
NULL);
ShowWindow(hWnd,nShowCmd);
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg); // Send into Loop
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg) // msg loop
{
case WM_COMMAND:
{
switch(LOWORD(wParam)) // Used for controls to be added later
{
case BUTTON1: {
//Do Something here. Example:
MessageBoxA(BTN1,"You Clicked the Button!", "Hey LC!",MB_ICONINFORMATION|MB_OK);
}
default:{break;}
}
break;
case WM_DESTROY: // When user exists app
{
PostQuitMessage(0);
return 0;
}
break;
}
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
Enable/Disable
Cod: Selectaţi tot
EnableWindow( GetDlgItem(hWnd,BUTTON1), false );//Disable
EnableWindow( GetDlgItem(hWnd,BUTTON1), true ); // Enable
Cod: Selectaţi tot
SetWindowTextA(BTN1,"YourText");
Credits to BlueMelon.