在自定義.h文件中 繼承CFrameWnd 聲明 右鍵按下成員函數 而後在.cpp文件處理事件app
並在消息映射目中添加右鍵按下消息映射 ON_WM_LBUTTONDOWN()函數
#ifndef _AFXDLL
#define _AFXDLL
#endif
#include <afx.h>
#include <afxwin.h>
class MainApp :public CWinApp
{
public:
virtual BOOL InitInstance();
};this
class AppFrame :public CFrameWnd{
public:
AppFrame();
protected:
afx_msg void OnPaint();
//鼠標右鍵按下
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP();
};繼承
.cpp 文件實現功能和GUI繪製事件
#include "app.h"
MainApp app;
AppFrame * aframe;
void AppFrame::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.DrawText(TEXT("The Windows"),-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);it
}
BEGIN_MESSAGE_MAP(AppFrame,CFrameWnd)
ON_WM_PAINT()
//鼠標右鍵按下消息註冊
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()io
BOOL MainApp::InitInstance()
{
aframe = new AppFrame;
aframe->ShowWindow(SW_NORMAL);
aframe->UpdateWindow();
m_pMainWnd = aframe;
return true;
}
AppFrame::AppFrame()
{
Create(NULL,"application");
}
//響應鼠標右鍵處理
void AppFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
//畫線
CRect rect;
GetClientRect(&rect);
CClientDC dc(this);
dc.MoveTo(rect.left, rect.right);
dc.LineTo(rect.right, rect.bottom);
dc.MoveTo(rect.right, rect.top);
dc.LineTo(rect.left, rect.bottom);
}class