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

📄 mfc1view.cpp

📁 自己编的VC++程序
💻 CPP
字号:
// MFC1View.cpp : implementation of the CMFC1View class
//

#include "stdafx.h"
#include "MFC1.h"

#include "MFC1Doc.h"
#include "MFC1View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMFC1View

IMPLEMENT_DYNCREATE(CMFC1View, CView)

BEGIN_MESSAGE_MAP(CMFC1View, CView)
//{{AFX_MSG_MAP(CMFC1View)
ON_WM_LBUTTONDOWN()
ON_COMMAND(IDM_INIT, OnInit)
ON_WM_RBUTTONDOWN()
ON_WM_PAINT()
//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMFC1View construction/destruction

CMFC1View::CMFC1View()
{
	// TODO: add construction code here
	m_ileftcount=0;
	m_irightcount=0;
}

CMFC1View::~CMFC1View()
{
}

BOOL CMFC1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMFC1View drawing

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

/////////////////////////////////////////////////////////////////////////////
// CMFC1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMFC1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMFC1View message handlers

void CMFC1View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	/*	CRect rect;	//画两条交叉线
	GetClientRect(&rect);
	CClientDC dc(this);
	dc.MoveTo(rect.left,rect.top);
	dc.LineTo(rect.right,rect.bottom);
	dc.MoveTo(rect.right,rect.top);
	dc.LineTo(rect.left,rect.bottom);*/
	CClientDC dc(this);
	
	/*   
	CString   str,str1;  
    str1.Format("%ld",point.x);    
    MessageBox(str1);
    str.Format("%ld",point.y);    
    MessageBox(str);
	
	  dc.SetMapMode(MM_TEXT);		//默认情况下就是这种模式
	  dc.DPtoLP(&point);
	  str1.Format("%ld",point.x);    
	  MessageBox(str1);
	  str.Format("%ld",point.y);    
    MessageBox(str);*/
	
	CPen pen(PS_DASHDOTDOT,5,RGB(0,0,255));
	dc.SelectObject(&pen);
	CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	dc.SelectObject(pBrush);
	dc.MoveTo(point);
	dc.LineTo(point);
	m_ptLeft[m_ileftcount++]=point;
	
	//	point.x;
	//	point.y;
	CView::OnLButtonDown(nFlags, point);
}

void CMFC1View::OnInit() 
{
/*	CRect rect;	//画两条交叉线
GetClientRect(&rect);
CClientDC dc(this);
dc.MoveTo(rect.left,rect.top);
dc.LineTo(rect.right,rect.bottom);

  CPoint point1(75 ,255);
  CPoint point2(493,422);
  drawline(point1,point2);
  //	dc.MoveTo(rect.right,rect.top);
	//	dc.LineTo(rect.left,rect.bottom);*/	
	/*	bool left,right;
	left=1;right=1;
	//	drawline2(1,15);
	for(k=0.01;k<10000;k=k+0.01)
	{
	for(b=0;b<100;b++)
	{
	for(int i=0;i<=m_ileftcount;i++)
	{
				if(m_ptLeft[i].x*k+b-m_ptLeft[i].y<0)
				{
				left=0;
				//MessageBox("OKOK");
				break;
				}
				left=1;break;
				}
				for(int j=0;j<=m_irightcount;j++)
				{
				if(m_ptRight[j].x*k+b-m_ptRight[j].y>0)
				{
				right=0;
				break;
				}
				//测试所有点,如果有不符合划分的,就进行参数调整,直接跳出本循环
				//如果所有参数都通过测试,那么right=1;goto L1
				right=1;break;
				}
				if(left&&right)
				{
				//MessageBox("OKOK");
				drawline2(k,b);
				}
				}
	}*/
	int returntag=0;
	for(k=0.01;k<655352;k=k+0.1)
	{
		for(b=0;b<100;b++)
		{
			if(A1()&&A2())
			{
				returntag=1;
				break;
			}
		}
		if(returntag)
			break;
	}
	drawline2(k,b);
}

void CMFC1View::drawline(CPoint point1, CPoint point2)
{
	int x1=point1.x;
	int y1=point1.y;
	int x2=point2.x;
	int y2=point2.y;
	double k=(y1-y2)/(x1-x2);
	double b=(x1*y2-x2*y1)/(x1-x2);
	
	CRect rect;	//画两条交叉线
	GetClientRect(&rect);
	CClientDC dc(this);
	
	dc.MoveTo((x2*y1-x1*y2)/(y1-y2+0.0000001),0);
	dc.LineTo(rect.right,(y1-y2)*rect.right/(x1-x2)+(x1*y2-x2*y1)/(x1-x2));
}

void CMFC1View::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	CPen pen(PS_DASHDOTDOT,5,RGB(0,255,0));
	dc.SelectObject(&pen);
	CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	dc.SelectObject(pBrush);
	dc.MoveTo(point);
	dc.LineTo(point);
	m_ptRight[m_irightcount++]=point;
	
	CView::OnRButtonDown(nFlags, point);
}

void CMFC1View::OnPaint() 
{
	
	CPaintDC dc(this); // device context for painting
	
	//将存储的点全部重新画出来!
	CPen pen(PS_DASHDOTDOT,5,RGB(0,0,255));
	dc.SelectObject(&pen);
	CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	dc.SelectObject(pBrush);
	
	for(int i=0;i<=m_ileftcount;i++)
	{
		dc.MoveTo(m_ptLeft[i]);
		dc.LineTo(m_ptLeft[i]);	
	}
	
	CPen pen2(PS_DASHDOTDOT,5,RGB(0,255,0));
	dc.SelectObject(&pen2);
	pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	dc.SelectObject(pBrush);
	
	for(int j=0;j<=m_irightcount;j++)
	{
		dc.MoveTo(m_ptRight[j]);
		dc.LineTo(m_ptRight[j]);
	}
	
	drawline2(k,b);
	// Do not call CView::OnPaint() for painting messages
}

void CMFC1View::drawline2(double k, double b)
{
	CPoint point1(1,k+b);
	CPoint point2(10,10*k+b);
	drawline(point1,point2);
}

BOOL CMFC1View::A1()
{
	for(int i=0;i<m_ileftcount;i++)
	{
		if(m_ptLeft[i].y-k*m_ptLeft[i].x-b>0)
		{
			return FALSE;
		}
	}
	return TRUE;
}

BOOL CMFC1View::A2()
{
	for(int i=0;i<m_irightcount;i++)
	{
		if(m_ptRight[i].y-k*m_ptRight[i].x-b<0)
		{
			return FALSE;
		}
	}
	return TRUE;
}

⌨️ 快捷键说明

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