小弟刚VC不久,遇到的问题真不少.下面就是一个画直线的类出的问题.
我定义了一个直线类.
DrawLine.h的代码:#if !defined(AFX_DRAWLINE_H__1C5A6622_9C9C_448D_A1F2_5A205B666E31__INCLUDED_)
#define AFX_DRAWLINE_H__1C5A6622_9C9C_448D_A1F2_5A205B666E31__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CDrawLine  
{public:
CDrawLine();
virtual ~CDrawLine();
public:
bool sign;
CDC* pDC;
int type;
void OnLButtonDown(UINT nFlags,CPoint Point);
void OnLButtonUp(UINT nFlags,CPoint Point);
void OnMouseMove(UINT nFlags,CPoint Point);
void OnRButtonDown(UINT nFlags,CPoint Point);};#endif // !defined(AFX_DRAWLINE_H__1C5A6622_9C9C_448D_A1F2_5A205B666E31__INCLUDED_)
下面是DrawLine.cpp里面的代码:#include "stdafx.h"
#include "VCCAD.h"
#include "DrawLine.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CView *pView;//我付给了它当前的视类的指针
CDrawLine::CDrawLine()
{
type=1;
sign=0;
pDC=pView->GetDC();}CDrawLine::~CDrawLine()
{}
void  CDrawLine::OnLButtonDown(UINT nFlags,CPoint Point)
{
sign=1;
pDC->LineTo(Point);
}
void CDrawLine::OnLButtonUp(UINT nFlags,CPoint Point)
{
sign=0;
}
void CDrawLine::OnMouseMove(UINT nFlags,CPoint Point)
{   
if(sign==1)
pDC->MoveTo(Point);}void CDrawLine::OnRButtonDown(UINT nFlags,CPoint Point)
{
sign=0;
}
我还没有定义它的对象,编译的时候可以通过,但是不能执行,不知道为什么,请大家给点意见.

解决方案 »

  1.   

    OnMouseMove
    这个事件中用LineToOnLButtonDown
    这个事件中用MoveTo
      

  2.   

    回wanilyer(缘随影去)
    还是不行啊,我刚改了,也试了,就是不行,你看是不是别的原因,谢谢.
      

  3.   

    你的CDrawLine类对象是CView的成员吧,那她构造的时候pView是否是啥状况,没见你给pView赋值啊,不知道你在那儿的extern啥意图,在pDC=pView->GetDC();这句加个断点监视一下看看pView吧,还有,自己写类没必要起名OnLButtonDown吧,扎一看还以为是CView个人观点
      

  4.   

    对了,extern CView *pView的意思图就是要取得当前的视类的指针,然后好在视类窗口上画图,我这样想是不是有问题??这只是简单的一个类,按照这个思想我还可以设计其它的作图类,不过好像有问题。
      

  5.   

    老大!你有没搞错啊!首先你的流程就不对,更别提实现你的效果了!画直线 首先要在你的类里申明一个成员变量,OnLButtonDown记录画直线的起始坐标;
    再在OnLButtonUp中 moveto(成员变量) ,lineto(Point)
      

  6.   

    我也是刚学vc++,今后多探讨探讨。
    下面是我写的一个画带箭头直线的代码,给你看看:// DrawMainView.cpp : implementation of the CDrawMainView class
    //#include "stdafx.h"
    #include "DrawMain.h"#include "DrawMainDoc.h"
    #include "DrawMainView.h"
    #include <MATH.H>
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endifconst double PI=3.1415926;
    const int ArrDgr=30;
    const int LineLen=30;
    /////////////////////////////////////////////////////////////////////////////
    // CDrawMainViewIMPLEMENT_DYNCREATE(CDrawMainView, CView)BEGIN_MESSAGE_MAP(CDrawMainView, CView)
    //{{AFX_MSG_MAP(CDrawMainView)
    ON_WM_CREATE()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_MOUSEMOVE()
    ON_WM_RBUTTONDOWN()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDrawMainView construction/destructionCDrawMainView::CDrawMainView()
    {
    // TODO: add construction code here
    BSign=FALSE;
    }CDrawMainView::~CDrawMainView()
    {
    }BOOL CDrawMainView::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
    }/////////////////////////////////////////////////////////////////////////////
    // CDrawMainView drawingvoid CDrawMainView::OnDraw(CDC* pDC)
    {
    CDrawMainDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    }/////////////////////////////////////////////////////////////////////////////
    // CDrawMainView printingBOOL CDrawMainView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    // default preparation
    return DoPreparePrinting(pInfo);
    }void CDrawMainView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add extra initialization before printing
    }void CDrawMainView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    }/////////////////////////////////////////////////////////////////////////////
    // CDrawMainView diagnostics#ifdef _DEBUG
    void CDrawMainView::AssertValid() const
    {
    CView::AssertValid();
    }void CDrawMainView::Dump(CDumpContext& dc) const
    {
    CView::Dump(dc);
    }CDrawMainDoc* CDrawMainView::GetDocument() // non-debug version is inline
    {
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawMainDoc)));
    return (CDrawMainDoc*)m_pDocument;
    }
    #endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
    // CDrawMainView message handlersint CDrawMainView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;
    // TODO: Add your specialized creation code here
    m_OrgPot.x=0;
    m_OrgPot.y=0;
    return 0;
    }
     void CDrawMainView::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
        m_OrgPot=point;
    m_OldPot=point;
    BSign=TRUE;
    CView::OnLButtonDown(nFlags, point);
    }
      

  7.   

    ////续上void CDrawMainView::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    /*CDC *dc;
    dc=GetDC();
    dc->MoveTo(m_OrgPot);
        dc->LineTo(point);
    ReleaseDC(dc);*/
    //CClientDC dc(this);
    //dc.MoveTo(m_OrgPot);
    //dc.LineTo(point);
    BSign=FALSE;
    CView::OnLButtonUp(nFlags, point);
    }void CDrawMainView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    if (BSign) 
    {
    CClientDC dc(this);  
    dc.SetROP2(R2_NOTXORPEN);
    CPen pen(PS_SOLID,4,RGB(0,255,0)); 
    dc.SelectObject(&pen); 
    dc.MoveTo(m_OrgPot);
    dc.LineTo(m_OldPot);
    double x,y,degree;
    CPoint ptL,ptR; 
    if (m_OrgPot.x<=m_OldPot.x)
    {
    if (m_OrgPot.y<=m_OldPot.y)

    x=double(m_OldPot.x-m_OrgPot.x);
    y=double(m_OldPot.y-m_OrgPot.y);
    degree=atan(y/x)+(ArrDgr*PI/180); 
    ptR.x=m_OldPot.x-int(LineLen*cos(degree)+0.5);
    ptR.y=m_OldPot.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptR);
    degree=atan(y/x)-(ArrDgr*PI/180);
    ptL.x=m_OldPot.x-int(LineLen*cos(degree)+0.5);
    ptL.y=m_OldPot.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptL);
    }
    else
    {
    x=double(m_OldPot.x-m_OrgPot.x);
    y=double(m_OrgPot.y-m_OldPot.y);
    degree=atan(y/x)+(ArrDgr*PI/180); 
    ptR.x=m_OldPot.x-int(LineLen*cos(degree)+0.5);
    ptR.y=m_OldPot.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptR);
    degree=atan(y/x)-(ArrDgr*PI/180);
    ptL.x=m_OldPot.x-int(LineLen*cos(degree)+0.5);
    ptL.y=m_OldPot.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptL);
    }
    }
    else
    {
    if (m_OrgPot.y<=m_OldPot.y)
    {
    x=double(m_OrgPot.x-m_OldPot.x);
    y=double(m_OldPot.y-m_OrgPot.y);
    degree=atan(y/x)-(ArrDgr*PI/180); 
    ptR.x=m_OldPot.x+int(LineLen*cos(degree)+0.5);
    ptR.y=m_OldPot.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptR);
    degree=atan(y/x)+(ArrDgr*PI/180);
    ptL.x=m_OldPot.x+int(LineLen*cos(degree)+0.5);
    ptL.y=m_OldPot.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptL);
    }
    else
    {
    x=double(m_OrgPot.x-m_OldPot.x);
    y=double(m_OrgPot.y-m_OldPot.y);
    degree=atan(y/x)-(ArrDgr*PI/180); 
    ptR.x=m_OldPot.x+int(LineLen*cos(degree)+0.5);
    ptR.y=m_OldPot.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptR);
    degree=atan(y/x)+(ArrDgr*PI/180);
    ptL.x=m_OldPot.x+int(LineLen*cos(degree)+0.5);
    ptL.y=m_OldPot.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(m_OldPot);
    dc.LineTo(ptL);
    }
    }  
    dc.MoveTo(m_OrgPot);
    dc.LineTo(point); 
    if (m_OrgPot.x<=point.x)
    {
    if (m_OrgPot.y<=point.y)
    {
    x=double(point.x-m_OrgPot.x);
    y=double(point.y-m_OrgPot.y);
    degree=atan(y/x)+(ArrDgr*PI/180); 
    ptR.x=point.x-int(LineLen*cos(degree)+0.5);
    ptR.y=point.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptR);
    degree=atan(y/x)-(ArrDgr*PI/180);
    ptL.x=point.x-int(LineLen*cos(degree)+0.5);
    ptL.y=point.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptL);

    }
    else
    {
    x=double(point.x-m_OrgPot.x);
    y=double(m_OrgPot.y-point.y);
    degree=(ArrDgr*PI/180)+atan(y/x);
    ptR.x=point.x-int(LineLen*cos(degree)+0.5);
    ptR.y=point.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptR); 
    degree=atan(y/x)-(ArrDgr*PI/180); 
    ptL.x=point.x-int(LineLen*cos(degree)+0.5);
    ptL.y=point.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptL);
    }
    }
    else
    {
    if (m_OrgPot.y<=point.y)
    {
    x=double(m_OrgPot.x-point.x);
    y=double(point.y-m_OrgPot.y);
    degree=atan(y/x)-(ArrDgr*PI/180); 
    ptR.x=point.x+int(LineLen*cos(degree)+0.5);
    ptR.y=point.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptR);
    degree=atan(y/x)+(ArrDgr*PI/180);
    ptL.x=point.x+int(LineLen*cos(degree)+0.5);
    ptL.y=point.y-int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptL);
    }
    else
    {
    x=double(m_OrgPot.x-point.x);
    y=double(m_OrgPot.y-point.y);
    degree=atan(y/x)-(ArrDgr*PI/180); 
    ptR.x=point.x+int(LineLen*cos(degree)+0.5);
    ptR.y=point.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptR);
    degree=atan(y/x)+(ArrDgr*PI/180);
    ptL.x=point.x+int(LineLen*cos(degree)+0.5);
    ptL.y=point.y+int(LineLen*sin(degree)+0.5);
    dc.MoveTo(point);
    dc.LineTo(ptL);
    }
    }
    m_OldPot=point;
    }
    CView::OnMouseMove(nFlags, point);
    }void CDrawMainView::OnRButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
      }