Creating Dialogbased Windows Application (4) / 成立基于对话框的Windows应用程序(四卡塔 尔(英语:State of Qatar)Edit Control、C

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

  

  此前的牵线中,我们用到了Button、Static Text、Checkbox那七个控件。那风流倜傥节中我们将学习运用Edit Control(编辑框卡塔 尔(英语:State of Qatar)、Combo Box控件,在那之中还包含Unicode转ANSI的点子、成立Open File Dialog、 读取文件、可变参数(这里用于转移日志卡塔 尔(阿拉伯语:قطر‎、文本框自动滚动的作用等。 

24、首先切换成Reasource View(Ctrl+Shift+E卡塔 尔(英语:State of Qatar),找到待改革的主窗体,并从Toolbox(Ctrl+Atl+X卡塔尔国中加多艾德it Control、Combo 博克斯、Button控件如下:

 图片 1

其中Edit Box的Properties设置为:

 图片 2

点击Combo Box右侧三角箭头能够调动其下拉框的大小:

 图片 3

转移控件ID后记得在reasource.h文件军长多余的概念删除或注释掉:

 图片 4

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

25、在被调用的通令音信响应函数(Dlg_OnCommand卡塔尔中增添对Browse开关的响应动作。

点击Browse按键将创立Open File Dialog,并将筛选的文件路线呈现在两旁的Combo Box中:

 图片 5

内部WideCharToMultiByte函数达成了Unicode到ANSI的更动。

26、实今后Edit Control中添Gavin本并促成全自动滚动:

 图片 6

图片 7

内部当“count == 0”时,将屏蔽可变参数的接受。关于可变参数的施用能够在后头的代码中看出。

按下Open开关,将依据Combo Box中的路线读取文件,并按ANSI文本格式展现在Edit Control中:

 图片 8

图片 9图片 10

  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, '', 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 %srnrn"), num, file);
143         num++;
144         AddText(0, buffer);
145         delete[] buffer;
146         AddText(0, "rnrn");
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,能够核查所需功用皆是到位。

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

正文为本人原创,转发请注解出处。

 

本文由2020欧洲杯官方投注-2020欧洲杯官方投注网址发布于win7,转载请注明出处:Creating Dialogbased Windows Application (4) / 成立基于对话框的Windows应用程序(四卡塔 尔(英语:State of Qatar)Edit Control、C

相关阅读