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 dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    m_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);
}

posted @ 2012-11-04 15:40  cococo点点  阅读(343)  评论(0编辑  收藏  举报