作业内容:
1.编程实现消息测试程序,满足以下要求:
(1)按下键盘任意键,客户区中显示按键信息;
(2)单击鼠标左键,消息对话框中显示鼠标信息;
(3)假设鼠标右键失灵,用Ctrl+鼠标左键代替,消息对话框中显示鼠标信息;
(4)自定义WM_MY_MESSAGE消息,带50和100两个参数,由“?”键激活,客户区中显示相应信息。
2.编程实现SmallBall程序,满足以下要求:
(1)黑色小球,从左上角开始,45度方向移动,遇到边界反弹;
(2)通过新增的菜单项,调节小球的移动幅度;
(3)通过新增的工具栏按钮,控制小球停止与再次启动。
// Q756939View.cpp : implementation of the CQ756939View class
//
#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "Q756939.h"
#endif
#include "Q756939Doc.h"
#include "Q756939View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
UINT WM_MY_MESSAGE = WM_USER + 100;
// CQ756939View
IMPLEMENT_DYNCREATE(CQ756939View, CView)
BEGIN_MESSAGE_MAP(CQ756939View, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CQ756939View::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_CHAR()
ON_WM_LBUTTONUP()
ON_MESSAGE(WM_MY_MESSAGE, &CQ756939View::OnMyMessage)
END_MESSAGE_MAP()
// CQ756939View construction/destruction
CQ756939View::CQ756939View()
{
// TODO: add construction code here
}
CQ756939View::~CQ756939View()
{
}
BOOL CQ756939View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CQ756939View drawing
void CQ756939View::OnDraw(CDC* pDC)
{
CQ756939Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
pDC->TextOut(100, 100, m_str);
}
// CQ756939View printing
void CQ756939View::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CQ756939View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CQ756939View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CQ756939View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CQ756939View::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CQ756939View::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CQ756939View diagnostics
#ifdef _DEBUG
void CQ756939View::AssertValid() const
{
CView::AssertValid();
}
void CQ756939View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CQ756939Doc* CQ756939View::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CQ756939Doc)));
return (CQ756939Doc*)m_pDocument;
}
#endif //_DEBUG
// CQ756939View message handlers
void CQ756939View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CString presskey("Press key: ");
if ((nChar >= 'A' && nChar <= 'Z') || (nChar >= 'a' && nChar <= 'z') || (nChar >= '0' && nChar <= '9'))
m_str = presskey + CString((char)nChar);
else
{
if (nChar != 63)
{
m_str = presskey + CString((char)((nChar / 100) % 10 + '0'));
m_str = m_str + CString((char)((nChar / 10 % 10) + '0'));
m_str = m_str + CString((char)((nChar % 10) + '0'));
}
else
{
CView* pmfrm = ((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
pmfrm->SendMessage(WM_MY_MESSAGE, 100, 50);
}
}
GetDocument()->UpdateAllViews(NULL);
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CQ756939View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (GetKeyState(VK_CONTROL) < 0)
{
CString pressmouse("Press ctrl + left mouse button");
m_str = pressmouse;
}
else
{
CString pressmouse("Press left mouse button");
m_str = pressmouse;
}
GetDocument()->UpdateAllViews(NULL);
CView::OnLButtonUp(nFlags, point);
}
afx_msg LRESULT CQ756939View::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
CString mymsg("WM_MY_MESSAGE %d %d");
mymsg.Format(mymsg, wParam, lParam);
m_str = mymsg;
GetDocument()->UpdateAllViews(NULL);
return 0;
}
// Q756939_2View.h : interface of the CQ756939_2View class
//
#pragma once
class CQ756939_2View : public CView
{
protected: // create from serialization only
CQ756939_2View();
DECLARE_DYNCREATE(CQ756939_2View)
int predelta;
int delta;
int x;
int y;
int xd;
int yd;
int iscreated;
// Attributes
public:
CQ756939_2Doc* GetDocument() const;
// Operations
public:
// Overrides
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
// Implementation
public:
virtual ~CQ756939_2View();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
afx_msg void OnFilePrintPreview();
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnJian();
afx_msg void OnJia();
afx_msg void OnStart();
afx_msg void OnStop();
};
#ifndef _DEBUG // debug version in Q756939_2View.cpp
inline CQ756939_2Doc* CQ756939_2View::GetDocument() const
{ return reinterpret_cast<CQ756939_2Doc*>(m_pDocument); }
#endif
// Q756939_2View.cpp : implementation of the CQ756939_2View class
//
#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "Q756939_2.h"
#endif
#include "Q756939_2Doc.h"
#include "Q756939_2View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CQ756939_2View
IMPLEMENT_DYNCREATE(CQ756939_2View, CView)
BEGIN_MESSAGE_MAP(CQ756939_2View, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CQ756939_2View::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_TIMER()
ON_WM_CREATE()
ON_COMMAND(ID_JIAN, &CQ756939_2View::OnJian)
ON_COMMAND(ID_JIA, &CQ756939_2View::OnJia)
ON_COMMAND(ID_start, &CQ756939_2View::OnStart)
ON_COMMAND(ID_stop, &CQ756939_2View::OnStop)
END_MESSAGE_MAP()
// CQ756939_2View construction/destruction
CQ756939_2View::CQ756939_2View()
{
// TODO: add construction code here
x = 100;
y = 100;
xd = 1;
yd = 1;
delta = 30;
predelta = 0;
iscreated = false;
}
CQ756939_2View::~CQ756939_2View()
{
}
BOOL CQ756939_2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CQ756939_2View drawing
void CQ756939_2View::OnDraw(CDC* pDC)
{
CQ756939_2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
CRect rect(x - 100, y - 100, x + 100, y + 100);
pDC->Ellipse(&rect);
}
// CQ756939_2View printing
void CQ756939_2View::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CQ756939_2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CQ756939_2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CQ756939_2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CQ756939_2View::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CQ756939_2View::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CQ756939_2View diagnostics
#ifdef _DEBUG
void CQ756939_2View::AssertValid() const
{
CView::AssertValid();
}
void CQ756939_2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CQ756939_2Doc* CQ756939_2View::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CQ756939_2Doc)));
return (CQ756939_2Doc*)m_pDocument;
}
#endif //_DEBUG
// CQ756939_2View message handlers
void CQ756939_2View::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent == 123)
{
if (xd)
x += delta;
else
x -= delta;
if (yd)
y += delta;
else
y -= delta;
CRect rect;
GetClientRect(&rect);
if (x + 100 > rect.right) xd = !xd;
if (y + 100 > rect.bottom) yd = !yd;
if (x < 100) xd = !xd;
if (y < 100) yd = !yd;
GetDocument()->UpdateAllViews(NULL);
}
CView::OnTimer(nIDEvent);
}
int CQ756939_2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetTimer(123, 100, NULL);
iscreated = true;
return 0;
}
void CQ756939_2View::OnJian()
{
// TODO: Add your command handler code here
if (delta > 10)
delta -= 10;
}
void CQ756939_2View::OnJia()
{
// TODO: Add your command handler code here
delta += 10;
}
void CQ756939_2View::OnStart()
{
// TODO: Add your command handler code here
if (!iscreated)
{
SetTimer(123, 100, NULL);
iscreated = true;
}
}
void CQ756939_2View::OnStop()
{
// TODO: Add your command handler code here
if (iscreated)
{
KillTimer(123);
iscreated = false;
}
}