MFC 编辑框内容更新方法以及滚动条设置
Posted Milo_lu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC 编辑框内容更新方法以及滚动条设置相关的知识,希望对你有一定的参考价值。
- 内容更新,之前已经说过一种就是调用UpdateData(FALSE);来实现。但是这种方法是对整个编辑框进行更新。
- 通过单个编辑内容设置更新内容。这种是调用SetDlgItemText(IDC_EDIT,m_Edit1_Value);来实现。
- 滚动条的设置,首先是要先设置一下编辑框属性,使能多行滚动,以及其滚动方式。之后申请一个编辑框的控制变量类型。
- 在事件处理时调用函数:m_Edit1_path_Ctl.LineScroll(m_Edit1_path_Ctl.GetLineCount());
- 更新后代码如下:
-
1 // FileCrcDlg.cpp : implementation file 2 // 3 4 #include "stdafx.h" 5 #include "FileCrc.h" 6 #include "FileCrcDlg.h" 7 8 #ifdef _DEBUG 9 #define new DEBUG_NEW 10 #undef THIS_FILE 11 static char THIS_FILE[] = __FILE__; 12 #endif 13 14 ///////////////////////////////////////////////////////////////////////////// 15 // CAboutDlg dialog used for App About 16 17 class CAboutDlg : public CDialog 18 { 19 public: 20 CAboutDlg(); 21 22 // Dialog Data 23 //{{AFX_DATA(CAboutDlg) 24 enum { IDD = IDD_ABOUTBOX }; 25 //}}AFX_DATA 26 27 // ClassWizard generated virtual function overrides 28 //{{AFX_VIRTUAL(CAboutDlg) 29 protected: 30 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 31 //}}AFX_VIRTUAL 32 33 // Implementation 34 protected: 35 //{{AFX_MSG(CAboutDlg) 36 //}}AFX_MSG 37 DECLARE_MESSAGE_MAP() 38 }; 39 40 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 41 { 42 //{{AFX_DATA_INIT(CAboutDlg) 43 //}}AFX_DATA_INIT 44 } 45 46 void CAboutDlg::DoDataExchange(CDataExchange* pDX) 47 { 48 CDialog::DoDataExchange(pDX); 49 //{{AFX_DATA_MAP(CAboutDlg) 50 //}}AFX_DATA_MAP 51 } 52 53 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 54 //{{AFX_MSG_MAP(CAboutDlg) 55 // No message handlers 56 //}}AFX_MSG_MAP 57 END_MESSAGE_MAP() 58 59 ///////////////////////////////////////////////////////////////////////////// 60 // CFileCrcDlg dialog 61 62 CFileCrcDlg::CFileCrcDlg(CWnd* pParent /*=NULL*/) 63 : CDialog(CFileCrcDlg::IDD, pParent) 64 { 65 //{{AFX_DATA_INIT(CFileCrcDlg) 66 m_path = _T(""); 67 m_crc_hex = _T(""); 68 m_crc_dec = _T(""); 69 //}}AFX_DATA_INIT 70 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 71 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 72 } 73 74 void CFileCrcDlg::DoDataExchange(CDataExchange* pDX) 75 { 76 CDialog::DoDataExchange(pDX); 77 //{{AFX_DATA_MAP(CFileCrcDlg) 78 DDX_Control(pDX, IDC_EDIT3_crc_dec, m_Edit3_dec_Ctl); 79 DDX_Control(pDX, IDC_EDIT2_crc_hex, m_Edit2_hex_Ctl); 80 DDX_Control(pDX, IDC_EDIT1_path, m_Edit1_path_Ctl); 81 DDX_Text(pDX, IDC_EDIT1_path, m_path); 82 DDX_Text(pDX, IDC_EDIT2_crc_hex, m_crc_hex); 83 DDX_Text(pDX, IDC_EDIT3_crc_dec, m_crc_dec); 84 //}}AFX_DATA_MAP 85 } 86 87 BEGIN_MESSAGE_MAP(CFileCrcDlg, CDialog) 88 //{{AFX_MSG_MAP(CFileCrcDlg) 89 ON_WM_SYSCOMMAND() 90 ON_WM_PAINT() 91 ON_WM_QUERYDRAGICON() 92 ON_BN_CLICKED(IDOK, OnOpen) 93 ON_BN_CLICKED(IDC_BUTTON1, OnButton1_ClearAll) 94 ON_WM_DROPFILES() 95 //}}AFX_MSG_MAP 96 END_MESSAGE_MAP() 97 98 ///////////////////////////////////////////////////////////////////////////// 99 // CFileCrcDlg message handlers 100 101 BOOL CFileCrcDlg::OnInitDialog() 102 { 103 CDialog::OnInitDialog(); 104 105 // Add "About..." menu item to system menu. 106 107 // IDM_ABOUTBOX must be in the system command range. 108 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 109 ASSERT(IDM_ABOUTBOX < 0xF000); 110 111 CMenu* pSysMenu = GetSystemMenu(FALSE); 112 if (pSysMenu != NULL) 113 { 114 CString strAboutMenu; 115 strAboutMenu.LoadString(IDS_ABOUTBOX); 116 if (!strAboutMenu.IsEmpty()) 117 { 118 pSysMenu->AppendMenu(MF_SEPARATOR); 119 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 120 } 121 } 122 123 // Set the icon for this dialog. The framework does this automatically 124 // when the application‘s main window is not a dialog 125 SetIcon(m_hIcon, TRUE); // Set big icon 126 SetIcon(m_hIcon, FALSE); // Set small icon 127 128 // TODO: Add extra initialization here 129 130 return TRUE; // return TRUE unless you set the focus to a control 131 } 132 133 void CFileCrcDlg::OnSysCommand(UINT nID, LPARAM lParam) 134 { 135 if ((nID & 0xFFF0) == IDM_ABOUTBOX) 136 { 137 CAboutDlg dlgAbout; 138 dlgAbout.DoModal(); 139 } 140 else 141 { 142 CDialog::OnSysCommand(nID, lParam); 143 } 144 } 145 146 // If you add a minimize button to your dialog, you will need the code below 147 // to draw the icon. For MFC applications using the document/view model, 148 // this is automatically done for you by the framework. 149 150 void CFileCrcDlg::OnPaint() 151 { 152 if (IsIconic()) 153 { 154 CPaintDC dc(this); // device context for painting 155 156 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 157 158 // Center icon in client rectangle 159 int cxIcon = GetSystemMetrics(SM_CXICON); 160 int cyIcon = GetSystemMetrics(SM_CYICON); 161 CRect rect; 162 GetClientRect(&rect); 163 int x = (rect.Width() - cxIcon + 1) / 2; 164 int y = (rect.Height() - cyIcon + 1) / 2; 165 166 // Draw the icon 167 dc.DrawIcon(x, y, m_hIcon); 168 } 169 else 170 { 171 CDialog::OnPaint(); 172 } 173 } 174 175 // The system calls this to obtain the cursor to display while the user drags 176 // the minimized window. 177 HCURSOR CFileCrcDlg::OnQueryDragIcon() 178 { 179 return (HCURSOR) m_hIcon; 180 } 181 /****************************************************************************** 182 Public external variables declaration 183 ******************************************************************************/ 184 const unsigned short crc_table[256]={ 185 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 186 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 187 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 188 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 189 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 190 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 191 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 192 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 193 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 194 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 195 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 196 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 197 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 198 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 199 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 200 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 201 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 202 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, 203 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 204 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 205 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 206 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 207 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 208 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 209 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 210 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 211 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 212 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 213 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 214 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 215 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 216 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 217 }; 218 219 220 /************************************************ 221 function name: crc 16 222 using e.g. 223 crc = 0xffff; 224 for(i=0;i<bytes_length;i++) 225 { 226 crc = crc_16(crc,data_bytes_buf[i]); 227 } 228 return crc; 229 *** 230 parameter: crc, data 231 return: crc-16 232 ************************************************/ 233 unsigned short crc_16(unsigned short crc, BYTE data) 234 { 235 BYTE da; 236 237 da=(BYTE)(crc/256); 238 crc<<=8; //crc=crc*256 239 crc ^= crc_table[da^data]; 240 return(crc); 241 } 242 //0x12a5 --> "12a5" 243 void swap_word_byte(WORD wd,BYTE *by) 244 { 245 unsigned char i,k; 246 for(i=0;i<4;i++) 247 { 248 k = (wd & 0xF000)>>12; 249 if(9>=k) 250 { 251 *by++=k+‘0‘; 252 } 253 else if((k>=0x0a)&&(0x0f>=k)) 254 { 255 *by++=k+0x37; 256 } 257 wd<<=4; 258 } 259 } 260 void CFileCrcDlg::OnOpen() 261 { 262 // TODO: Add your control notification handler code here 263 WORD n; 264 WORD i; 265 BYTE szchar[201]; 266 BYTE crc_hex[4],bt; 267 CString strtemp=""; 268 CFileDialog fileDlg(TRUE); 269 fileDlg.m_ofn.lpstrFilter="All files (*.*)"; 270 if(fileDlg.DoModal()==IDOK) 271 { 272 CFile file(fileDlg.m_ofn.lpstrFile,CFile::modeRead); 273 UpdateData(TRUE); 274 m_path += file.GetFilePath(); 275 m_path += "\r\n"; 276 //clear the Edit Dlg 277 i=0xffff; 278 memset(szchar,0,201); 279 while(file.Read(szchar,200)) 280 { 281 n=0; 282 while(szchar[n]) 283 { 284 i=crc_16(i,szchar[n]); 285 n++; 286 } 287 memset(szchar,0,201); 288 } 289 strtemp.Format("%d",i); 290 m_crc_dec+=strtemp; 291 m_crc_dec+="\r\n"; 292 293 swap_word_byte(i,crc_hex); 294 for(i=0;i<4;i++) 295 { 296 bt =*(char*)(crc_hex+i); 297 strtemp.Format("%c",bt); 298 m_crc_hex+=strtemp; 299 } 300 m_crc_hex += "\r\n"; 301 // write value to edit 302 SetDlgItemText(IDC_EDIT1_path,m_path); 303 SetDlgItemText(IDC_EDIT2_crc_hex,m_crc_hex); 304 SetDlgItemText(IDC_EDIT3_crc_dec,m_crc_dec); 305 //set the line scroll to the bottom 306 m_Edit1_path_Ctl.LineScroll(m_Edit1_path_Ctl.GetLineCount()); 307 m_Edit2_hex_Ctl.LineScroll(m_Edit2_hex_Ctl.GetLineCount()); 308 m_Edit3_dec_Ctl.LineScroll(m_Edit3_dec_Ctl.GetLineCount()); 309 file.Close(); 310 } 311 } 312 313 void CFileCrcDlg::OnButton1_ClearAll() 314 { 315 // TODO: Add your control notification handler code here 316 UpdateData(TRUE); 317 m_crc_dec = ""; 318 m_crc_hex = ""; 319 m_path = ""; 320 UpdateData(FALSE); 321 } 322 323 void CFileCrcDlg::OnDropFiles(HDROP hDropInfo) 324 { 325 // TODO: Add your message handler code here and/or call default 326 UINT j,iFileCount; 327 char file_name[MAX_PATH]; 328 //variable for crc 329 WORD n; 330 WORD i; 331 BYTE szchar[201]; 332 BYTE crc_hex[4],bt; 333 CString strtemp=""; 334 335 iFileCount=::DragQueryFile(hDropInfo,0xffffffff,NULL,0); 336 337 for(j=0;j<iFileCount;j++) 338 { 339 ::DragQueryFile(hDropInfo,j,file_name,MAX_PATH); 340 CFile file(file_name,CFile::modeRead); 341 UpdateData(TRUE); 342 //clear the Edit Dlg 343 i=0xffff; 344 memset(szchar,0,201); 345 while(file.Read(szchar,200)) 346 { 347 n=0; 348 while(szchar[n]) 349 { 350 i=crc_16(i,szchar[n]); 351 n++; 352 } 353 memset(szchar,0,201); 354 } 355 strtemp.Format("%d",i); 356 m_crc_dec+=strtemp; 357 m_crc_dec+="\r\n"; 358 359 swap_word_byte(i,crc_hex); 360 for(i=0;i<4;i++) 361 { 362 bt =*(char*)(crc_hex+i); 363 strtemp.Format("%c",bt); 364 m_crc_hex+=strtemp; 365 } 366 m_crc_hex += "\r\n"; 367 368 m_path += file.GetFilePath(); 369 m_path += "\r\n"; 370 // write value to edit 371 SetDlgItemText(IDC_EDIT1_path,m_path); 372 SetDlgItemText(IDC_EDIT2_crc_hex,m_crc_hex); 373 SetDlgItemText(IDC_EDIT3_crc_dec,m_crc_dec); 374 //set the line scroll to the bottom 375 m_Edit1_path_Ctl.LineScroll(m_Edit1_path_Ctl.GetLineCount()); 376 m_Edit2_hex_Ctl.LineScroll(m_Edit2_hex_Ctl.GetLineCount()); 377 m_Edit3_dec_Ctl.LineScroll(m_Edit3_dec_Ctl.GetLineCount()); 378 file.Close(); 379 } 380 ::DragFinish(hDropInfo); 381 CDialog::OnDropFiles(hDropInfo); 382 }
OK, That is all. Thank you!
以上是关于MFC 编辑框内容更新方法以及滚动条设置的主要内容,如果未能解决你的问题,请参考以下文章
在MFC中:静态文本类;按纽类;编辑框类;滚动条类;列表框类;组合框类等所对应的类名以及其功能