MFC学习笔记——void CLi11View::OnDraw(CDC* /*pDC*/)






打开项目工作区中的ClassView类视图,单击CLi2_1View类左边的“+”展开视图类,双击打开OnDraw()函数,并添加代码。

void CLi2_1View::OnDraw(CDC*pDC)

{

  CLi2_1Doc* pDoc= GetDocument();

  ASSERT_VALID(pDoc);

  // TODO: add draw codefor native data here

  MessageBox("这是一个单文档应用程序!",

                                     "消息框",

                            MB_ICONEXCLAMATION|MB_OKCANCEL);


编译时出现——
1>e:\mfc\li1-1\li1-1\li1-1view.cpp(73): error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“const char [25]”转换为“LPCTSTR”
1>          与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换

解决办法——

(1)

MessageBox(TEXT("ddd"));

(2)

在项目属性->常规中,把Uicode改成多字符段。以上两种方法用一个即可
VC mfc单文档中代码如下void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); for (int i=0;i<points.size();i++){ color=RGB(rand()%256,rand()%256,rand()%256); r=rand()%46+5; br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(points[i].x-r,points[i].y-r,points[i].x+r,points[i].y+r); br.DeleteObject(); } // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center=point; r=rand()%46+5;//r=5~50 color=RGB(rand()%256,rand()%256,rand()%256); points.push_back(center); SetTimer(1,200,NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for(int i=0;i<points.size();i++) { points[i].y-=5; if(points[i].y<-r) { points.erase(points.begin()+i); i--; } } } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1){ RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); },运行效果中圆在上升过程中颜色和大小不停的变换,应怎么修改此代码使得圆在上升过程中的大小和颜色不会变换,完整步骤及代码
最新发布
05-29
要使圆在上升过程中大小和颜色不会变换,可以将颜色和大小的变化单独提取出来,放到循环外面进行定义,然后再在循环内部进行圆的上升操作。具体实现方式如下: 1. 在头文件中定义变量: ```cpp // 头文件中定义变量 COLORREF color; // 圆的颜色 int r; // 圆的半径 ``` 2. 在`OnDraw`函数中修改绘制圆的代码: ```cpp void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // 绘制圆 for (int i = 0;i < points.size();i++) { // 保持颜色和大小不变 color = RGB(0, 0, 255); // 蓝色 r = 20; // 绘制圆 CPen pen(PS_SOLID, 1, color); CBrush brush(color); pDC->SelectObject(&pen); pDC->SelectObject(&brush); pDC->Ellipse(points[i].x - r, points[i].y - r, points[i].x + r, points[i].y + r); } } ``` 在这个代码中,我们将颜色和大小的变化都删除了,直接将颜色设置为蓝色,大小设置为20。这样就可以让圆在上升过程中保持颜色和大小不变了。 完整修改后的代码如下: MyView.h文件: ```cpp #pragma once #include "MyDoc.h" #include <vector> class CMyView : public CView { protected: // create from serialization only CMyView() noexcept; DECLARE_DYNCREATE(CMyView) // Attributes public: CMyDoc* GetDocument() const; // Operations public: // Overrides public: virtual void OnDraw(CDC* pDC) override; virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual void OnInitialUpdate() override; // called first time after construct virtual void OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/) override; // Implementation public: virtual ~CMyView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: DECLARE_MESSAGE_MAP() public: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnTimer(UINT_PTR nIDEvent); void rise(); }; #ifndef _DEBUG // debug version in MyView.cpp inline CMyDoc* CMyView::GetDocument() const { return reinterpret_cast<CMyDoc*>(m_pDocument); } #endif ``` MyView.cpp文件: ```cpp #include "pch.h" #include "framework.h" #include "My.h" #include "MyDoc.h" #include "MyView.h" #ifdef _DEBUG #define new DEBUG_NEW #endif IMPLEMENT_DYNCREATE(CMyView, CView) BEGIN_MESSAGE_MAP(CMyView, CView) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_TIMER() END_MESSAGE_MAP() // CMyView construction/destruction CMyView::CMyView() noexcept { // TODO: add construction code here } CMyView::~CMyView() { } BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } // CMyView drawing void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // 绘制圆 for (int i = 0;i < points.size();i++) { // 保持颜色和大小不变 color = RGB(0, 0, 255); // 蓝色 r = 20; // 绘制圆 CPen pen(PS_SOLID, 1, color); CBrush brush(color); pDC->SelectObject(&pen); pDC->SelectObject(&brush); pDC->Ellipse(points[i].x - r, points[i].y - r, points[i].x + r, points[i].y + r); } } void CMyView::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class } void CMyView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: Add your specialized code here and/or call the base class } // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() const // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center = point; r = rand() % 46 + 5; color = RGB(rand() % 256, rand() % 256, rand() % 256); points.push_back(center); SetTimer(1, 200, NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for (int i = 0;i < points.size();i++) { points[i].y -= 5; if (points[i].y < -r) { points.erase(points.begin() + i); i--; } } } void CMyView::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default if (nIDEvent == 1) { RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); } ``` 这样修改后,圆在上升过程中的大小和颜色就不会变换了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值