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控件如下:

 

其中Edit Box的Properties设置为:

 

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

 

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

 

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

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

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

 

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

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

 

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

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

 

  1 #include <Windows.h>
  2 #include <windowsx.h>
  3 #include <tchar.h>
  4 #include <Shobjidl.h>
  5 #include <mutex>
  6 #include <string>
  7 #include "Resource.h"
  8 
  9 // Sets the dialog box icons
 10 inline void chSETDLGICONS(HWND hWnd, int idi) {
 11     SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)
 12         LoadIcon((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
 13             MAKEINTRESOURCE(idi)));
 14     SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)
 15         LoadIcon((HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
 16             MAKEINTRESOURCE(idi)));
 17 }
 18 
 19 // The normal HANDLE_MSG macro in WindowsX.h does not work properly for dialog
 20 // boxes because DlgProc returns a BOOL instead of an LRESULT (like
 21 // WndProcs). This chHANDLE_DLGMSG macro corrects the problem:
 22 #define chHANDLE_DLGMSG(hWnd, message, fn)                 \
 23    case (message): return (SetDlgMsgResult(hWnd, uMsg,     \
 24       HANDLE_##message((hWnd), (wParam), (lParam), (fn))))
 25 
 26 // Main dialog
 27 HWND     g_hDlg;
 28 
 29 std::mutex g_add;
 30 // Adds a string to the "TextView" edit control
 31 // Disable "..." when "count == 0", otherwise
 32 // "count" can't be smaller than the (formatted) string size
 33 void AddText(DWORD count, PCTSTR pszFormat, ...) {
 34     std::lock_guard<std::mutex> lock(g_add);
 35 
 36     static std::string text;
 37 
 38     if (count == 0)
 39     {
 40         text.append(pszFormat);
 41     }
 42     else
 43     {
 44         va_list argList;
 45         va_start(argList, pszFormat);
 46         TCHAR *sz = new TCHAR[count];
 47         memset(sz, '\0', count);
 48         _vstprintf_s(sz, count, pszFormat, argList);
 49         va_end(argList);
 50         text.append(sz);
 51         delete[] sz;
 52     }
 53 
 54     HWND hEdit = GetDlgItem(g_hDlg, IDC_TEXTVIEW);
 55     ::SendMessage(hEdit,
 56         WM_SETREDRAW, FALSE/*关闭重绘*/, 0);
 57 
 58     //Edit_SetText(hEdit, text.c_str());
 59     //::SetDlgItemText(g_hDlg/*包含Edit Control主窗口的句柄*/, 
 60     //IDC_TEXTVIEW/*Edit Control资源的编号*/, text.c_str()/*要输出的信息*/);
 61     ::SendMessage(hEdit, WM_SETTEXT, false, (LPARAM)text.c_str());
 62 
 63     int iLine = (int)::SendMessage(GetDlgItem(g_hDlg, 
 64         IDC_TEXTVIEW)/*Edit Control的句柄*/, EM_GETLINECOUNT, 0/*忽略*/, 0/*忽略*/);
 65     ::SendMessage(hEdit, EM_LINESCROLL, 0/*水平滚动的字符个数*/, 
 66         iLine/*垂直滚动的行数*/);
 67     size_t iOutputLen = _tcslen(pszFormat);
 68     ::SendMessage(hEdit, EM_SETSEL, iOutputLen/*要选中字符的起始位置*/, 
 69         iOutputLen/*要选中字符的结束位置*/);
 70 
 71     ::SendMessage(hEdit, WM_SETREDRAW, TRUE/*打开重绘*/, 0);
 72 }
 73 
 74 INT_PTR WINAPI NewDlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
 75     switch (uMsg)
 76     {
 77     case WM_CLOSE:
 78         EndDialog(hWnd, 0);
 79         break;
 80     }
 81 
 82     return(FALSE);
 83 }
 84 
 85 void Dlg_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify) {
 86     HRESULT hr;
 87     IFileDialog *pfd = NULL;
 88     LPWSTR filePath = L"";
 89     TCHAR file[MAX_PATH] = { 0 };
 90     COMDLG_FILTERSPEC rgSpec[] =
 91     {
 92         { TEXT(L"文本文档"), TEXT(L"*.txt") },
 93         { TEXT(L"所有文件"), TEXT(L"*.*") }
 94     };
 95     TCHAR* buffer;
 96     DWORD filesize;
 97     HWND hWndComboBox;
 98     static unsigned int num = 1;
 99     switch (id) {
100     case IDC_CHECKONTOP:
101         SetWindowPos(hWnd, IsDlgButtonChecked(hWnd, IDC_CHECKONTOP)
102             ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
103         break;
104     case IDC_BROWSE:
105         hr = CoCreateInstance(CLSID_FileOpenDialog,
106             NULL,
107             CLSCTX_INPROC_SERVER,
108             IID_PPV_ARGS(&pfd));
109         FILEOPENDIALOGOPTIONS dwFlags;
110         hr = pfd->GetOptions(&dwFlags);
111         hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
112         hr = pfd->SetFileTypes(2, rgSpec);
113         hr = pfd->SetFileTypeIndex(1);
114         hr = pfd->Show(hWnd);
115         IShellItem * pShellItem;
116         hr = pfd->GetResult(&pShellItem);
117         if (hr == S_OK)
118         {
119             hr = pShellItem->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &filePath);
120             WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, filePath, MAX_PATH, file, 
121                 sizeof(file), NULL, NULL);
122             SetDlgItemText(hWnd, IDC_EDITCOMBO, file);
123         }
124         break;
125     case IDOPEN:
126         GetDlgItemText(hWnd, IDC_EDITCOMBO, file, _countof(file));
127         hWndComboBox = GetDlgItem(hWnd, IDC_EDITCOMBO);
128         SendMessage(hWndComboBox, CB_ADDSTRING, 0, (LPARAM)file);
129         HANDLE hFile;
130         DWORD readsize;
131         hFile = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, 
132             OPEN_EXISTING, NULL, NULL);
133         if (hFile == INVALID_HANDLE_VALUE)
134         {
135             CloseHandle(hFile);
136             break;
137         }
138         filesize = GetFileSize(hFile, NULL);
139         buffer = new char[filesize + 1];
140         ReadFile(hFile, buffer, filesize, &readsize, NULL);
141         buffer[filesize] = 0;
142         AddText(MAX_PATH + 32, _T("NO.%03d %s\r\n\r\n"), num, file);
143         num++;
144         AddText(0, buffer);
145         delete[] buffer;
146         AddText(0, "\r\n\r\n");
147         CloseHandle(hFile);
148         break;
149     case IDCANCEL:
150         SendMessage(hWnd, WM_CLOSE, 0, 0);
151         break;
152 
153     }
154 }
155 
156 BOOL Dlg_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam) {
157     g_hDlg = hWnd;
158     
159     chSETDLGICONS(hWnd, IDI_ICON1);
160 
161     SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
162     CheckDlgButton(hWnd, IDC_CHECKONTOP, BST_CHECKED);
163 
164     return(TRUE);
165 }
166 
167 INT_PTR WINAPI Dlg_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
168     switch (uMsg)
169     {
170         chHANDLE_DLGMSG(hWnd, WM_INITDIALOG, Dlg_OnInitDialog);
171         chHANDLE_DLGMSG(hWnd, WM_COMMAND, Dlg_OnCommand);
172     case WM_CLOSE:
173         EndDialog(hWnd, 0);
174         break;
175     }
176 
177     return(FALSE);
178 }
179 
180 int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) {
181     DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_DIALOG),
182         NULL, Dlg_Proc, _ttoi(pszCmdLine));
183 
184     return(0);
185 }
Win32WindowsApplication.cpp

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

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

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

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

posted on 2016-09-19 18:24  兰亭集  阅读(740)  评论(0编辑  收藏  举报

导航