MFC 绘制移动小球问题

不知名的悲伤 2013-02-27 09:55:56
小球类

class ball  
{
public:
int y;
int x;
ball();
virtual ~ball();

};


对话框代码,主要是Onpaint,Onkeydown
class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDRAWDlg dialog

CDRAWDlg::CDRAWDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDRAWDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDRAWDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDRAWDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDRAWDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDRAWDlg, CDialog)
//{{AFX_MSG_MAP(CDRAWDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDRAWDlg message handlers

BOOL CDRAWDlg::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

return TRUE; // return TRUE unless you set the focus to a control
}

void CDRAWDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.




void myEllipse(CDC *dc, POINT ptCenter, UINT uRadius)
{
POINT ptLeftTop, ptRightBottom;

ptLeftTop.x = ptCenter.x - uRadius;
ptLeftTop.y = ptCenter.y - uRadius;
ptRightBottom.x = ptCenter.x + uRadius;
ptRightBottom.y = ptCenter.y + uRadius;

Ellipse(*dc, ptLeftTop.x, ptLeftTop.y,
ptRightBottom.x, ptRightBottom.y);
} 画圆函数。转换为半径,圆心。

int r=20;
void CDRAWDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
int x=0;
CDialog::OnPaint();
CDC *pDC =GetDC();
CBrush brushColor(RGB(255,255,0));//填充的颜色
CPen penColor;
penColor.CreatePen(PS_SOLID,1,RGB(0,64,128));//边框颜色
pDC->SelectObject(penColor);
pDC->SelectObject(brushColor);


CPoint p(b.x,b.y);
myEllipse(pDC,p,r);
brushColor.DeleteObject();
penColor.DeleteObject();
ReleaseDC(pDC);

}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDRAWDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}



void CDRAWDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default

CDialog::OnKeyDown(nChar, nRepCnt, nFlags);

switch(nChar)

{

case VK_LEFT://按下左键

b.x=b.x-10;

UpdateWindow();

break;

case VK_RIGHT://按下右键
b.x=b.x+10;
UpdateWindow();

break;

case VK_UP://按下上键

b.y=b.y+10;
UpdateWindow();

break;

case VK_DOWN:// 按下下键

b.y=b.y-10;
UpdateWindow();

break;

default:

break;

}

}


编译通过,运行按键不移动。将UpdateWindow(); 换invalidate(true)也没用。
...全文
376 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimette 2013-02-28
  • 打赏
  • 举报
回复
UpdateWindow 是你自己写的?
shen_wei 2013-02-28
  • 打赏
  • 举报
回复
int nPosX,nPosY;
void CCMoveBallView::OnDraw(CDC* pDC)
{
	CCMoveBallDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	
	CBrush bush(RGB(255,0,0));
	pDC->SelectObject(bush);
	CPen pen(PS_SOLID,2,RGB(255,0,0));
	pDC->SelectObject(pen);
	pDC->Ellipse(CRect(nPosX,nPosY,nPosX+20,nPosY+20));
}

void CCMoveBallView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	switch(nChar)
	{
	case VK_UP:
			 nPosY -=10;
			 break;
	case VK_DOWN:
			 nPosY +=10;
			 break;
	case VK_LEFT:
		nPosX -=10;
			 break;
	case VK_RIGHT:
		nPosX +=10;
		break;
	}
	this->Invalidate();

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

这个是在当文档中实现的。。。你可以参考!!
不知名的悲伤 2013-02-27
  • 打赏
  • 举报
回复
OnPaint()
CDC *pDC =GetDC();
    CBrush brushColor(RGB(255,255,0));//填充的颜色
    CPen penColor;
    penColor.CreatePen(PS_SOLID,1,RGB(0,64,128));//边框颜色
    pDC->SelectObject(penColor);
    pDC->SelectObject(brushColor);
 
    OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 


CPoint p(b.x,b.y);
myEllipse(pDC,p,r);
    brushColor.DeleteObject();
   penColor.DeleteObject();
  ReleaseDC(pDC);
myEllipse(pDC,p,r);
void myEllipse(CDC *dc, POINT ptCenter, UINT uRadius)
{
POINT ptLeftTop, ptRightBottom;
 
ptLeftTop.x = ptCenter.x - uRadius;
ptLeftTop.y = ptCenter.y - uRadius;
ptRightBottom.x = ptCenter.x + uRadius;
ptRightBottom.y = ptCenter.y + uRadius;
 
Ellipse(*dc, ptLeftTop.x, ptLeftTop.y,
ptRightBottom.x, ptRightBottom.y);
}
void CDRAWDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
    // TODO: Add your message handler code here and/or call default
     
    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
 
  switch(nChar)
 
       {
 
       case VK_LEFT://按下左键
 
              b.x=b.x-10;
              
UpdateWindow();
 
              break;    
 
       case VK_RIGHT://按下右键
               b.x=b.x+10;
            UpdateWindow();
 
              break;
 
       case VK_UP://按下上键
 
               b.y=b.y+10;
UpdateWindow();
 
              break;
 
       case VK_DOWN:// 按下下键
 
              b.y=b.y-10;
UpdateWindow();
 
              break;
 
       default:
 
              break;
 
       }
 
}

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧