Reputation: 11045
I have this problem that drives me mad, so I am here to ask for your help. I have this code that is supposed to create a simple window and show it:
void ShowMainWindow() {
WNDCLASSEX main_window_class; // New window class for the splash window //
main_window_class.cbSize = sizeof(WNDCLASSEX); // Set size of the splash window class //
main_window_class.style = CS_PARENTDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; // Main window class style //
main_window_class.lpfnWndProc = MainProc; // Pointer to the main window procedure //
main_window_class.cbClsExtra = 0; // No extra bytes after class structure //
main_window_class.cbWndExtra = 0; // No extra bytes after window's instance //
main_window_class.hInstance = Instance; // Set instance of the window //
main_window_class.hIcon = LoadIcon(Instance, MAKEINTRESOURCE(MICON)); // Executable's icon //
main_window_class.hCursor = LoadCursor(NULL, IDC_ARROW); // Main window's default cursor //
main_window_class.hbrBackground = HBRUSH(COLOR_WINDOW + 1); // Main window's default background //
main_window_class.lpszClassName = L"MyAppClass"; // Main window's class name //
main_window_class.hIconSm = LoadIcon(Instance, MAKEINTRESOURCE(SICON)); // Application's small icon //
if (!RegisterClassEx(&main_window_class)) { // If the class was not registered //
MessageBox(NULL, L"RegisterClassEx", L"Error", MB_OK|MB_ICONERROR);
}
MainWindow = CreateWindowEx ( // Create the main window //
WS_EX_APPWINDOW, // Extended style to support transparency //
main_window_class.lpszClassName, // Assign the anterior class name //
(WCHAR*)"App Title", // Main window's title //
WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, // No border window //
CW_USEDEFAULT, // Default left position for the moment //
CW_USEDEFAULT, // Default top position for the moment //
600, // Main window width //
400, // Main window height //
NULL, // No parent //
NULL, // No ID //
Instance, // Assign to main instance //
NULL // No additional data needed //
);
if (!MainWindow) { // If the window was not created //
MessageBox(NULL, L"CreateWindowEx", L"Error", MB_OK|MB_ICONERROR);
}
long Style = GetWindowLong(MainWindow, GWL_STYLE);
Style &= ~WS_MAXIMIZEBOX;
SetWindowLong(MainWindow, GWL_STYLE, Style);
ShowWindow(MainWindow, SW_SHOWNORMAL); // Display main window at normal size //
UpdateWindow(MainWindow); // Update the window's client area //}
My problem is that, when the window is opened, the title of the window is not "App Title" but some weird characters plus "CreateWindowEx". It is so weird. It's like it assigns that text from the MessageBox function to the title of the window. I must specifiy that I use UNICODE encoding. Anyway, it never happened to me before and I just don't know what could be wrong. Thanks!
Upvotes: 3
Views: 753
Reputation: 126787
(WCHAR*)"App Title"
is wrong. "App Title"
is an ANSI string, while your project is set up to use Unicode by default for Windows API, so CreateWindowEx
expects a WCHAR *
, i.e. a Unicode string.
Because of this, the correct parameter to pass is L"App Title"
; the L
prefix is needed to make the compiler provide a wide (~Unicode) string literal instead of a "regular" string literal.
I suppose that you tried with a "regular" string literal, the compiler told you that it couldn't convert it to const WCHAR_T *
and you just stick a cast in front of it. This is the wrong way to solve this kind of problems; if you get the compiler to complain about a pointer type, almost always you are wrong, and just casting the pointer to make the compiler shut up won't fix the problem. Blindly casting pointers removes the "safety net" of the type system, and allows you to pass wrong data to functions.
In this particular case, the bytes of your ANSI string (along with other data found on the stack) were interpreted as Unicode, which resulted in garbage in the title bar.
Again: think twice before casting.
Upvotes: 6
Reputation: 614
You can't cast a const char*
to a WCHAR*
.
You need to replace (WCHAR*)"App Title"
with L"App Title"
.
Upvotes: 3