C++ CreateWindow() function not defined - c++

I started playing around with the win32 api in c++ and as i was about to make a button, the intellisense says "define CreateWindow CreateWindowA". When i seem to hover down, it also says "Error: expected a )'".
Here is my code:
CreateWindow(TEXT("button"), TEXT("You should press this button"), WS_VISIBLE | WS_CHILD, 50, 50, 300, 300, hWnd, (HMENU)somecrap, NULL, NULL);
So could you tell me what is exactly the problem? From searching the internet, people were saying it has something to do with the character set with the preprocessor, but i'm not exactly sure what to do .

The name CreateWindow is a macro. The real Win32 API functions are 2: CreateWindowA for ANSI and CreateWindowW for Unicode. Depending on UNICODE definition is used one of them.

Related

Win32 GUI accent not displayed even in UNICODE

I'm making a GUI using window API with MSVC 2022,
When I try to display é char in my widget, there is a problem with encoding
That is the code for the widget:
I tried with wide string and the TEXT macro
CreateWindowW(
L"STATIC", // Predefined class; Unicode assumed
L"éééé", // text
WS_VISIBLE | WS_CHILD, // Styles
10, // x position
10, // y position
250, // Button width
50, // Button height
hWnd, // Parent window
NULL, // No menu.
hInst,
NULL); // Pointer not needed.
CreateWindowW(
L"BUTTON", // Predefined class; Unicode assumed
TEXT("réééé"), // Button text
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles
300, // x position
10, // y position
250, // Button width
50, // Button height
hWnd, // Parent window
(HMENU)ID_OK, // No menu.
hInst,
NULL); // Pointer not needed.
My MSVC use UNICODE for the file (I checked in parameters) and defines UNICODE and _UNICODE.
Did I miss something ?
Thank you for your time
Thank to John is the comment, I solved the problem, the cpp file was saved in utf-8 instead of utf-16. This can be changed with the 'Save as' menu.
Did I miss something ?
Yes, the charset used in MVSC doesn't need to be the same than the one used in the library you are using, or the one selected at runtime. Check your library documentation, to support UNICODE, as it seems to be showing your unicode sequences as iso-latin-1 character pairs.
IMHO Microsoft has taken long to adapt to unicode. Despite MVSC already supports it, it is very probable that the Windows environment/library you use is not supporting it (or configured to) by default. The comment you use in the CreateWindowW call is not enough to make it to assume UNICODE characters. You'll probably need to specify it somewhere else.

How to update the current state for window after showing/hiding controls?

I'm a beginner and today is my first day in learning to create Windows application.
I have two buttons.
#define BUTTON_SW 1
#define BUTTON_SW2 2
HWND Button1;
HWND Button2;
Button1 = CreateWindow("button", "Enter", WS_VISIBLE | WS_CHILD, 215, 10, 40, 25, hwnd, HMENU(BUTTON_SW), NULL, NULL);
Button2 = CreateWindow("button", "You'll be gone", WS_VISIBLE | WS_CHILD, 260, 10, 95, 25, hwnd, HMENU(BUTTON_SW2), NULL, NULL);
When Button1 is clicked, how can I hide Button2 or make it lose its WS_VISIBLE flag and reflect current situation correctly, like this?
LONG style = GetWindowLong(Button2,GWL_STYLE);
style = style | WS_VISIBLE; // style = style & ~WS_VISIBLE
SetWindowLong(Button2,GWL_STYLE,style);
This works very well. However, once WS_VISIBLE flag is assigned, button still stays invisible until the first mouseclick on it.
Vice versa, when I use style = style & ~WS_VISIBLE; once WS_VISIBLE flag is removed, button becomes passive (unclickable) but stays visible.
How to fix this? Tried many things that I've found online but couldn't fix it. Also please don't suggest me to buy a decent book, I don't have the money for now.
(P.S: ShowWindow function with SW_HIDE/SW_SHOW somehow doesn't work for me, perhaps I'm using it wrong. Can you help me on how I can hide this Button2 correctly? I'm trying the following command, but nothing happens.)
ShowWindow(GetDlgItem(Button2, 2), SW_HIDE);
#Edit: I've noticed that when I minimize the app and maximize it
again, the state of button updates. But how will it automatically
update the state?
This should work
ShowWindow(Button2, SW_HIDE);
or
ShowWindow(GetDlgItem(DialogHWND, BUTTON_SW2), SW_HIDE);
GetDlgItem needs HWND of the parent window (dialog) as first argument.
In order to a window to reflect changes you must ask the OS to do it.
Learn about RedrawWindow and the invalidated area.
Note that some actions, like resizing or restoring from minimized, automatically makes the OS to invalidate the region and redraw it.
Use:
RedrawWindow(Button2,NULL,NULL,RDW_INVALIDATE | RDW_INTERNALPAINT);

Visual style set on BS_CHECKBOX button overrides NM_CUSTOMDRAW

I am looking to create a checkbox in the C++ WinAPI with a custom bitmap resource as the actual check box.
Unfortunately, when I create a WM_NOTIFY-compatible checkbox (That actually deals with NM_CUSTOMDRAW in a meaningful way), it seems to work just fine except for one thing: The checkbox seems to fade when changing states. This has to do with the visual style, it seems, so I simply unset the visual style with SetWindowTheme(chkLogging, _T(" "), _T(" "));.
HWND chkLogging = CreateWindowEx(
NULL,
_T("button"),
_T("Export to log file"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX | BS_NOTIFY,
300, 100, 100, 20,
hWnd, (HMENU)IDC_EXPORTLOG, NULL, NULL);
CheckDlgButton(hWnd, IDC_EXPORTLOG, BST_CHECKED);
SetWindowTheme(chkLogging, _T(" "), _T(" "));
Unfortunately, setting the theme for that HWND will override the NM_CUSTOMDRAW rendering. I have tested this by removing the SetWindowTheme line and trying with the default theme, and the checkbox NM_CUSTOMDRAW worked like a charm.
This typically wouldn't be an issue, but it actually works as seemingly intended for normal buttons.
Any assistance on this matter would be greatly appreciated :)

C++ Declare Combo Box Identifier

just wondering how to declare a combobox identifier. I've got two comboboxes on my page so I need two identifiers to distinguish them. I enter the following code but Visual Studio 15.5 tells me that IDC_COMBOBOX_LAYER is an undeclared identifier. It's supposed to be an integer but a number like 100 won't work either.
HWND hWndComboBox = CreateWindow(WC_COMBOBOX, TEXT(""),
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
xpos, ypos, nwidth, nheight, parWnd, IDC_COMBOBOX_LAYER, NULL,
NULL);
I've searched Google but I'm resigned to asking you here. How do I declare the identifier?
If this is not being used in a dialog resource I would suggest that you just assign a value and use it:
enum CHILD_IDs : UINT { IDC_COMBOBOX = 1 };
If it is being used in a dialog (but not part of the template for some reason) I suggest you define it through the resource system, go to the resource explorer (ctrl+shift+e) right click on the resource file and select "resource symbols", you then have the ooption of creating a new symbol value.

Creating a multi-lined textbox/textfield in C++, not VC++

I was wondering how to create a text box that can respond to the "enter" key and create a new line just like the field I am typing in now.
CreateWindow ("edit", 0, WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 130, 20, hwnd, (HMENU) 1000, GetModuleHandle (NULL), NULL);
That is what I have currently, but when I create it, it is a tiny strip and when I press the "Enter" key, it does not jump down to the next line like a paragraph. Any ideas? I am using C++ not VC++ or any .net framework and I am on Windows 7. Thank you.
You need to use the "ES_MULTILINE" flag. See Edit Control Styles