消息处理初步

来源:互联网 发布:淘宝网雪纺衫 编辑:程序博客网 时间:2024/05/21 06:37

在visual C++ 6.0 中创建一个MFC单文档应用程序,命名为XiaoXi 。

鼠标消息处理:

在视类CXiaoXiView中添加一个Private(protected或public都行)类型成员变量m_sMousePoint ,并在构造函数中初始化:m_sMousePoint="";

在OnDraw中添加:PDC->TextOUT(200,200,m_sMousePoint);

在视类上右键->Add Windows Message Handler,添加鼠标消息WM_LBUTTONDOWN / WM_LBUTTONUP / WM_MOUSEMOVE响应函数 ,在消息响应函数中添加:

void CXiaoXiView::OnLButtonDown(UINT nFlags, CPoint point)

{

    // TODO: Add your message handler code here and/or call default

    m_sMousePoint.Format("鼠标左键在点(%d,%d)按下",point.x,point.y);

    Invalidate();

    CView::OnLButtonDown(nFlags, point);

}

void CXiaoXiView::OnLButtonUp(UINT nFlags, CPoint point)

{

    // TODO: Add your message handler code here and/or call default

    m_sMousePoint.Format("鼠标左键被释放");

    Invalidate();

    

    CView::OnLButtonUp(nFlags, point);

}

void CXiaoXiView::OnMouseMove(UINT nFlags, CPoint point)

{

    // TODO: Add your message handler code here and/or call default

    m_sMousePoint.Format("鼠标箭头指在点(%d,%d)",point.x,point.y);

    Invalidate();

    

    CView::OnMouseMove(nFlags, point);

}

Format

Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString.

 

Invalidate

Invalidates the entire client area of CWnd. The client area is marked for painting when the next WM_PAINT message occurs.

OK ^_^

键盘消息处理:

在视类中添加成员

    int m_nLine;            //存储按回车键的次数

    CString m_strDisplay;    //存储当前行输入的字符

 

在构造函数中初始化成员:m_nLine=0;

添加键盘消息WM_CHAR响应函数,方法同上,接着编辑:

void CXiaoXiView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

{

    // TODO: Add your message handler code here and/or call default

    if(VK_RETURN==nChar)

    {

        m_strDisplay.Empty();

        m_nLine++;

    }

    else

    {

        m_strDisplay+=(TCHAR)nChar;

    }

    CClientDC dc(this);

    dc.TextOut(0,m_nLine*20,m_strDisplay);

    Invalidate();

 

    CView::OnChar(nChar, nRepCnt, nFlags);

}

OK ^_^ !!!!!!

 

定时器消息处理:

在视类上,右键->Add Virtual Function ,选OnInitalUpdate函数,在函数中设定定时器

SetTimer(1,1000,NULL);    //安装定时器

 

再添加WM_DESTROY消息响应函数,在此函数中清除定时器

    SetTimer(1,1000,NULL);    //安装定时器

再添加WM_TIMER消息响应函数, 在此函数中添加程序代码 :

    MessageBeep(-1);

OK ^_^ !!!!!!!!!!!!!!!

 

自定义消息处理:

在视类头文件中定义用户消息:

    #if _MSC_VER > 1000

#pragma once

const int WM_USERMSG=WM_USER+100; //定义用户消息

#endif // _MSC_VER > 1000

在视类头文件中声名用户消息:

protected:

    //{{AFX_MSG(CXiaoXiView)

    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);

    afx_msg void OnMouseMove(UINT nFlags, CPoint point);

    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);

    afx_msg void OnDestroy();

    afx_msg void OnTimer(UINT nIDEvent);

    afx_msg LRESULT OnMyFunction(WPARAM wParam,LPARAM lParam);

    afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);    //声名消息响应函数

    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

 

在视类源文件中添加消息映射声明:

BEGIN_MESSAGE_MAP(CXiaoXiView, CView)

    //{{AFX_MSG_MAP(CXiaoXiView)

    ON_WM_LBUTTONDOWN()

    ON_WM_LBUTTONUP()

    ON_WM_MOUSEMOVE()

    ON_WM_CHAR()

    ON_WM_DESTROY()

    ON_WM_TIMER()

    ON_MESSAGE(WM_USERMSG,OnMyFunction) //添加消息映射声明

    ON_WM_KEYDOWN()

    //}}AFX_MSG_MAP

    // Standard printing commands

    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)

    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)

    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)

END_MESSAGE_MAP()

 

定义用户消息响应函数:

在源文件中直接输入以下代码:

LRESULT CXiaoXiView::OnMyFunction(WPARAM wParam,LPARAM lParam)

{

    AfxMessageBox("恭喜你,消息发送成功!");

    return NULL;

}

编写程序代码处理消息,添加WM_KEYDOWN消息处理函数,写上程序

LRESULT CXiaoXiView::OnMyFunction(WPARAM wParam,LPARAM lParam)

{

    AfxMessageBox("恭喜你,消息发送成功!");

    return NULL;

}

 

OK ^_^ !!!!!!!!!!!!!!