Creating Dialogbased Win32 Application / 创建基于对话框的Win32应用程序Edit Control的应用Unicode转ANSI自动滚动 /

Posted 兰亭集

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Creating Dialogbased Win32 Application / 创建基于对话框的Win32应用程序Edit Control的应用Unicode转ANSI自动滚动 /相关的知识,希望对你有一定的参考价值。

创建基于对话框的Win32应用程序(四)——Edit Control的应用、Unicode转ANSI、自动滚动

 

  之前的介绍中,我们用到了Button、Static Text、Checkbox这三个控件。这一节中我们将学习使用Edit Control(编辑框)控件,其中还包括Unicode转ANSI的方法、文本框自动滚动的功能等。

 

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

 技术分享

其中较大的Edit Box的Properties设置为:

 技术分享

较小的Edit Box的Properties可以保持默认设置。

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

 技术分享

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

25、在被调用的命令消息响应函数(Dlg_OnCommand)中添加对新增控件的响应操作。

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

 技术分享

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

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

 技术分享

技术分享

按下Open按钮,根据较小编辑框中的路径读取文件,并按ANSI文本格式显示在较大编辑框中:

 技术分享

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

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

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

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

以上是关于Creating Dialogbased Win32 Application / 创建基于对话框的Win32应用程序Edit Control的应用Unicode转ANSI自动滚动 /的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5从子窗口关闭父窗口和子窗口

CREATING SPRING BEANS

[Spring Boot ] Creating the Spring Boot Project : Demo: Creating a REST Controller

Creating Hyperv Agent Installer

[RxJS] Creating Observable From Scratch

7.3 Models -- Creating And Deleting Records