C++ Declare Combo Box Identifier - c++

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.

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.

Explorer-type custom dialog is maximized on the first open in the application

I have customized an explorer type browse dialog and added some controls. Upon opening the dialog, even though I have specified the width and height on the template resource, it is showing in maximized state.
I have tried to override this behavior by using the SetWindowPos, MoveWindow and ShowWindow API but none of them seems to resize the dialog.
Here are the specific codes, called during dialog initialization:
HWND hTrueDlg = GetParent(hwndDlg);
::SetWindowPos(hTrueDlg, HWND_TOP, iXPx, iYPx, iWinWidthPx, iWinHeightPx, SWP_SHOWWINDOW);
::ShowWindow(hTrueDlg, SW_RESTORE);
This is the resource file:
WPOPENDLGCUSTOM DIALOG 0, 0, 600, 40
STYLE DS_3DLOOK | DS_CONTROL | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION
// I have also tried this
STYLE (DS_3DLOOK | DS_CONTROL | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CAPTION) & ~WS_MAXIMIZE
When I double click the title bar, it is "Restored" and for the next time I open the dialog, it is displaying the correct size. The problem is only on the first open. When a user don't know this work around, it might cause some problems.
The explorer-style dialog box read size value from one registry hive first opening and record and read size value to one registry hive that could be Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\CIDSizeMRU later.

C++ CreateWindow() function not defined

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.

Changing a dialog's style at creation to override what's in the .RC template

I have a little dialog I created using Resource Editor in VS2005. It's going to be used as a child dialog of another dialog, in 2 different ways... either as a child which sits on top of the dialog like a custom control, or as a popup which can move beyond the boundaries of the parent window. If I modify a single style in the RC file, both of these work as expected - simply substitute WS_POPUP for WS_CHILD to toggle:
IDD_WIDGET DIALOGEX 0, 0, 221, 78
STYLE DS_SETFONT | DS_3DLOOK | DS_FIXEDSYS | DS_CONTROL | WS_POPUP | WS_BORDER
EXSTYLE WS_EX_TOOLWINDOW | WS_EX_STATICEDGE
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
PUSHBUTTON "Pre&view",IDC_ACTION_PREVIEW,64,59,50,15
PUSHBUTTON "M&ore",IDC_ACTION_MORE,115,59,50,15
PUSHBUTTON "S&elect",IDC_ACTION_SELECT,168,59,50,15
END
The problem is, I want to be able to choose the behavior when creating the child dialog at run-time as a sort of widget type framework, e.g overriding the RC file style
I tried:
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_Widget.Create(IDD_WIDGET,this);
DWORD oldstyle = m_Widget.GetStyle();
m_Widget.ModifyStyle(WS_POPUP,WS_CHILD);
DWORD newstyle = m_Widget.GetStyle();
}
But it makes no difference that I can see, other than that the result of GetStyle changes from 0x8400044C to 0x4400044C... the widget child-dialog starts invisible but when I show it, it retains the WS_POPUP behavior.
What's wrong, and how can it be fixed?
As Ben suggests, I wonder if this is not a style that can be changed after the window is created, but the problem then is how to intercept the dialog-template structure and modify the style before it's used to create the window?
I found this article for you: link text
Looks like InitDialog is too late to change the style. There's an example of how to do it in this link.

How to recreate style used by resource editor when creating MFC controls dynamically?

I need to create some controls in a MFC dialog dynamically. The creation works fine so far, but the controls created dynamically are looking different from controls created with the resource editor. Some controls even behave different. I think, that I'm missing some initializations the generated code does.
Currently I only create CStatic and CEdit controls. Both don't use the standard windows font when I create them dynamically (the font looks more like the default font used prior to Windows 95, if I remember correctly).
Also, the CEdit control behaves different from when I create it with the resource editor. The dynamically created control seems to limit the text length to the visible size. I can set a longer text with SetWindowText() and read the full text back in with GetWindowText(), but the user can't enter a text longer than the size displayed. The CEdit control created by the resource editor behaves different: If the user enters a text longer than what can be displayed, the entered text ist "scrolled" inside the control (no scrollbars, as its only a single line control).
I tried fixing that problem by calling SetLimitText() on the control, but that didn't change the behavior.
The controls are saved to arrays defined in the dialog-class:
CStatic** m_pLabels;
CEdit** m_pEdits;
The creation of the controls happens in the OnInitDialog() method of the dialog-class:
for (int i = 0; i < max; i++)
{
m_pLabels[i] = new CStatic();
m_pLabels[i]->Create("key", WS_CHILD | WS_VISIBLE | SS_RIGHT,
CRect(10, 10 + i * 30, 130, 35 + i * 30), this);
m_pEdits[i] = new CEdit();
m_pEdits[i]->CreateEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_BORDER,
CRect(133, 10 + i * 30, 350, 35 + i * 30), this, i + 100);
m_pEdits[i]->SetLimitText(499);
m_pEdits[i]->SetWindowText("value to be edited");
}
Thanks for your help!
Dynamically created controls always get the stock font initially: the usual approach is to just set the control's font to the parent dialog's font: something like
pEdits[i]->SetFont(GetFont());
I think the best way to do is to put a control through dialog editor on a dialog, set it's visual styles to the ones of your choice and then open the .rc file in a text editor and copy the styles of that control from there. This way you will be able to create controls that are far more closer to the ones you add through dialog editor.
e.g., after putting a simple button on a dialog having OK/Cancel buttons and a text control, my dialog looks like this in the .rc file:
IDD_MFCAPP_DIALOG DIALOGEX 0, 0, 320, 200
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_APPWINDOW
CAPTION "MFCApp"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,263,7,50,16
PUSHBUTTON "Cancel",IDCANCEL,263,25,50,16
CTEXT "TODO: Place dialog controls here.",IDC_STATIC,10,96,300,8
PUSHBUTTON "Button1",IDC_BUTTON1,43,17,50,14
END
Now, since I have all the information about how the dialog was created at the back-end, I can simply replicate this behviour through code.
P.S. Off course, you would do this in a separate test project.
You need to add the ES_AUTOHSCROLL style to the editbox. This style is responsible for scrolling the text leftwards when you enter more text than the box can display.
Opening .rc files in text editor and looking up the styles of controls can help you find out such points.