DrawText(), DT_LEFT and colons moving to beginning of text? - mfc

on some PCs I am finding that my calls to DrawText() result in colons being placed at the start of the text.
dc.DrawText( "Name:", &rText, DT_LEFT | DT_VCENTER | DT_WORDBREAK );
The output on my PC is
Name:
But on one PC in particular it is:
:Name
If I change DT_LEFT to DT_CENTER the colon appears at the end where it should. Any ideas, please?

OK, the line of code above wasn't entirely accurate. I was requesting the style of the CStatic control that I'd derived which had the DT_MODIFY flag set. On that one particular machine that resulted in the colon being moved.. So, to fix this issue I masked them flags with 0x0000FFFF and all is working fine.
Cheers
Sparky

Related

Character encoding not consistent (Win32 API)

I am developing a Win32 desktop application with C++ where the target-language is Swedish. As a result of that I rely on Unicode to properly format and display any given string. However, I encountered a very peculiar issue where strings consisting of non-english characters (such as å, ä, ö) work in one part of the code, but breaks in another part of the code.
More specifically, the code works in one file, but not the other. If I move the code-block to the other file, then the text is properly displayed again. See the following example:
CreateWindowW(L"Button", L"Lägg till kund", WS_TABSTOP | WS_VISIBLE | WS_CHILD,
0, 0, 80, 25, windowHandle, (HMENU)0, NULL, NULL);
-Which is called from the Application.cpp file.
AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_ArkivMenu, L"&Arkiv");
AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_VisaMenu, L"&Visa");
AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_ArkivMenu, L"&Hjälp");
AppendMenu(m_MainMenuBar, MF_POPUP, (UINT_PTR)m_ArkivMenu, L"&Sök");
-Which is called from the Window.cpp file.
All of this results in the following:
Note that the code called from Window.cpp generates text with invalid characters, which indicates that the symbol(s) couldn't be found. This is very strange since the problem ceases to exist if I move the code from Window.cpp to Application.cpp.
Thus, the only logical conclusion is that the character encoding must differ between these two files, but why?
The problem is indeed, as the title states, "Character encoding not consistent".
And to be precise, that's the character encoding of Window.cpp compared to the character encoding of Application.cpp. I suspect the environment is Visual Studio, which can handle files in multiple encodings. But for source code that contains Unicode, you probably want to use UTF-8 with "signature" (BOM).
This is available in Visual Studio 2017+ via "Save As", and in that save dialog choose "Save With Encoding" instead of plain "Save".

Why CSpinButtonCtrl is not handling correctly numbers higher than 1000?

I have a dialog in MFC with a CSpinButtonCtrl and an attached buddy (CEdit). They work correctly when the maximum value of the spin control is lower than 1000, but if it is higher, the value in the CEdit is clamped to the thousand units when the the value is 1000 or higher (it is clamped to 4 instead of 4345, for example).
BEGIN
EDITTEXT IDC_EDIT_1,274,42,40,14,ES_AUTOHSCROLL | ES_NUMBER
CONTROL "",IDC_SPIN_1,"msctls_updown32",UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS,313,42,11,14
END
The range is set programmatically:
const int max_value = 5000;
auto spin = (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_1);
spin->SetRange(1, max_value);
Any idea what's going on?
As I wasn't able to find any related question to it, I'm publishing my inquiries:
The problem arose from the fact that when the CSpinButtonCtrl sets the text of the buddy it converts its numeric value into a string that, by default, includes the thousands separator. As it can be noticed, the CEdit control is set to accept only numbers. As the thousands separator is not a number, the CEdit clamps the text on it, leaving only the text at the left of the separator (the thousand units in my case).
To solve it, just add the UDS_NOTHOUSANDS style to the spin control:
CONTROL "",IDC_SPIN_1,"msctls_updown32",UDS_NOTHOUSANDS | UDS_SETBUDDYINT | UDS_ALIGNRIGHT | UDS_AUTOBUDDY | UDS_ARROWKEYS,313,42,11,14
Of course, another option would be to remove the ES_NUMBER from the edit control, but that wasn’t my UI need.

How to get the scale factor of dwm for a non-dpi-aware process?

Size and position of a control are modified by windows (dwm). I need a way to get the exact scaling in x and y to counter-balance this.
I can not use a manifest, because I am writing a plugin to an .exe (3ds Max).
I can not change the manifest of 3dsMax ( I am not Autodesk. I can not call setprocessdpiawareness() , again because I am a plugin (dll)
into another exe and I would kill layout of my parent process.
The solution should work in windows 7, 8, 10. So i can not use calls that had been introduced in 10 or 8.1
I already tried:
LogicalToPhysicalPoint(0,& p1);
PhysicalToLogicalPoint(0, &p2);
This doesnt change anything (POINTS are unchanged). I do use old school C++ and common controls (subclassing). No WPF, again because I cant use WPF as it doesnt run with my parent exe.
HWND hw = CreateWindowEx(0, L"STATIC", L"thumbnails",
WS_CHILD | WS_VISIBLE | SS_WHITEFRAME | SS_BITMAP ,
9*2, 64*2, 90*2, 46*2, hParentDialog, 0, 0, 0);
After adding the magic "2*" to the controls size and position I do get it where I want. But this is a hack that only works on my current machine with the current monitor.
I would like to replace "2" with the multiplier that dwm applies.

Use of word with accent give an error

i'm creating a simple program in C++ using WinAPI, see this code below:
CreateWindowW(L"STATIC", L"Portão", WS_CHILD | WS_VISIBLE, 10, 10, 100, 20, hwnd, (HMENU)ID_LABEL1, NULL, NULL);
The above code is to create a static control on the main form, the problem is that the 2nd parameter uses a brazilian portuguese word with accent (Portão means Gate), and it give an error, the error is:
C:\CBProjects\ListF\main.cpp|46|error: converting to execution character set: Invalid argument|
i'm using wide character(wchar_t*), but if i replace "Portão" for "Portao" (without accent), it works just fine, Why? How can i solve this?
i'm using Code::Blocks IDE with MinGW Compilator.
C++ has notions of source character set and execution character set. Basically source character set is about characters in file with code and execution character set is about internal string representation in compiler.
Please look at this stack overflow question for more details on this topic.

Word wrap with DrawTextEx in MFC

I need to draw text inside the rect. But when the text is long, it's clip out the extra text. I need to wrap the text, so that extra part of text which don't fits in the first line should goes to the next line.
I have google it, there was a suggestion to use DrawTextEx() with DT_CALCRECT flag instead of DrawText(). But still it's not working, when I uses this flag I am not getting the text.
DRAWTEXTPARAMS lpDTParams;
lpDTParams.cbSize = sizeof(lpDTParams);
lpDTParams.iLeftMargin = 1;
lpDTParams.iRightMargin = 1;
lpDTParams.iTabLength = 4;
lpDTParams.uiLengthDrawn = 0;
pDC->DrawTextExA(sTemp, rc, DT_NOCLIP|DT_CENTER|DT_WORDBREAK|DT_CALCRECT, &lpDTParams);
Any suggestion will be greatly appreciated.
Thanks
You do not need to calculate rectangle.
Use following flags: DT_LEFT | DT_EDITCONTROL | DT_WORDBREAK.
One piece of the advice:
Do not use UNICODE or ANSI specific function names.
Depending on your project settings compiler will pick up the right version. This way your application is portable.