創建基於對話框的Win32應用程式(四)——Edit Control的應用、Unicode轉ANSI、自動滾動 之前的介紹中,我們用到了Button、Static Text、Checkbox這三個控制項。這一節中我們將學習使用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,可以檢驗所需功能皆已完成。
————————————————
本文為本人原創,轉載請註明出處。