MFC 对话框小总结
簡單記錄一下,以備日后用到,參數就忽略了,用的時候直接MSDN好了~
下列內容可參考:http://blog.csdn.net/yc_8301/article/details/2570951
http://www.cnblogs.com/Totems/archive/2012/07/11/2586841.html
http://wmnmtm.blog.163.com/blog/static/38245714200810265737699/
1.獲取和設定窗口信息
GetDlgItem( ID... ) 獲取ID窗口的句柄
CWnd::GetWindowText(str...)獲取窗口內的信息
CWnd::SetWindowText(str...)設置窗口內的信息
CWnd::GetWindowTextLength(str...) 獲取窗口內信息的長度
2.屏蔽回車和ESC鍵直接退出程序
? ? 這里使用的方法很簡單,就是增加虛函數OnOk和OnCancel把這兩個函數的內容設置成空函數。
步驟,在類中分別添加黃色部分,然后在.cpp文件中對這兩個函數分別實現為空函數。
// Generated message map functions//{{AFX_MSG(CHwDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnClose(); afx_msg void OnOK();afx_msg void OnCancel();afx_msg void OnOk1();afx_msg void OnCancel1();afx_msg void OnButton1();//}}AFX_MSG DECLARE_MESSAGE_MAP() };
3.在BUTTON上設置位圖
在BUTTON屬性上,把樣式中的所有者繪制和位圖都勾選上。然后,我們要選擇兩個圖標,這里我就直接用程序自帶的位圖編輯器自己編輯了兩個位圖。
然后修改DoDataExchange(...)內容,以及OnInitDialog(...)函數。
void CHwDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);DDX_Control( pDX, IDC_BUTTON1, m_BitmapBtn ); //{{AFX_DATA_MAP(CHwDlg)//}}AFX_DATA_MAP } BOOL CHwDlg::OnInitDialog() {CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herem_BitmapBtn.LoadBitmaps(IDB_BITMAP2,IDB_BITMAP1); return TRUE; // return TRUE unless you set the focus to a control }?
?
4.把enter鍵換為tab鍵同樣的功能
重載PreTranslateMessage(...)函數,如下,我們截取按鍵消息,來實現控件焦點的轉移。
BOOL CHwDlg::PreTranslateMessage(MSG* pMsg)?{
? ? ?// TODO: Add your specialized code here and/or call the base class
? ? if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
? ? {
? ? ? ? CWnd *mwnd = GetNextDlgTabItem (GetFocus()); //取得當前焦點控件的下一個控件的句柄
? ? ? ? if (mwnd)
? ? ? ? {
? ? ? ? ? ? ?mwnd->SetFocus(); //設置下一件控件得到輸入焦點
? ? ? ? ? ? ?return TRUE;
? ? ? ? }
? ? }
? ? return CDialog::PreTranslateMessage(pMsg);
}
總結
以上是生活随笔為你收集整理的MFC 对话框小总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 迅雷离线工具 小众雷友 测试版
- 下一篇: Guava 2.2-新集合类型