Win32 code anomaly - c++

I am facing a very odd problem. Can any one tell me what is wrong with the following code-:
#include <Windows.h>
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
char szWinName[]="MyWin";
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
LPSTR lpszArgs, int nWinMode)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(WNDCLASSEX);
wndclass.hInstance=hThisInst;
wndclass.lpszClassName=szWinName;
wndclass.lpfnWndProc=WindowFunc;
wndclass.style=0;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION)
wndclass.hIconSm=NULL;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.lpszMenuName=NULL;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH) GetStockObject(LTGRAY_BRUSH);
if(!RegisterClassEx(&wndclass)) return 0;
hwnd=CreateWindow(
szWinName,
"Hello World",
WS_OVERLAPPED,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
500,
NULL,
NULL,
hThisInst,
NULL
);
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wparam,
LPARAM lparam)
{
switch(message){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wparam,lparam);
}
return 0;
}
I am getting the following window-:
As you can see there is no system menu. I do not know why this is happening. But if I replace the above code with the following code it seems to work just fine-:
#include<windows.h>
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
char szWinName[]="Main Window";
int WINAPI WinMain(HINSTANCE thisInst,HINSTANCE prevInst,
LPSTR lpCmdArgs, int nMode){
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(WNDCLASSEX);
wndclass.hInstance=thisInst;
wndclass.lpszClassName=szWinName;
wndclass.lpfnWndProc=WinProc;
wndclass.style=0;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION)
wndclass.hIconSm=NULL;
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.lpszMenuName=NULL;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
if(!RegisterClassEx(&wndclass)) return 0;
hwnd=CreateWindow( szWinName,
"Hello World",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
500,
NULL,
NULL,
thisInst,
NULL
);
ShowWindow(hwnd,nMode);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT message,
WPARAM wparam, LPARAM lparam){
switch(message){
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wparam, lparam);
}
return 0;
}
Please can someone tell me what I doing wrong in the first code segment I have tried everything and not been able to find what is wrong with it. I am using a normal Win32 Project in Visual Studio 2008 Professional Edition. If anyone wants I can mail the project to them to test it out for themselves. A quick reply would be appreciated. Thank You.

In the bottom code segment you use WS_OVERLAPPEDWINDOW as a window style, which is what gives you the system menu. The first code segment only has WS_OVERLAPPED, which only gives you the title bar and border.

Related

open window program in visual c++

I just started to learn windows app development in c++ and I found some program code on Microsoft docs that all it should do is open simple window. I tried to build it on visual studio and it was built but it didn't run. And when I tried to compile and run it with the local debugger I got some error that I have no idea what is it about. I'd like to get some help with it.
Here is the code and the error message.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

MessageBox doesn't appear

I code in codeblocks.
Compiler = gcc version 4.9.2 (tdm-1)
I call MessageBox in WM_COMMAND.
When I press the button, The main window stop, But the MessageBox does not show.
I'm sure the WindowProc receive the button event!
But if I create a thread, and call MessageBox in the thread function, the MessageBox will show when I press the button. But if I call MessageBox in thread, Then then MessageBox can`t stop the main window.
How I can solve this problem?
#define IDI_BUTTON_1 10001
#define IDI_BUTTON_2 10002
#ifndef UNICODE
#define UNICODE
#endif
#include "resource.h"
#include "main.h"
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"mywindow";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
//wc.style = CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindow( CLASS_NAME,
L"Mywindow",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
250,
200,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int i;
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CREATE:
{
HWND hwndButton1 = CreateWindow( L"button" ,L"button1",
WS_CHILD|WS_VISIBLE,
10,
10,
75,
50,
hwnd,
(HMENU)IDI_BUTTON_1,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
HWND hwndButton2 = CreateWindow( L"button" ,L"button2",
WS_CHILD|WS_VISIBLE,
120,
10,
75,
50,
hwnd,
(HMENU)IDI_BUTTON_2,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
return 0;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDI_BUTTON_1:
MessageBoxW( hwnd, L"button1", L"button1", MB_ICONERROR|MB_DEFAULT_DESKTOP_ONLY );
break;
case IDI_BUTTON_2:
MessageBoxW( hwnd, L"button2", L"button2", MB_ICONERROR|MB_DEFAULT_DESKTOP_ONLY );
break;
}
return 0;
}
case WM_PAINT:
{
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

Windows Programming, window not opening, no build errors

I don't have much experience with windows programming, but can't specifically see what's wrong with this code, yet the window doesn't open. Although sometimes, not always, it will be open in task manager, so my guess is that it is registering the class and creating the window, but the problem is with the ShowWindow() function. But, I'm not positive.
To my understanding the flow of the program is:
Window is created with the registered class.
The window is shown.
Continuously looks for messages that are processed in the window Proc.
I feel like I've done all these things, so is my understanding wrong, or is my code missing something?
Thanks.
Source Code:
#include <Windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
//Variable for message loop.
MSG msg;
//Setting up the window class.
WNDCLASSEX windowClass;
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_PARENTDC;
windowClass.lpfnWndProc = WindowProc;
windowClass.hInstance = hinstance;
windowClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
windowClass.lpszClassName = "WindowClass";
RegisterClassEx(&windowClass);
HWND windowHandle = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "WindowClass", "My Program", WS_OVERLAPPEDWINDOW, 500, 200, 800, 500, NULL, NULL, hinstance, 0);
if (!windowHandle)
return FALSE;
ShowWindow(windowHandle, nCmdShow);
// Start the message loop.
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Return the exit code to the system.
return msg.wParam;
}
Your window procedure is invoking DefWindowProc but not actually returning the result, and you have undefined behavior because of that. The return value is important, and it can control how the OS handles successive messages to your window. For example, it's important to return the correct value in response to the WM_CREATE message.
Change your window procedure to:
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Also, as Mark Ransom identified in the comments section, you should zero-initialize your WNDCLASSEX structure to ensure that you don't get garbage on any members that you didn't explicitly initialize.

Error creating a window in windows "undefined reference to 'WinMain#16'"

#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"Learn to Program Windows",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
This is the code, which is also the standard example you can find in the microsoft website that teach people how to program windows.
http://msdn.microsoft.com/en-us/library/windows/desktop/ff381409(v=vs.85).aspx
The problem I receive is the following whenever I try to compile this code in codeblocks.
undefined reference to 'WinMain#16'
What is that and what can I do to compile and run this piece of code?
It seems strange to me that WinMain is called wWinMain: evidently you need a function called WinMain.
Oh, wait, you are using codeblocks? Probably wWinMain is specific of visual studio. Codeblocks wants the standard WinMain.

Win32 API window won't open

okay, so I have taken out the time to learn a but of the Win32 API to do with opening windows, and the code I came up with in the end I would think would work, but doesn't. I registered the window class, made all the things I have to, but when I run it, nothing happens... It would be a great help if someone could point out what I am doing wrong/missing.
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#pragma comment (lib, "wsock32.lib")
#define WNDCLASSNAME "wndclass"
bool quit = false;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
{
WNDCLASSEX WCE;
WCE.cbSize = sizeof(WNDCLASSEX);
WCE.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
WCE.lpfnWndProc = WndProc;
WCE.cbClsExtra = 0;
WCE.cbWndExtra = 0;
WCE.hInstance = hinstance;
WCE.hIcon = NULL;//LoadImage()
WCE.hCursor = NULL;//LoadCursor(NULL, IDC_CROSS);
WCE.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
WCE.lpszMenuName = NULL;
WCE.lpszClassName = "KyleWindow";
WCE.hIconSm = NULL;
RegisterClassEx(&WCE);
HWND WindowHandle;
WindowHandle = CreateWindowEx(WS_OVERLAPPEDWINDOW, "KyleWindow", "Xerus", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hinstance, NULL);
ShowWindow(WindowHandle, SW_SHOWNORMAL);
UpdateWindow(WindowHandle);
std::cout<<"'Opened' Window"<<std::endl;
MSG msg;
while(!quit)
{
if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
if(msg.message == WM_QUIT)
quit = true;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.lParam;
}
Use WS_EX_OVERLAPPEDWINDOW as the first parameter of your CreateWindowEx function (instead of WS_OVERLAPPEDWINDOW, which is not a valid extended window style).
instead of using WNDCLASSEX use WNDCLASS
change:
WNDCLASSEX WCE; to WNDCLASS WCE;
remove line:
WCE.cbSize = sizeof(WNDCLASSEX);
change:
RegisterClassEx(&WCE); to RegisterClass(&WCE);
The function int WINAPI WinMain must be before function LRESULT CALLBACK WndProc. Compilers read in order.