⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 38view.cpp

📁 画直线
💻 CPP
字号:
// 38View.cpp : implementation of the CMy38View class
//

#include "stdafx.h"
#include "38.h"

#include "brush.h"
#include "pendlg.h"
#include "38Doc.h"
#include "38View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy38View

IMPLEMENT_DYNCREATE(CMy38View, CView)

BEGIN_MESSAGE_MAP(CMy38View, CView)
	//{{AFX_MSG_MAP(CMy38View)
	ON_COMMAND(ID_LINE, OnLine)
	ON_UPDATE_COMMAND_UI(ID_LINE, OnUpdateLine)
	ON_COMMAND(ID_RECTANGLE, OnRectangle)
	ON_UPDATE_COMMAND_UI(ID_RECTANGLE, OnUpdateRectangle)
	ON_COMMAND(ID_ELLIPSE, OnEllipse)
	ON_UPDATE_COMMAND_UI(ID_ELLIPSE, OnUpdateEllipse)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_PEN, OnPen)
	ON_COMMAND(ID_BRUSH, OnBrush)
	ON_COMMAND(ID_COLOR, OnColor)
	ON_WM_SETCURSOR()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMy38View construction/destruction

CMy38View::CMy38View()
{
	// TODO: add construction code here
    n_style="PS_SOLID";
	n_width=1;
	n_color=RGB(0,0,0);
	n_type=0;                                                                                                                                                                                                                                                                                                                                         
	n_brush=0;
	n_hatch="HS_BDIAGONAL";
	n_solid=0;
}

CMy38View::~CMy38View()
{
}

BOOL CMy38View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy38View drawing

void CMy38View::OnDraw(CDC* pDC)
{
	CMy38Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMy38View printing

BOOL CMy38View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMy38View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMy38View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMy38View diagnostics

#ifdef _DEBUG
void CMy38View::AssertValid() const
{
	CView::AssertValid();
}

void CMy38View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMy38Doc* CMy38View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy38Doc)));
	return (CMy38Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMy38View message handlers

void CMy38View::OnLine() 
{
	// TODO: Add your command handler code here
	n_type=1;
	HICON hicon=AfxGetApp()->LoadIcon(IDI_LINE);
	AfxGetMainWnd()->SetIcon(hicon,TRUE);
}

void CMy38View::OnUpdateLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(n_type==1);
}

void CMy38View::OnRectangle() 
{
	// TODO: Add your command handler code here
	n_type=2;
	HICON hicon=AfxGetApp()->LoadIcon(IDI_RECTANGLE);
	AfxGetMainWnd()->SetIcon(hicon,TRUE);
}

void CMy38View::OnUpdateRectangle(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(n_type==2);
}

void CMy38View::OnEllipse() 
{
	// TODO: Add your command handler code here
	n_type=3;
	HICON hicon=AfxGetApp()->LoadIcon(IDI_ELLIPSE);
	AfxGetMainWnd()->SetIcon(hicon,TRUE);
}

void CMy38View::OnUpdateEllipse(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(n_type==3);
}

void CMy38View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	now_point=point;
	old_point=point;
	SetCapture();
	CView::OnLButtonDown(nFlags, point);
}

void CMy38View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC  *pDC;
CPen newpen;
CBrush newbsh;
CBrush *oldbsh;
int ndrawmode;
if(nFlags==MK_LBUTTON)
{
 if(n_style=="PS_SOLID")
   n_style_int=PS_SOLID;
 else if (n_style=="PS_DASH")
   n_style_int=PS_DASH;
 else if (n_style="PS_DASHDOT")
   n_style_int=PS_DASHDOT;
 else if (n_style="PS_DOT")
   n_style_int=PS_DOT;
 else 
 n_style_int=PS_SOLID;

if(n_style_int!=PS_SOLID)
 n_width=1;


if(n_hatch=="HS_BDIAGONAL")
   n_hatch_int=HS_BDIAGONAL;
 else if (n_hatch=="HS_CROSS")
   n_hatch_int=HS_CROSS;
 else if (n_hatch="HS_DIAGCROSS")
   n_hatch_int=HS_DIAGCROSS;
 else if (n_hatch="HS_FDIAGONAL")
   n_hatch_int=HS_FDIAGONAL;
 else if (n_hatch="HS_HORIZONTAL")
   n_hatch_int=HS_HORIZONTAL;
 else if (n_hatch="HS_VERTICAL")
   n_hatch_int=HS_VERTICAL;
 else 
 n_hatch_int=HS_BDIAGONAL;


if(n_solid)
 {
if(n_brush==1)
newbsh.CreateHatchBrush(n_hatch_int,n_color);
else
newbsh.CreateSolidBrush(n_color);
}

pDC=GetDC();
newpen.CreatePen(n_style_int,n_width,n_color);
CPen *oldpen=pDC->SelectObject(&newpen);
if(n_solid)
oldbsh=pDC->SelectObject(&newbsh);
CRect n_rect(old_point,point);
switch(n_type)
{
  case 1:
  pDC->MoveTo(now_point);
  pDC->LineTo(point);
  break;
  case 2:
  ndrawmode=pDC->GetROP2();
  pDC->SetROP2(R2_WHITE);
  pDC->Rectangle(old_point.x,old_point.y,now_point.x,now_point.y);
  pDC->SetROP2(ndrawmode);
  pDC->Rectangle(&n_rect);
  break;
  
  case 3:
  ndrawmode=pDC->GetROP2();
  pDC->SetROP2(R2_WHITE);
  pDC->Ellipse(old_point.x,old_point.y,now_point.x,now_point.y);
  pDC->SetROP2(ndrawmode);
  pDC->Ellipse(&n_rect);
  break;

  default:
  break;
}
   now_point=point;
   pDC->SelectObject(oldpen);
  if(n_solid)
   pDC->SelectObject(oldbsh);
   ReleaseDC(pDC);
}

	CView::OnMouseMove(nFlags, point);
}

void CMy38View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	now_point=point;
	ReleaseCapture();
	CView::OnLButtonUp(nFlags, point);
}

void CMy38View::OnPen() 
{
	// TODO: Add your command handler code here
	Cpendlg pendlg;
	pendlg.m_style=n_style;
	pendlg.m_width=n_width;
	if(pendlg.DoModal()==IDOK)
	{
		n_style=pendlg.m_style;
		n_width=pendlg.m_width;
	}
}

void CMy38View::OnBrush() 
{
	// TODO: Add your command handler code here
	Cbrush brush;
	if(brush.DoModal()==IDOK)
	{
		n_brush=brush.m_solid;
		n_hatch=brush.m_style;
		n_solid=1;
	}
}

void CMy38View::OnColor() 
{
	// TODO: Add your command handler code here
	CColorDialog c_dlg;
	if(c_dlg.DoModal()==IDOK)
	{
		n_color=c_dlg.GetColor();
	}
}

BOOL CMy38View::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	HCURSOR  newcursor;
	switch(n_type)
	{
	case 1:
		newcursor=AfxGetApp()->LoadCursor(IDC_LINE);
		SetCursor(newcursor);
		break;
	case 2:
        newcursor=AfxGetApp()->LoadCursor(IDC_RECTANGLE);
		SetCursor(newcursor);
		break;
	case 3:
        newcursor=AfxGetApp()->LoadCursor(IDC_ELLIPSE);
		SetCursor(newcursor);
		break;
    default:
		newcursor=AfxGetApp()->LoadStandardCursor(IDC_ARROW);
		SetCursor(newcursor);
		break;
	}
	 return TRUE;
		//return CView::OnSetCursor(pWnd, nHitTest, message);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -