之前的介紹中,我們用到了Button、Static Text、Checkbox這三個控制項。這一節中我們將學習使用Edit Control(編輯框)、Combo Box控制項,其中還包括Unicode轉ANSI的方法、創建Open File Dialog、 讀取文件、可變參數(這裡用於生成日誌)、文本框... ...
創建基於對話框的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