一个画图程序,其他的都能实现,为什么圆的实现不了?

super_busters 2014-09-24 11:25:13
CCircle.h:
#pragma once
#include "Element.h"
class CCircle :
public CElement
{
public:

virtual ~CCircle(void);
virtual void Draw(CDC* pDC) override; //Function to display a circle
//Constructor for a circle object
CCircle(const CPoint& start, const CPoint& end, COLORREF color);
protected:
CPoint m_BottomRight; //Bottom-right point for defining rectangle
CCircle(void); //Default constructor-should not be used
};

CCircle.cpp:
#include "stdafx.h"
#include "Circle.h"
#include "cmath"

//Construcotr for a cicle object
CCircle::CCircle(const CPoint& start, const CPoint& end, COLORREF color) : CElement(start,color)
{
//Calculate the radius using floating-Point values
//because that is required by sqrt()function (in cmath)
long radius = static_cast<long>(sqrt(static_cast<double>((end.x - start.x)*(end.x - start.x) + (end.y - start.y)*(end.y - start.y))));
if (radius < 1L) radius = 1L; //Circle radius must be >=1
//Define left-top and right-bottom points of rectangle for MM_TEXT mode

m_StartPoint = CPoint(start.x - radius, start.y - radius);
m_BottomRight = CPoint(start.x + radius, start.y + radius);
//Define the enclosing rectangle
m_EnclosingRect = CRect(m_StartPoint.x, m_StartPoint.y,
m_BottomRight.x, m_BottomRight.y);
m_EnclosingRect.InflateRect(m_PenWidth, m_PenWidth);
}


CCircle::~CCircle()
{
}
//Draw a circle
void CCircle::Draw(CDC* pDC)
{
//Creat a pen for this object and initialize it
CPen aPen;
CreatePen(aPen);
CPen* pOldPen = pDC->SelectObject(&aPen); //Select a pen
//Select a null brush
CBrush* pOldBrush = dynamic_cast<CBrush*>(pDC->SelectStockObject(NULL_BRUSH));
//Now draw the circle
pDC->Ellipse(m_StartPoint.x, m_StartPoint.y, m_BottomRight.x, m_BottomRight.y);
pDC->SelectObject(pOldPen); //Restore the old pen
pDC->SelectObject(pOldBrush); //Restore the old Brush
}

求大牛来帮忙啊,本人新手,刚刚学习MFC
...全文
565 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
hemmingway 2014-09-25
  • 打赏
  • 举报
回复
用GDI+画图吧。。。
relaxisland 2014-09-25
  • 打赏
  • 举报
回复
引用 16 楼 super_busters 的回复:
void CCircle::Draw(CDC* pDC) 没有被调用!怎么回事?
那就是调用的地方出问题了吧 那部分代码,没有贴上来吧
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
MSDN98\SAMPLES\VC98\MFC\TUTORIAL\SCRIBBLE\STEP1~7\*.*
super_busters 2014-09-25
  • 打赏
  • 举报
回复
还是没成功,大家有没有其他实现画圆的方法?分享一下
-小仙- 2014-09-25
  • 打赏
  • 举报
回复
类是CElement,析构器等实现确实CCircle, 你的代码太乱了,为什么不按照C++风格贴代码?

//class CMFCApplication1View : public CView

void CMFCApplication1View::OnDraw(CDC* pDC)
{
	CMFCApplication1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO:  在此处为本机数据添加绘制代码
	
	pDC->MoveTo(50, 50);
	pDC->LineTo(100, 100);

	CRect rc(CPoint(100, 100), CSize(100, 100));
	pDC->Ellipse(rc);
}
上面代码正常, 在按照步骤F9设置断点,或者添加打印TRACE(_T("\r\n "));跟踪!
super_busters 2014-09-25
  • 打赏
  • 举报
回复
引用 24 楼 zhao4zhong1 的回复:
建议楼主还是老老实实跟着20楼提供的MSDN98中的MFC循序渐进学习案例过一遍。
谢谢这么认真的回复 我还是从头开始认真理解吧
schlafenhamster 2014-09-25
  • 打赏
  • 举报
回复
void CCircle::Draw(CDC* pDC) 没有被调用!怎么回事? 按逻辑检查.
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
仅供参考
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    PAINTSTRUCT ps;
    HDC hdc;
    HFONT hfont,ohfont;
    RECT r;
    COLORREF oc;

    switch(message) {
    case WM_CLOSE://按Alt+F4退出
        PostQuitMessage(0);
        break;
    case WM_PAINT:
        BeginPaint(hWnd, &ps);
        hdc = ps.hdc; // the device context to draw in
        GetClientRect(hWnd, &r); // Obtain the window's client rectangle
        hfont = CreateFont(240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
        ohfont=(HFONT)SelectObject(hdc,hfont);
        oc=SetTextColor(hdc,0x00C080FF);
        SetBkMode(hdc, TRANSPARENT);
        TextOut(hdc,r.left+r.right/2-720, r.top+r.bottom/2-120,"最短画图程序",12);
        SelectObject(hdc,ohfont);
        SetTextColor(hdc,oc);
        DeleteObject(hfont);
        EndPaint(hWnd, &ps);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    MSG msg             = {0};
    WNDCLASS wc         = {0};
    HBRUSH hbrh;
    hbrh=CreateSolidBrush(0x00000000);
    wc.lpfnWndProc      = WndProc;
    wc.hInstance        = hInstance;
    wc.hbrBackground    = hbrh;
    wc.lpszClassName    = "minwindowsapp";
    wc.hCursor          = LoadCursor(NULL,IDC_ARROW);
    if( FAILED(RegisterClass(&wc)) ) return 1;

    if(FAILED(CreateWindow(wc.lpszClassName,
                        "Minimal Windows Application",
                        WS_POPUP|WS_VISIBLE,
                        0,
                        0,
                        GetSystemMetrics(SM_CXSCREEN),
                        GetSystemMetrics(SM_CYSCREEN),
                        0,
                        0,
                        hInstance,
                        NULL)))
        return 2;

    while( GetMessage( &msg, NULL, 0, 0 ) > 0 ) {
        DispatchMessage( &msg );
    }
    DeleteObject(hbrh);
    return 0;
}
malone1 2014-09-25
  • 打赏
  • 举报
回复
还有人学MFC
不败的拿破仑 2014-09-25
  • 打赏
  • 举报
回复
引用 16 楼 super_busters 的回复:
void CCircle::Draw(CDC* pDC) 没有被调用!怎么回事?
你连程序都没被调用 那还能画什么。。 建议你直接新建一个对话框工程,在onpaint函数里面话就行了。还有如果不想对图形有深入的了解的话,直接上gdi+,opencv吧,一两句话的事情。应该从简到难来学,反过来在深入这些绘图函数的内部
赵4老师 2014-09-25
  • 打赏
  • 举报
回复
建议楼主还是老老实实跟着20楼提供的MSDN98中的MFC循序渐进学习案例过一遍。
  • 打赏
  • 举报
回复
呵呵,就不是感觉用MFC别手别脚的?给你来一段直接用windows API写的。

void DrawCircle(HDC&hdc, int R,COLORREF color=RGB(255,0,255))
{
	HPEN hPen = CreatePen(PS_COSMETIC, 1, color);
	SelectObject(hdc, hPen);
	HBRUSH hBrush = CreateSolidBrush(color);
	SelectObject(hdc, hBrush);
	Ellipse(hdc,0,0,2*R,2*R);
	DeleteObject(hBrush);
        DeleteObject(hPen);
}
super_busters 2014-09-24
  • 打赏
  • 举报
回复
long radius = static_cast<long>(sqrt(static_cast<double>((end.x - start.x)*(end.x - start.x) + (end.y - start.y)*(end.y - start.y)))); 算的就是半径呀~!
super_busters 2014-09-24
  • 打赏
  • 举报
回复
pDC->Ellipse(10,10,20,20) 不管用,没有显示 会不会是Draw放的位置不对?
赵4老师 2014-09-24
  • 打赏
  • 举报
回复
CCircle.h: #pragma once #include "Element.h" class CCircle : public CElement { public: virtual ~CCircle(void); virtual void Draw(CDC* pDC) override; //Function to display a circle //Constructor for a circle object CCircle(const CPoint& start, const CPoint& end, COLORREF color); protected: CPoint m_BottomRight; //Bottom-right point for defining rectangle CCircle(void); //Default constructor-should not be used }; CCircle.cpp: #include "stdafx.h" #include "Circle.h" #include "cmath" //Construcotr for a cicle object CCircle::CCircle(const CPoint& start, const CPoint& end, COLORREF color) : CElement(start,color) { //Calculate the radius using floating-Point values //because that is required by sqrt()function (in cmath) long radius = static_cast<long>(sqrt(static_cast<double>((end.x - start.x)*(end.x - start.x) + (end.y - start.y)*(end.y - start.y)))); if (radius < 1L) radius = 1L; //Circle radius must be >=1 //Define left-top and right-bottom points of rectangle for MM_TEXT mode m_StartPoint = CPoint(start.x - radius, start.y - radius); m_BottomRight = CPoint(start.x + radius, start.y + radius); //Define the enclosing rectangle m_EnclosingRect = CRect(m_StartPoint.x, m_StartPoint.y, m_BottomRight.x, m_BottomRight.y); m_EnclosingRect.InflateRect(m_PenWidth, m_PenWidth); } CCircle::~CCircle() { } //Draw a circle void CCircle::Draw(CDC* pDC) { //Creat a pen for this object and initialize it CPen aPen; CreatePen(aPen); CPen* pOldPen = pDC->SelectObject(&aPen); //Select a pen //Select a null brush CBrush* pOldBrush = dynamic_cast<CBrush*>(pDC->SelectStockObject(NULL_BRUSH)); //Now draw the circle pDC->Ellipse(10,10,20,20);//先看这句管用不 pDC->Ellipse(m_StartPoint.x, m_StartPoint.y, m_BottomRight.x, m_BottomRight.y); pDC->SelectObject(pOldPen); //Restore the old pen pDC->SelectObject(pOldBrush); //Restore the old Brush }
ri_aje 2014-09-24
  • 打赏
  • 举报
回复
执行这句的时候: pDC->Ellipse(m_StartPoint.x, m_StartPoint.y, m_BottomRight.x, m_BottomRight.y); 参数值都是多少?对应你的绘制窗口,看起来正确吗? 另外,如果你是用外接矩形画椭圆的话,这句 long radius = static_cast<long>(sqrt(static_cast<double>((end.x - start.x)*(end.x - start.x) + (end.y - start.y)*(end.y - start.y)))); 算的是直径,不是半径。
super_busters 2014-09-24
  • 打赏
  • 举报
回复
可以编译通过也没有报错,程序也可以运行。就是画不出圆。代码注释也发现不了错误才来救助的
赵4老师 2014-09-24
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。 请判断每个函数的返回值。
super_busters 2014-09-24
  • 打赏
  • 举报
回复
qt??现在主流的应该还是MFC吧?QT学起来更简单么?
fyxtc 2014-09-24
  • 打赏
  • 举报
回复
还是早点放弃MFC吧...ms自己都放弃了...还不如学qt来得好
加载更多回复(8)

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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