I'm new to WinApi and I'm looking to create a simple window inside my program containing a blank parent window and two smaller child buttons "button1" & "button2". with this button I'm hoping to change a bool value from false to true and visa versa, but nearly all the examples I have seen are quite hard to understand, it seems like you have to return an MSG value of some kind which I don't know how to process.
I have a pseudocode below of what I'm trying to do, I have left comments explaining what I want to do at each moment, am I going about it the right way?:
#include <windows.h>
static int buildwindow(){
MSG msg;
//create parent window
HWND hWnd = CreateWindow(TEXT("scrollbar"), TEXT("Parent"), WS_VISIBLE | WS_POPUP,
10, 10, 800, 500, NULL, NULL, NULL, NULL);
//create child window
HWND hWnd1 = CreateWindow(TEXT("button"), TEXT("button1"), WS_CHILD|WS_VISIBLE | WS_POPUP,
10, 10, 80, 25, hWnd, NULL, NULL, NULL);
//create child window2
HWND hWnd2 = CreateWindow(TEXT("button"), TEXT("button2"), WS_CHILD|WS_VISIBLE | WS_POPUP,
100, 100, 80, 25, hWnd, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
ShowWindow(hWnd1, SW_SHOW);
UpdateWindow(hWnd1);
ShowWindow(hWnd2, SW_SHOW);
UpdateWindow(hWnd2);
//wait for buttonpress
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//return the buttonpress
return (int) msg.wParam;
}
int main(void)
{
//create window inside the buttonpress method
int buttonpress = buildwindow();
//check which button was pressed
if(buttonpress = button1){
//do something
}
elseif(buttonpress = button2){
//do something else
}
//finish
return(0);
}
The message loop (GetMessage) won't end until a WM_QUIT message arrives.
You need to implement callback functions for the button click events.
I suggest reading more on button messages here:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775941(v=vs.85).aspx
Related
I need a Windows native window handle for a renderer, but I'm struggling to poll events correctly.
First, I create a window, which works fine on its own:
WNDPROC Window::MakeWindow( LPCWSTR _title, unsigned int _width, unsigned int _height ) {
HINSTANCE hInstance = GetModuleHandle( NULL );
HWND hwnd;
//Step 1: Registering the Window Class
m_WindowClass.cbSize = sizeof(WNDCLASSEX);
m_WindowClass.style = 0;
m_WindowClass.lpfnWndProc = WindowProc;
m_WindowClass.cbClsExtra = 0;
m_WindowClass.cbWndExtra = 0;
m_WindowClass.hInstance = hInstance;
m_WindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
m_WindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
m_WindowClass.lpszMenuName = NULL;
m_WindowClass.lpszClassName = (LPCWSTR)g_szClassName;
m_WindowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&m_WindowClass))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
0, // Optional window styles.
(LPCWSTR)g_szClassName, // Window class
_title, // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT,
_width, _height,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if(hwnd == NULL)
{
MessageBox(NULL, L"Window Creation Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, 1);
UpdateWindow(hwnd);
PollEvents();
return NULL;
}
After creating the window, I want to check for user inputs. In the code snippets I copied, they did it like this:
void PollEvents() {
MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
But, since this blocks my code, I tried using a separate thread to do this. So, at the end of my window creation, I create a thread like so:
m_PollThread = new std::thread(PollEvents);
To test if it's working, I wrote this main() function:
int main() {
// poll thread is created here
Window* window = new Window( "Test Window", 1024, 720 );
while (true) {
Sleep(10);
};
// poll thread is closed/awaited here
delete window;
}
But, the window ends up frozen, so just the while loop is executed while the other thread seems to do nothing.
Only the thread that creates a window can receive messages for that window. You cannot create a window in one thread and then receive messages for that window in another thread.
GetMessage() blocks until it receives a message, so your code freezes because you are calling GetMessage() in a thread that has no window to receive messages for, and the thread that created the window is not processing any messages for that window.
So, if you want to poll events periodically, in the creating thread, without blocking your code, use PeekMessage() instead of GetMessage(), eg:
void PollEvents() {
MSG Msg;
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
Otherwise, you will have to move both the window creation and the event loop into your worker thread.
I am able to add image to the button as background but later I want to add text to the button as "Weclome", I tried all possible ways using Settext, SendmessageA.
please help
#include <Windows.h>
int main()
{
MSG msg;
HWND hWnd = CreateWindow(TEXT("button"), TEXT("START"), WS_VISIBLE | WS_POPUP | WS_CHILD | WS_TABSTOP | BS_BITMAP,
250, 250, 500, 500, NULL, NULL, NULL, NULL);
HANDLE hImg = LoadImageW(NULL, L"Untitled.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
SendMessageW(hWnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImg);
SendMessageA(hWnd, WM_SETTEXT, 0, (LPARAM)"Welcome");
//SendMessageW(hWnd, WM_SETTEXT, (WPARAM) 256,NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
You do realize you have to create a window and then put the button inside the window?
Chances are the program is looking for a file in the wrong directory. Use full path names and do error checking to make sure the bitmap is loaded. Example:
HANDLE hImg = LoadImageW(NULL, L"c:\\fullpath\\Untitled.bmp", IMAGE_BITMAP, 0, 0,
LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (!hImg)
report error...
Don't put ShowWindow and UpdateWindow in the message loop. Just show the window and then call the message loop. Example:
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
I'm trying to get the button press event in c++ win32 using WM_Command
HWND hBtn;
HWND hBtnParent = HWND("UploadVideo");
HWND SelectVideoBTN, UploadBTN;
HWND hWnd;
HINSTANCE hUpload;
WNDCLASSEX wcexUpload;
int nCmdShowUpload = 1;
using namespace std;
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
loader::alert("rrr");
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == WORD(SelectVideoBTN)) {
loader::alert("hello");
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
SelectVideoBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Select Video's", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
UploadBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Upload", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
390, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
I've been looking at this example - http://forums.devshed.com/programming-42/create-button-clicked-148407.html - but I can't quite get it to work, it won't even call the CALLBACK WindowProcedure - is there anyone who could help me?
The buttons are present on the window I've created, I create the window by doing -
WNDCLASSEX vidUploader;
vidUploader.cbSize = sizeof(WNDCLASSEX);
vidUploader.style = CS_HREDRAW | CS_VREDRAW;
vidUploader.lpfnWndProc = WndProc;
vidUploader.cbClsExtra = 0;
vidUploader.cbWndExtra = 0;
vidUploader.hInstance = hUpload;
vidUploader.hIcon = LoadIcon(hUpload, MAKEINTRESOURCE(IDI_P2GOVIDEOUPLOADER20));
vidUploader.hCursor = LoadCursor(NULL, IDC_ARROW);
vidUploader.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
vidUploader.lpszMenuName = MAKEINTRESOURCE(IDC_P2GOVIDEOUPLOADER20);
vidUploader.lpszClassName = (LPCWSTR)(L"UploadVideo");
vidUploader.hIconSm = LoadIcon(wcexUpload.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassEx(&vidUploader);
hInst = hUpload; // Store instance handle in our global variable
and then to create the window
hWnd = CreateWindow((LPCWSTR)(L"UploadVideo"), (LPCWSTR)(L"Upload Video's"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hUpload, NULL);
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShowUpload);
UpdateWindow(hWnd);
Child windows (i.e. windows with the WS_CHILD window style) are identified by a unique numeric value, often called control ID or window ID. It is passed to the parent when it receives a WM_COMMAND message, for example. You never assigned a control ID to your button controls, though, and the parent window cannot identify them. In case of a child window, the hMenu parameter in the call to CreateWindow is overloaded to carry the unique identifier:
hMenu
For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.
In other words, your application picks a numeric value to assign to controls. Since the lower IDs are used by the dialog manager already (e.g. IDOK), it is common practice to start assigning control IDs starting at 100 (see Why do dialog editors start assigning control IDs with 100?).
In your WM_COMMAND handler you can then compare LOWORD(wParam) to the identifier assigned to your button controls.
You need to apply the following changes to your code.
// Declare control IDs. This is usually done in a file called Resource.h
#define IDC_SELECT_VIDEO (100)
Change your window creation code:
SelectVideoBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Select Video's", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
(HMENU)IDC_SELECT_VIDEO, // Assign appropriate control ID
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
Check for the control ID in your WM_COMMAND handler:
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDC_SELECT_VIDEO) {
loader::alert("hello");
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
If your window procedure isn't called at all, this could mean that you aren't dispatching messages on the calling thread. A GUI thread always needs a message loop. The standard message loop suffices:
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
I'm creating a plugin which loads a native window with 2 buttons, on press they should make a message box pop-up but there's no pup-up
Creating a thread for the message loop
//create message thread
class startMessageThreadLoop
{
public:
static DWORD WINAPI StaticThreadStart(void* Param)
{
MessageBox(hWnd, L"StaticThreadStart", L"StaticThreadStart", 0);
startMessageThreadLoop* This = (startMessageThreadLoop*)Param;
return This->ThreadStart();
}
DWORD ThreadStart(void)
{
MessageBox(hWnd, L"ThreadStart", L"ThreadStart", 0);
//create message loop for buttons
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
//translate and send messages
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void startMyThread()
{
MessageBox(hWnd, L"startMyThread", L"startMyThread", 0);
DWORD ThreadID;
CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
char szTest[100];
printf(szTest, "%d", ThreadID);
MessageBox(hWnd, LPCWSTR(szTest), L"ThreadIDBaby", 0);
}
};
Starting the thread
startMessageThreadLoop ThreadLoopInstance;
ThreadLoopInstance.startMyThread();
Creating the window
vidUploader.cbSize = sizeof(WNDCLASSEX);
vidUploader.style = CS_HREDRAW | CS_VREDRAW;
vidUploader.lpfnWndProc = WndProc;
vidUploader.cbClsExtra = 0;
vidUploader.cbWndExtra = 0;
vidUploader.hInstance = hUpload;
vidUploader.hIcon = LoadIcon(hUpload, MAKEINTRESOURCE(IDI_P2GOVIDEOUPLOADER20));
vidUploader.hCursor = LoadCursor(NULL, IDC_ARROW);
vidUploader.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
vidUploader.lpszMenuName = MAKEINTRESOURCE(IDC_P2GOVIDEOUPLOADER20);
vidUploader.lpszClassName = (LPCWSTR)(L"UploadVideo");
vidUploader.hIconSm = LoadIcon(wcexUpload.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassEx(&vidUploader);
hInst = hUpload; // Store instance handle in our global variable
hWnd = CreateWindow((LPCWSTR)(L"UploadVideo"), (LPCWSTR)(L"Upload Video's"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 100, NULL, NULL, hUpload, NULL);
Handle Button
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDC_SELECT_VIDEO) {
MessageBox(hWnd, L"Heeey", L"Hoi", 0);
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
Creating buttons & show Window
SelectVideoBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Select Video's", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
10, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
(HMENU)IDC_SELECT_VIDEO, // Assign appropriate control ID
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
UploadBTN = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed
L"Upload", // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
390, // x position
460, // y position
100, // Button width
25, // Button height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
RECT rect = { 0, 0, uploadWNDWidth, uploadWNDHeight };
AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE);
SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE);
if (!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed!"), _T("Win32 Guided Tour"), NULL);
return 1;
}
MSG msg;
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
//nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShowUpload);
UpdateWindow(hWnd);
I believe that I'm doing everything that I have to in order to get the buttons to work, I have a message loop in a thread, I'm registering & handling the buttons and window, what am I missing?
The message queue of a window is linked to the thread in which the window was created. The message loop must be processed in the same thread.
I'm trying to use the following code to show an empty border-less window, but no windows appear at all. I followed the documentation:
HWND hWnd = CreateWindowEx(WS_EX_TOPMOST,NULL,NULL,WS_POPUP,0,0,1000,1000,NULL,NULL,NULL,NULL);
ShowWindow(hWnd, SW_SHOW);
HWND hWnd = CreateWindowEx(WS_EX_TOPMOST, L"STATIC", NULL, WS_POPUPWINDOW, 0, 0, 100, 100, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOW);