1

I have MFC application for which I want to add one dialog to browse file location, using CMFCEditBrowseCtrl object. But I have not been able to set initial path properly, e.g. "C:\Program Files\Path". When tried it is showing chinese letters. How can i do that? I have the code as follows :

m_pathCtrl.EnableFolderBrowseButton();
m_pathCtrl.SetWindowText(_T("C:\\Program Files\\Path"));

But it is showing something like this ->encoding problem

How to properly show the path in English? Please Guide.

Codename_DJ
  • 553
  • 1
  • 11
  • 40
  • Any warnings during build? Is your project configured in UNICODE mode? – Frédéric Hamidi Mar 03 '15 at 10:12
  • "configured in UNICODE mode?" Can you please tell me how to verify that? – Codename_DJ Mar 03 '15 at 10:16
  • That information is present in the project properties. I can't specify exactly where because it depends on the version of Visual Studio you're using, but that should give you a starting point. – Frédéric Hamidi Mar 03 '15 at 10:18
  • The character set is set to "Use Multi-Byte Character Set". Is there any work-around to keep it as it is? – Codename_DJ Mar 03 '15 at 10:29
  • So, Unicode is off. That means `_T()` should map to nothing and `SetWindowText()` should map to `SetWindowTextA()`. Despite that, it looks like the control operates in Unicode mode (thus the incorrectly rendered string). Is there a `#define SetWindowText SetWindowTextW` somewhere in your code? But even then, the compiler should emit a warning, and possibly even an error. – Frédéric Hamidi Mar 03 '15 at 10:32
  • No, #define SetWindowText SetWindowTextW is not present. I initially tried SetWindowTextA("C:\\Program Files\\Path"); with same result. Don't have a clue what is wrong here :/ ? – Codename_DJ Mar 03 '15 at 10:40
  • Me neither. Does `SetWindowTextW(L"C:\\Program Files\\Path")` give a different result? – Frédéric Hamidi Mar 03 '15 at 10:42
  • No. same chinese texts – Codename_DJ Mar 03 '15 at 11:00
  • 2
    Note that, in my experience, `CMFCEditBrowseCtrl` only works correctly in Unicode builds. There's no way around it except by changing your project to Unicode. The control is from Common Controls V6, which is Unicode only. – Roger Rowland Mar 03 '15 at 13:21

1 Answers1

0

The issue arises because you are using the ASCII character set, but the control expects Unicode. MS explains how to set up an CMFCEditBrowseCtrl in a dialog when using ASCII here: https://learn.microsoft.com/en-us/cpp/mfc/reference/cmfceditbrowsectrl-class?view=msvc-170. Use the dialog editor to insert an edit control in the dialog, then change its type from CEdit to CMFCEditBrowseCtrl in the header file.

You can also see the proper characters in the window by using SetWindowTextW; e g, inputFilesCtrl.SetWindowTextW(L"C:\SomeDirectory");. The Chinese characters you are seeing are what happens when a 1-byte character set is interpreted as a 2-byte one.

Woody20
  • 791
  • 11
  • 30