creating a GUI using the native windows API

Hello everyone. I've started a project from scratch and I'm currently trying to create the user interface using the native windows API(Windows.h). Using the Microsoft given documentation which isn't all that helpful. I'm bumping into the issue of "CreateWindow" vs "CreateWindowEx" have no idea what the difference is between these two and in what scenario to use either of these. Right now I am generating a window using the "createWindowEx" function and trying to add a button to this window. Having some experience coding I am trying to add this button through a class which will handle all future buttons as well as other UI elements.

as it stands right now this according to the Microsoft sample I was provided this should be sufficient to create a button on the screen and ready to go. but it is not. The code given below is a snippet from my interface class (the cpp file). This code was originally in the main.cpp (obviously without the variables in place instead using hard values e.g 100 // x position). So basically when and why do I use "createWindow" vs "createWindowEx"? And why does this code snippet trigger the following error: "1 IntelliSense: identifier "L" is undefined"?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void UserInterface::createButton(std::string buttonText, int Xposition, int Yposition, int width, int height, std::string parentWindow)
{
	//create the button.
	HWND hwndButton = CreateWindow(
		L"BUTTON",  // Predefined class; Unicode assumed 
		L buttonText,      // Button text 
		WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
		Xposition,         // x position 
		Yposition,         // y position 
		width,        // Button width
		height,        // Button height
		parentWindow,     // Parent window
		NULL,       // No menu.
		(HINSTANCE)GetWindowLong(parentWindow, GWL_HINSTANCE), //instance handler
		NULL);      // Pointer not needed.
}


Thanks in advance, I really appreciate all input(that is constructive of course :P).
use createWindowEx() if you want the extended functionality, otherwise use the basic function.

See for CreateWindowEx:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680%28v=vs.85%29.aspx

and for CreateWindow:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632679%28v=vs.85%29.aspx


The L is used to mark a string literal as unicode (16 / wchar_t), i.e. a character uses at least two bytes.

you cannot apply L to a std::string since it is not a function that is meant to convert strings.
thanks for the reply. A few follow up questions, Obviously I want to pass the button text through the parameter but how do I accomplish that? removing the L"buttonText" only gives me errors. following up what else do I need to do for this button to show up on screen? the snippet from above is all I have and it's just not showing up and I'm not sure why. I'm using the MSDN given example for creating a window but it's just not working.
for now I'd suggest to change the project setting from the default UNICODE to ANSI. See

http://msdn.microsoft.com/en-us/library/669zx6zc%28v=vs.71%29.aspx

change "Character Set" in
http://msdn.microsoft.com/en-us/library/8x480de8%28v=vs.71%29.aspx
to something not UNICODE

then change:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void UserInterface::createButton(std::string buttonText, int Xposition, int Yposition, int width, int height, std::string parentWindow)
{
	//create the button.
	HWND hwndButton = CreateWindow(
		"BUTTON", 
		buttonText.c_str(),      // Note: .c_str()!
		WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
		Xposition,
		Yposition,
		width,
		height,
		parentWindow, // Note: parentWindow must not be string!
		NULL,
		(HINSTANCE)GetWindowLong(parentWindow, GWL_HINSTANCE), // Note: parentWindow must not be string!
		NULL);
}
coder777 wrote:
for now I'd suggest to change the project setting from the default UNICODE to ANSI.


Do not do this. Instead... you should use the proper string types for whatever function you're calling.

CreateWindow takes TCHAR strings. Not wide chars, and not narrow chars... but TCHARs.

Likewise... putting the 'L' before a string literal makes it wide... but does not necessarily make it a TCHAR so that is not the correct solution either.

If you are not using TCHARs, you should not be using CreateWindow. It's that simple. Changing the project setting as coder777 is suggesting is side-stepping the problem.

Since you are using narrow strings, you should use the narrow string version of the function: CreateWindowA

1
2
3
CreateWindow // <- takes TCHAR strings (LPCTSTR)
CreateWindowA // <- takes char strings (LPCSTR)
CreateWindowW // <- takes wchar_t strings (LPCWSTR) 


Practically all other WinAPI structs/functions operate the same way (MessageBoxA, CreateFileA, etc)

do not mix and match. If you have char strings, call the 'A' functions. Do not call the "normal" version unless you have TCHAR strings.
Topic archived. No new replies allowed.