Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

创建基于对话框的Windows应用程序(四)—— Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、自动滚动

  

  之前的介绍中,我们用到了Button、Static Text、Checkbox这三个控件。这一节中我们将学习使用Edit Control(编辑框)、Combo Box控件,其中还包括Unicode转ANSI的方法、创建Open File Dialog、 读取文件、可变参数(这里用于生成日志)、文本框自动滚动的功能等。

24、首先切换到Reasource View(Ctrl+Shift+E),找到待修改的主窗体,并从Toolbox(Ctrl+Atl+X)中添加Edit Control、Combo Box、Button控件如下:

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

其中Edit Box的Properties设置为:

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

点击Combo Box右边三角箭头可以调整其下拉框的大小:

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

更改控件ID后记得在reasource.h文件中将多余的定义删除或注释掉:

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

注意:在以代码形式打开reasource.h或.rc文件后若要回到Reasource View中查看编辑,须先将打开的各相关文件关闭。

25、在被调用的命令消息响应函数(Dlg_OnCommand)中添加对Browse按钮的响应动作。

点击Browse按钮将创建Open File Dialog,并将选取的文件路径显示在一旁的Combo Box中:

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

其中WideCharToMultiByte函数实现了Unicode到ANSI的转换。

26、实现在Edit Control中添加文本并实现自动滚动:

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

其中当“count == 0”时,将屏蔽可变参数的使用。关于可变参数的使用可以在后面的代码中看到。

按下Open按钮,将根据Combo Box中的路径读取文件,并按ANSI文本格式显示在Edit Control中:

Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

 #include <Windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <Shobjidl.h>
#include <mutex>
#include <string>
#include "Resource.h" // Sets the dialog box icons
inline void chSETDLGICONS(HWND hWnd, int idi) {
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)
LoadIcon((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
MAKEINTRESOURCE(idi)));
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)
LoadIcon((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
MAKEINTRESOURCE(idi)));
} // The normal HANDLE_MSG macro in WindowsX.h does not work properly for dialog
// boxes because DlgProc returns a BOOL instead of an LRESULT (like
// WndProcs). This chHANDLE_DLGMSG macro corrects the problem:
#define chHANDLE_DLGMSG(hWnd, message, fn) \
case (message): return (SetDlgMsgResult(hWnd, uMsg, \
HANDLE_##message((hWnd), (wParam), (lParam), (fn)))) // Main dialog
HWND g_hDlg; std::mutex g_add;
// Adds a string to the "TextView" edit control
// Disable "..." when "count == 0", otherwise
// "count" can't be smaller than the (formatted) string size
void AddText(DWORD count, PCTSTR pszFormat, ...) {
std::lock_guard<std::mutex> lock(g_add); static std::string text; if (count == )
{
text.append(pszFormat);
}
else
{
va_list argList;
va_start(argList, pszFormat);
TCHAR *sz = new TCHAR[count];
memset(sz, '\0', count);
_vstprintf_s(sz, count, pszFormat, argList);
va_end(argList);
text.append(sz);
delete[] sz;
} HWND hEdit = GetDlgItem(g_hDlg, IDC_TEXTVIEW);
::SendMessage(hEdit,
WM_SETREDRAW, FALSE/*关闭重绘*/, ); //Edit_SetText(hEdit, text.c_str());
//::SetDlgItemText(g_hDlg/*包含Edit Control主窗口的句柄*/,
//IDC_TEXTVIEW/*Edit Control资源的编号*/, text.c_str()/*要输出的信息*/);
::SendMessage(hEdit, WM_SETTEXT, false, (LPARAM)text.c_str()); int iLine = (int)::SendMessage(GetDlgItem(g_hDlg,
IDC_TEXTVIEW)/*Edit Control的句柄*/, EM_GETLINECOUNT, /*忽略*/, /*忽略*/);
::SendMessage(hEdit, EM_LINESCROLL, /*水平滚动的字符个数*/,
iLine/*垂直滚动的行数*/);
size_t iOutputLen = _tcslen(pszFormat);
::SendMessage(hEdit, EM_SETSEL, iOutputLen/*要选中字符的起始位置*/,
iOutputLen/*要选中字符的结束位置*/); ::SendMessage(hEdit, WM_SETREDRAW, TRUE/*打开重绘*/, );
} INT_PTR WINAPI NewDlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg)
{
case WM_CLOSE:
EndDialog(hWnd, );
break;
} return(FALSE);
} void Dlg_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify) {
HRESULT hr;
IFileDialog *pfd = NULL;
LPWSTR filePath = L"";
TCHAR file[MAX_PATH] = { };
COMDLG_FILTERSPEC rgSpec[] =
{
{ TEXT(L"文本文档"), TEXT(L"*.txt") },
{ TEXT(L"所有文件"), TEXT(L"*.*") }
};
TCHAR* buffer;
DWORD filesize;
HWND hWndComboBox;
static unsigned int num = ;
switch (id) {
case IDC_CHECKONTOP:
SetWindowPos(hWnd, IsDlgButtonChecked(hWnd, IDC_CHECKONTOP)
? HWND_TOPMOST : HWND_NOTOPMOST, , , , , SWP_NOMOVE | SWP_NOSIZE);
break;
case IDC_BROWSE:
hr = CoCreateInstance(CLSID_FileOpenDialog,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pfd));
FILEOPENDIALOGOPTIONS dwFlags;
hr = pfd->GetOptions(&dwFlags);
hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
hr = pfd->SetFileTypes(, rgSpec);
hr = pfd->SetFileTypeIndex();
hr = pfd->Show(hWnd);
IShellItem * pShellItem;
hr = pfd->GetResult(&pShellItem);
if (hr == S_OK)
{
hr = pShellItem->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &filePath);
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, filePath, MAX_PATH, file,
sizeof(file), NULL, NULL);
SetDlgItemText(hWnd, IDC_EDITCOMBO, file);
}
break;
case IDOPEN:
GetDlgItemText(hWnd, IDC_EDITCOMBO, file, _countof(file));
hWndComboBox = GetDlgItem(hWnd, IDC_EDITCOMBO);
SendMessage(hWndComboBox, CB_ADDSTRING, , (LPARAM)file);
HANDLE hFile;
DWORD readsize;
hFile = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, NULL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
break;
}
filesize = GetFileSize(hFile, NULL);
buffer = new char[filesize + ];
ReadFile(hFile, buffer, filesize, &readsize, NULL);
buffer[filesize] = ;
AddText(MAX_PATH + , _T("NO.%03d %s\r\n\r\n"), num, file);
num++;
AddText(, buffer);
delete[] buffer;
AddText(, "\r\n\r\n");
CloseHandle(hFile);
break;
case IDCANCEL:
SendMessage(hWnd, WM_CLOSE, , );
break; }
} BOOL Dlg_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam) {
g_hDlg = hWnd; chSETDLGICONS(hWnd, IDI_ICON1); SetWindowPos(hWnd, HWND_TOPMOST, , , , , SWP_NOMOVE | SWP_NOSIZE);
CheckDlgButton(hWnd, IDC_CHECKONTOP, BST_CHECKED); return(TRUE);
} INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg)
{
chHANDLE_DLGMSG(hWnd, WM_INITDIALOG, Dlg_OnInitDialog);
chHANDLE_DLGMSG(hWnd, WM_COMMAND, Dlg_OnCommand);
case WM_CLOSE:
EndDialog(hWnd, );
break;
} return(FALSE);
} int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {
DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_DIALOG),
NULL, Dlg_Proc, _ttoi(pszCmdLine)); return();
}

Win32WindowsApplication.cpp

26、此时按下F5 Start Debugging,可以检验所需功能皆已完成。

————————————————

本文为本人原创,转载请注明出处。

http://www.cnblogs.com/lantingji/p/5886181.html

上一篇:vs2019远程调试win7系统的程序


下一篇:OpenGL入门-画一个三角形(VS2019可运行源码)