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

📄 mfc毕业设计dlg.cpp

📁 二维点集合聚类的图像化演示程序 用它可以很直观的看到聚类的整个过程
💻 CPP
字号:
// mfc毕业设计Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "mfc毕业设计.h"
#include "mfc毕业设计Dlg.h"
#include "classify.h"
#include "myview.h"
#include "Mydialog.h"
//#include <new>
//#include <string>

using namespace std;

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

//用于存放数据文件的全名
int file_exist=0;
char *filename=new (char [100] );
//是否在获取数据:0为否,1为是
int  getdata=0;
//数据总数
int data_num;
//max_type
int max_type;
//用于存储点的数据结构
point *points;

CPoint current_Point;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcDlg dialog

CMfcDlg::CMfcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMfcDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMfcDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMfcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMfcDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMfcDlg, CDialog)
	//{{AFX_MSG_MAP(CMfcDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_MENUITEM_OPEN_FILE, OnMenuitemOpenFile)
	ON_COMMAND(ID_MENUITEM_EXIT, OnMenuitemExit)
	ON_COMMAND(ID_MENUITEM_COLLECT, OnMenuitemCollect)
	ON_WM_SETCURSOR()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_COMMAND(ID_MENUITEM_CLASSIFY_TYPE1, OnMenuitemClassifyType1)
	ON_COMMAND(ID_MENUITEM_AUTHER, OnMenuitemAuther)
	ON_COMMAND(ID_MENUITEM_CLASSIFY_TYPE2, OnMenuitemClassifyType2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcDlg message handlers
void CMfcDlg::CreateView()   
{   
  CRect   rect;   
  CWnd*   pWnd=GetDlgItem(IDC_STATIC_VIEW);   
  pWnd->GetWindowRect(&rect);   
  ScreenToClient(&rect);   
    
  CCreateContext   cx;   
  cx.m_pNewViewClass=RUNTIME_CLASS(myview);   
  cx.m_pCurrentDoc=new   CDocument();   
    
  CFrameWnd*   pfWnd=(CFrameWnd*)this;   
  CWnd*   pView=pfWnd->CreateView(&cx);   
  pView->MoveWindow(&rect);   
}   

BOOL CMfcDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	CreateView();
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMfcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMfcDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		HPEN hPen=CreatePen(PS_SOLID,3,RGB(255,0,0));
		dc.SelectObject(hPen);
		dc.TextOut(10,10,"fsdssf");
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMfcDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMfcDlg::OnMenuitemOpenFile() 
{
	// TODO: Add your command handler code here
	CFileDialog *lpfiledialog;
	lpfiledialog= new CFileDialog(TRUE);
	if(lpfiledialog->DoModal()==IDOK)
	{
		//
		file_exist=1;
		CString tempfilename,tempfilepath,test;
		char *tempstring;
		tempstring=new(char [50] );
		::GetCurrentDirectory(50,tempstring);
		tempfilename=lpfiledialog->GetFileName();
		tempfilepath.Empty();
		for(int i=0;i<50 && tempstring[i]!='\0';i++)
		{
			tempfilepath.Insert(i,tempstring[i]);
		}
		tempfilepath+='\\';
		tempfilepath+=tempfilename;
		for(i=0;i<100;i++)
		{
			filename[i]='\0';
		}
		for(i=0;i<tempfilepath.GetLength();i++)
		{
			filename[i]=tempfilepath.GetAt(i);
		}
		//显示结果
		//test.Empty();
		//test.Insert(0,tempfilepath.GetAt(tempfilepath.GetLength()-1));
		//AfxMessageBox(test);
		if(tempfilepath.GetAt(tempfilepath.GetLength()-1)!='t' || tempfilepath.GetAt(tempfilepath.GetLength()-2)!='x' || tempfilepath.GetAt(tempfilepath.GetLength()-3)!='t' )
		{
			data_num=-1;
			AfxMessageBox("data_num=-1");
		}
		else
			data_num=num_scan(filename);
		if(data_num!=-1)
		{
			delete []points;
			points=new(point [data_num]);
			read(filename,points,data_num);
			//AfxMessageBox(filename);
		}
		CDC *pDC=GetDC();
	    //pDC->TextOut(20,20,"fsdfsdf");
	    HPEN hpen=::CreatePen(PS_SOLID,5,RGB(230,0,0));
	    pDC->SelectObject(hpen);
	    for(i=0;i<data_num;i++)
		{
	    	//AfxMessageBox("in drawing");
	    	POINT t_point;
		    t_point.x=points[i]._x;
		    t_point.y=points[i]._y;
		    pDC->MoveTo(t_point);
		    pDC->LineTo(t_point);
		}
	//pDC->MoveTo(50,50);
	//pDC->LineTo(60,60);
	}
}


void CMfcDlg::OnMenuitemExit() 
{
	// TODO: Add your command handler code here
	exit(0);
}

BOOL CMfcDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	//delete []points;
	//delete []filename;
	return CDialog::DestroyWindow();
}

void CMfcDlg::OnMenuitemCollect() 
{
	// TODO: Add your command handler code here
	if(file_exist==1)
	{
		SetCapture();
		getdata=1;
		HCURSOR hcursor;
		hcursor=::LoadCursor(NULL,IDC_CROSS);
		::SetCursor(hcursor);
	}
	else
	{
		AfxMessageBox("请先打开/新建一个文件");
	}
}

BOOL CMfcDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if(getdata==1)
		return TRUE;
	else
		return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

void CMfcDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	//AfxMessageBox("进入到取点函数");
	// TODO: Add your message handler code here and/or call default
	if(getdata==1)
	{		
		fstream temp_file(filename,ios::app | ios::out);
		if(temp_file.eof())
			AfxMessageBox("fail!");
		GetCursorPos(&current_Point);

		CRect   rect;   
		CWnd*   pWnd=GetDlgItem(IDC_STATIC_VIEW);   
		pWnd->GetWindowRect(&rect);   
		
		temp_file<<endl;
		temp_file<<current_Point.x-rect.left+10<<"  "<<current_Point.y-rect.top+10<<" ";
		//AfxMessageBox("取点中");
		//temp_file.flush();
		temp_file.close();
		SetCapture();
		CDC *pDC=GetDC();
		HPEN hpen=::CreatePen(PS_SOLID,5,RGB(230,0,0));
	    pDC->SelectObject(hpen);
	    
		//AfxMessageBox("in drawing");
		pDC->MoveTo(current_Point.x-rect.left+10,current_Point.y-rect.top+10);
		pDC->LineTo(current_Point.x-rect.left+10,current_Point.y-rect.top+10);
		
		//画点
	}
	else
		;
		//AfxMessageBox("取点失败");
	CDialog::OnLButtonUp(nFlags, point);
}

void CMfcDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CMfcDlg::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	getdata=0;
	
	HCURSOR hcursor;
	hcursor=::LoadCursor(NULL,IDC_ARROW);
	::SetCursor(hcursor);
	ReleaseCapture();
	CDialog::OnRButtonUp(nFlags, point);
}

void CMfcDlg::OnMenuitemClassifyType1() 
{
	// TODO: Add your command handler code here
	if(file_exist==1)
	{
		CMydialog dlg;
		if(dlg.DoModal()==IDOK)
		{	
			data_num=0;
			data_num=num_scan(filename);
			delete []points;
			points=new(point [data_num]);
			read(filename,points,data_num);
			classify(points,data_num,max_type);
			CDC *pDC=GetDC();
			HPEN hpen;			
			for(int i=0;i<data_num;i++)
			{
				hpen=::CreatePen(PS_SOLID,5,RGB((255/max_type)*points[i]._type,(240/max_type)*points[i]._type,(200/max_type)*points[i]._type));
				pDC->SelectObject(hpen);
				//AfxMessageBox("in drawing");
				POINT t_point;
				t_point.x=points[i]._x;
				t_point.y=points[i]._y;
				pDC->MoveTo(t_point);
				pDC->LineTo(t_point);
			}
			//if(max_type==48)
			//	AfxMessageBox("ok");
		}
	}
	else
	{
		AfxMessageBox("请先打开一个文件");
	}
}

void CMfcDlg::OnMenuitemAuther() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlg;
	dlg.DoModal();
}

void CMfcDlg::OnMenuitemClassifyType2() 
{
	// TODO: Add your command handler code here
	if(file_exist==1)
	{
		
		data_num=num_scan(filename);
		delete []points;
		points=new(point [data_num]);
		read(filename,points,data_num);
		max_type=auto_classify(points,data_num);
		
		//AfxMessageBox("in drawing");
		classify(points,data_num,max_type);
		CDC *pDC=GetDC();
		HPEN hpen;	
		for(int i=0;i<data_num;i++)
		{
			hpen=::CreatePen(PS_SOLID,5,RGB((255/max_type)*points[i]._type,(240/max_type)*points[i]._type,(200/max_type)*points[i]._type));
			pDC->SelectObject(hpen);
			//AfxMessageBox("in drawing");
			POINT t_point;
			t_point.x=points[i]._x;
			t_point.y=points[i]._y;
			pDC->MoveTo(t_point);
			pDC->LineTo(t_point);
		}
		//if(max_type==48)
		//	AfxMessageBox("ok");
	}
	else
	{
		AfxMessageBox("请先打开一个文件");
	}
}

⌨️ 快捷键说明

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