I tried to create a window in an Empty C++ Project in Visual Studio, but when I run it, it shows me no window. However, it doesn't give me any error too.
#include <Windows.h>
using namespace std;
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
const auto pClassName = "TextClass";
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = pClassName;
wc.hIconSm = nullptr;
RegisterClassEx(&wc);
HWND hWnd = CreateWindowEx(
0,
pClassName,
"A sad Window",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
200, 200, 640, 480,
nullptr, nullptr, hInstance, nullptr);
ShowWindow(hWnd, SW_SHOW);
while (true);
return 0;
}
You have a typo: you are setting
wc.lpszMenuName = pClassName;
instead of wc.lpszClassName.
I believe just fixing that will get the window on the screen but the executable will then hang with it on the screen because nothing is fielding messages.
A minimal message loop instead of
while (true) ...
would be
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
DispatchMessage( &msg );
Related
So I have beginners knowledge of C++ (4 months daily study, so maybe slightly more than beginner but definitely very new) and I have started looking at some win API stuff and have a few blanks in my head.
I am sure there are many things wrong with the below, or better ways to do it, and that will come in time but I'd like to know why when I step through this code does it for instance loop a few times through the first WndProc definition before passing the control elsewhere, and seemingly cease to ever come into contact with other parts of the program.
Is windows looping through the code to check for events at high speed then moving control else where?
I am sure with your knowledge you may want to even correct my question, so all information that you find related to what I think I'm asking here is appreciated.
Forgive the comments, they're to future self ;)
#include <Windows.h>
#include <string>
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(69);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
const auto CLASSNAME = "hwWindow";
//register window class
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = nullptr;
wc.hIcon = nullptr;
wc.hCursor =nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = CLASSNAME;
wc.hIconSm = nullptr;
RegisterClassEx(&wc);
//create window instance
HWND RyanWin = CreateWindowEx(
0,
CLASSNAME,
"Hacker",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
1200, 1200, 640, 480,
nullptr,
nullptr,
hInstance,
nullptr);
HWND hWnd = CreateWindowEx(
0,
CLASSNAME,
"Happy HW Windows",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
200, 200, 640, 480,
nullptr,
nullptr,
hInstance,
nullptr);
ShowWindow(hWnd, SW_SHOW);
ShowWindow(RyanWin , SW_SHOW);
MSG msg;
BOOL gResult;
while ((gResult = GetMessage(&msg, nullptr, 0, 0)) > 0)
{
TranslateMessage(&msg);
UINT m = msg.message;
DispatchMessage(&msg);
}
if (gResult == -1) { return -1; }
else { return msg.wParam; }
return 0;
}
I have a problem with setting the titlebar icon for my application.
I have been trying to figure this out, Googling what is wrong, for 2 days already, without any success.
MainWindow.cpp:
#include "../../res/Icons.h"
void MainWindow::Create(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"MainWindow";
WNDCLASSEX wc = {};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hIcon = LoadIcon(NULL,IDI_MYICON);
wc.hIconSm = LoadIcon(NULL,IDI_MYICON);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(0, CLASS_NAME, wstring(Language::wText[1].begin(),Language::wText[1].end()).c_str(), WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Icons.h
#define IDI_MYICON 1000
Icons.rc
IDI_MYICON ICON "App.ico"
It compiles fine, and the icon is shown in the Taskbar, as well as in the executable file, but not in the titlebar. The icon is a standard ico with dimensions of 32x32. I have even tried using LoadImage(), but same result.
You are passing NULL to LoadIcon, you should pass the HINSTANCE of your application to load from your own resources.
The resource header file needs to be used in conjunction with the .rc file (i.e. #include "icon. h" in icon. rc), otherwise the specific icon file (path is specified in the .rc file) will not be found. In addition, if "icon.h" does not end with an empty line, you will get an unexpected end of file found error.
I am writing a C/C++ windows app which calls CreateWindow as follows:
HWND hWnd = CreateWindow(pszClassName, title_.c_str(), WS_OVERLAPPED | WS_VISIBLE | WS_POPUP | WS_SIZEBOX, 50, 50, 400, 100, NULL, NULL, hInst, this);
Nothing particularly complex about it, however, it is appearing on every desktop on my computer when I switch from one desktop to another with CTRL-WIN < or >.
How do you make window you create in an app remain only on the desktop that you created it on?
I am compiling this app in 64bit and running it on Windows 10.
Code:
Global:
WNDCLASSEX wc;
Window procedure:
case WM_KEYDOWN:
if(wParam == VK_F11)
{
wc.style = HWND_DESKTOP;//The window is child
//of desktop
}
break;
WinMain:
LPCSTR windowName = "Answer on StackOwerflow";
MSG messages;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "The classname";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
wc.lpszClassName,
windowName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd);
UpdateWindow(hWnd);
while(GetMessage(&messages, NULL, 0, 0) > 0)
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return 0;
I created in C++ a window with the Win API.
If I set the target platform to x86 in Visual Studio (2015) all works fine.
But if I set the target platform to x64 all works fine too except that I get a busy cursor for the first 4-5 seconds when the window popped up.
This isn't that bad but I am just wondering what is causing this.
I saw this behavior when using GLFW too so it seems to be not a programming error.
Maybe someone know what is causing this?
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName = NULL;
wc.lpszClassName = "MainWindowClass";
wc.hIconSm = NULL;
RegisterClassEx(&wc);
RECT wr = { 0, 0, 800, 800 };
AdjustWindowRectEx(&wr, WS_OVERLAPPEDWINDOW, FALSE, 0);
hWnd = CreateWindowEx(0, "MainWindowClass", "DirectX Lab", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, wr.right - wr.left, wr.bottom - wr.top, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
I was working through a book which shows me how to create a windows application. I have written the code, however when I compile and run it, it says that it was successfuly built but it doesn't show a window where it should write "Hello World". I am using Visual Studio 2010 with C++, what might be the problem?
Thanks
Here is the code;
//Header Files
#include <windows.h>
#include <stdlib.h>
#include <time.h>
//Application Title
#define APPTITLE L"Hello World"
//function prototypes (forward declarations)
BOOL InitInstance( HINSTANCE, int);
ATOM MyRegisterClass( HINSTANCE);
LRESULT CALLBACK WinProc( HWND, UINT, WPARAM, LPARAM);
//The window event callback function
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
//char *szHello = "Hello World!";
RECT rt;
int x, y, n;
COLORREF c;
switch(message)
{
case WM_PAINT:
//get the dimensions of the window
GetClientRect( hWnd, &rt);
//Start drawing on device context
hdc = BeginPaint( hWnd, &ps);
//Draw some text
DrawText( hdc, L"Hello World!", strlen( "Hello World!"), &rt, DT_CENTER);
//Draw 1000 random pixels
for( n=0; n < 3000; n++)
{
x = rand() % (rt.right - rt.left);
y = rand() % (rt.bottom - rt.top);
c = RGB( rand()%256, rand()%256, rand()%256);
SetPixel( hdc, x, y, c);
}
//Stop drawing
EndPaint( hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//create the window class structure
WNDCLASSEX wc;
wc.cbSize = sizeof( WNDCLASSEX);
//FILL THE STRUCT WITH INGO
wc.cbSize = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = APPTITLE;
wc.hIconSm = NULL;
//set up the window with the class info
return RegisterClassEx(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
//create a new window
hWnd = CreateWindow(
APPTITLE, //Window class
APPTITLE, //title bar
WS_OVERLAPPEDWINDOW, //window Style
CW_USEDEFAULT, //x position of window
CW_USEDEFAULT, //y postion of window
500, //width of the window
400, //height of the window
NULL, //parent window
NULL, //menu
hInstance, //application instance
NULL); //window parameters
//was there an error creating the window?
if( !hWnd)
return FALSE;
//Display the window
ShowWindow( hWnd, nCmdShow);
UpdateWindow( hWnd);
return TRUE;
}
//Entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//declare varuables
MSG msg;
//register the class
MyRegisterClass( hInstance);
//Initialize application
if( !InitInstance(hInstance, nCmdShow))
return FALSE;
//set random number seed
srand(time(NULL));
//Main message loop
while( GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage( &msg);
DispatchMessage( &msg);
}
return msg.wParam;
}
This line is wrong:
wc.cbSize = CS_HREDRAW | CS_VREDRAW;
You mean
wc.style = CS_HREDRAW | CS_VREDRAW;
In fact I would change the window class initialisation code so that you make it very clear in the code that the entire struct is initialised.
WNDCLASSEX wc = { 0 };//initialise struct to 0
wc.cbSize = sizeof( WNDCLASSEX);
//FILL THE STRUCT WITH INGO
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = APPTITLE;
And you were missing some error checking:
if (!MyRegisterClass( hInstance))
return FALSE;
Stepping through under the debugger will allow you to see where in the process things are going wrong.