C ++编辑框不能正确显示文本 [英] C++ Edit Boxes not displaying text properly
问题描述
我正在尝试选择文件并将文件路径放在编辑框中。当我这样做时,我得到一个乱码如穨<当实际路径类似于c:\\\333.conf
我还有两个其他编辑框用于选择有效的文件夹路径正确。
我有一个wchar_t *持有我要写入编辑框的路径。
I am trying to select a file and place the file path in an edit box. When i do so i get a garbled string like 穨< when the actual path is something like c:\config\333.conf
I have two other edit boxes that i am using to select a folder path that work correctly.
I have a wchar_t* holding the path that i want to write to the editbox.
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDCANCEL:
SendMessage(hDlg, WM_CLOSE, 0, 0);
return TRUE;
case IDOK:
//SetDlgItemText(hDlg, IDC_IMAGE_A_EDIT, _T("OK was pressed"));
DoTune();
return TRUE;
case IDC_IMAGE_A_FOLDER_SELECT_BUTTON:
str = OpenFolder();
SetDlgItemText(hDlg, IDC_IMAGE_A_EDIT, str);//this works
return TRUE;
case IDC_IMAGE_B_FOLDER_SELECT_BUTTON:
str = OpenFolder();
SetDlgItemText(hDlg, IDC_IMAGE_B_EDIT, str);//so does this
return TRUE;
case IDC_CONF_FILE_SELECT_BUTTON:
wchar_t* filter = L"CONF Files (*.conf)\0*.conf\0";
str = OpenFileName(filter, NULL);
SetDlgItemTextW(hDlg, IDC_CONF_EDIT, str); //this doesn't work
return TRUE;
}
break;
case WM_CLOSE:
if(MessageBox(hDlg, TEXT("Close the program?"), TEXT("Close"),
MB_ICONQUESTION | MB_YESNO) == IDYES)
{
DestroyWindow(hDlg);
}
return TRUE;
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
}
return FALSE;
}
//main.h
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#include "resource.h"
#include <string>
#include <commdlg.h>
#include <shlobj.h>
#include <stdio.h>
#include <strsafe.h>
#include <comdef.h>
#include <string.h>
wchar_t* str;
//file path variable
char* CONFIG_PATH_ANALYSIS;
int numFiles;
void DoTune();
void DisplayErrorBox(LPTSTR lpszFunction);
wchar_t* OpenFolder();
void RunAnalysis(wchar_t* Folder);
wchar_t* ConvertCharArrayToLPCWSTR(const char* charArray);
wchar_t* OpenFileName(wchar_t *filter, HWND owner);
typedef int (__cdecl *ANALYZE_PROC)(char *, char *, char *,char *, bool, bool);
typedef int (__cdecl *GETAVERAGE_PROC)(char *, char *);
typedef int (__cdecl *VERIFY_PROC)(char *, char *);
HINSTANCE hFoo;
HWND hDlg;
GETAVERAGE_PROC getAverageProcess;
ANALYZE_PROC analyzeProcess;
VERIFY_PROC verifyProcess;
//OpenFileName Definition
wchar_t* OpenFileName(wchar_t *filter, HWND owner) {
OPENFILENAME ofn;
wchar_t fileName[MAX_PATH] = L"";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = owner;
ofn.lpstrFilter = filter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = NULL;
wchar_t* fileNameStr;
if ( GetOpenFileName(&ofn) )
fileNameStr = fileName;
return fileNameStr;
}
因为你可以看到它非常简单,我不是C ++的新手所以我想我错过了一些简单的东西。此外,我已经验证str在传递给SetDlgItemText函数之前具有正确的路径。
一如既往,我感谢任何输入。
as you can see it's pretty straightforward and i am not new to C++ so i figure i am missing something simple. Also, i have verified that str has the correct path before it gets passed to the SetDlgItemText function.
As always, i appreciate any input.
推荐答案
我想你忘了把char转换成wchar所以...
你知道char是8位而wchar是16位...
所以2,将8位信息放入1个wchar变量中。
i不知道你在哪里有垃圾对话框或主窗口的编辑框?
但是我认为错误就在这里
i think you forget convert char to wchar so...
you know that char is 8 bit and wchar is 16 bit...
so 2, 8 bit information is put in 1 wchar variable.
i don't know where you have garbage in dialog box or in edit box in main window?
but i think mistake is here
if ( GetOpenFileName(&ofn) )
fileNameStr = fileName;
抱歉信息不好:-D
sorry for poor info :-D
我有一个解决方案,但我在遗憾地解释它为何起作用。我将OpenFileName函数从原始格式更改为此格式,并且完美运行。
I have a solution but i am at a loss to explain why it works. I changed the OpenFileName function from its original form to this and it works perfectly.
char* OpenFileName(char *filter = "Conf Files (*.conf)\0*.conf\0", HWND owner = NULL) {
OPENFILENAME ofn;
char fileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = owner;
ofn.lpstrFilter = filter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "";
char* fileNameStr;
if ( GetOpenFileName(&ofn) )
fileNameStr = fileName;
return fileNameStr;
}
所以基本上只是将wchar_t改为char工作;但是,建议的标准是在Windows应用程序中使用wchar_t over char,这样就可以了。我仍然想知道为什么这会在另一个失败的情况下起作用。
So basically just changing wchar_t to char worked; however, the recommended standard is to use wchar_t over char in a windows application so this is going a bit against that. I would still like to know why this works where the other failed.
这篇关于C ++编辑框不能正确显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!