hientoado
/* nhay phai chuot hien toa do */
#include<afxwin.h>//thu vien MFC
//lop ung dung
class CMyApp:public CWinApp
{
public:
BOOL InitInstance();//ham khoi tao
//ung dung
};
//lop cua so
class CMyWin:public CFrameWnd
{
private://du lieu moi cua lop
CDC m_memDC;//ngu canh tb cua cs ao
CBitmap m_bmp;//anh chua cs t
CBrush m_bkbrush;//mau nen cs ao
int maxX,maxY;
CPoint oldPoint;//de chua td chuot
CPen p;//but ve
CBrush b;//choi to
public://phuoc thuc cua lop
CMyWin();//ham tao
//ham xu ly nhan chuot trai
afx_msg void OnRButtonDown(UINT nFlags,CPoint pt);
afx_msg void OnPaint();//xu ly ve lai cs
//ham xy ly khi nha chuot trai
afx_msg void OnRButtonUp(UINT nFlags,CPoint point);
DECLARE_MESSAGE_MAP()
};
CMyApp theApp;//doi tuong cua lop ung dung
BOOL CMyApp::InitInstance()//xay dung ham kt ung dung
{
m_pMainWnd=new CMyWin;
m_pMainWnd->ShowWindow(m_nCmdShow);//dat tt hien cs
m_pMainWnd->UpdateWindow();//ve cs ra man hinh
return true;
}
//khai bao anh xa thong diep
BEGIN_MESSAGE_MAP(CMyWin,CFrameWnd)
ON_WM_RBUTTONDOWN() //nhan chuot trai
ON_WM_PAINT()//ve lai cs
ON_WM_RBUTTONUP()//nha chuot trai
END_MESSAGE_MAP()
CMyWin::CMyWin()//xay dung ham tao cs
{
Create(NULL,"chuong trinh vi du5. 02");//tao cs
//tao cs ao
maxX=GetSystemMetrics(SM_CXSCREEN);
maxY=GetSystemMetrics(SM_CYSCREEN);
CClientDC DC(this);
m_memDC.CreateCompatibleDC(&DC);
m_bmp.CreateCompatibleBitmap(&DC,maxX,maxY);
m_memDC.SelectObject(&m_bmp);
m_bkbrush.CreateStockObject(WHITE_BRUSH);
m_memDC.SelectObject(&m_bkbrush);
m_memDC.PatBlt(0,0,maxX,maxY,PATCOPY);
}
void CMyWin::OnPaint()//ve lai cs
{
CPaintDC DC(this);
//copy tu cua so ao dua ra cs that
DC.BitBlt(0,0,maxX,maxY,&m_memDC,0,0,SRCCOPY);
}
//xay dung ham xu ly sk cho nhan chuot trai
afx_msg void CMyWin::OnRButtonDown(UINT nFlags,CPoint pt)
{
CDC *dc=GetDC();//khoi tao ngu canh thiet bi
char s[10];
sprintf(s,"[%d,%d]",pt.x,pt.y); //lay toa do chuot
dc->TextOut(pt.x,pt.y,s);//viet toa do ra man hinh
oldPoint=pt;//lua toa do lai
}
//xu ly su kien nha chuot trai
void CMyWin::OnRButtonUp(UINT nFlags,CPoint point)
{
char s1[20];
m_memDC.SelectObject(&b);//chon but
m_memDC.SelectObject(&p);//chon choi to
sprintf(s1,"[%d,%d]",oldPoint.x,oldPoint.y); //lay toa do
m_memDC.TextOut(oldPoint.x,oldPoint.y, s1);//ve len cs ao
}
Bạn đang đọc truyện trên: Truyen247.Pro