小弟刚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;
}
我还没有定义它的对象,编译的时候可以通过,但是不能执行,不知道为什么,请大家给点意见.
我定义了一个直线类.
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;
}
我还没有定义它的对象,编译的时候可以通过,但是不能执行,不知道为什么,请大家给点意见.
解决方案 »
- 今夜难眠!请教RegisterWindowMessage函数的用法!最好给个实例!
- 请教关于Win32下ListBox双击消息(LBN_DBLCLK)的问题
- 封装一个用ADO访问数据库的DLL,调试出错
- 如何按频率向串口发送数据?
- 请问如何使用VC撰写对一个文件加密和解密的程序
- ATL不使用MFC,Edit,Tree, List等控件对应哪些类啊?
- 如何在vc的ADO编程中对数据库进行导入导出的操作,做过的一定觉得很简单,100分!!!
- 谁有pop3原代码?
- vc++对数据库编程的书为什么这么少?
- 关于服务进程的问题--服务进程弹出的错消息怎么获取??
- Visual stdio 2005 编译遇到的const char问题
- 求DirectShow相关书籍
这个事件中用LineToOnLButtonDown
这个事件中用MoveTo
还是不行啊,我刚改了,也试了,就是不行,你看是不是别的原因,谢谢.
再在OnLButtonUp中 moveto(成员变量) ,lineto(Point)
下面是我写的一个画带箭头直线的代码,给你看看:// 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);
}
{
// 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
}