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

📄 mapguiddlg.cpp

📁 我写的,画出最短路径,及最小生成树 Prim Dijistra算法+ GDI实现
💻 CPP
字号:
// MapGuidDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MapGuid.h"
#include "MapGuidDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapGuidDlg dialog

CMapGuidDlg::CMapGuidDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMapGuidDlg::IDD, pParent)
{
	//Initialization
    this->SetPath=false;
    this->ChoosePoint=true;
	this->Start=true;
	this->StartToEnd=false;
	this->PenNum=0;
	//{{AFX_DATA_INIT(CMapGuidDlg)
		// 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 CMapGuidDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMapGuidDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMapGuidDlg, CDialog)
	//{{AFX_MSG_MAP(CMapGuidDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapGuidDlg message handlers

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

	// 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 CMapGuidDlg::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 CMapGuidDlg::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;

		// 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 CMapGuidDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMapGuidDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	EndPoint.x=point.x;
    EndPoint.y=point.y;;
	CClientDC dc(this);
/*	if(StartPoint.x!=-1)
	{
		dc.MoveTo(StartPoint);
		dc.LineTo(EndPoint);
		StartPoint.x=EndPoint.x;
        StartPoint.y=EndPoint.y;
	}*/
	CDialog::OnMouseMove(nFlags, point);
}

void CMapGuidDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    CClientDC dc(this);
	
	if(this->ChoosePoint==true)
	{
	
		CBrush MyBrush(RGB(250, 25, 5));
		CBrush *pBrush=dc.SelectObject(&MyBrush);
		dc.Ellipse(point.x-5,point.y+5,point.x+5,point.y-5);
        dc.SelectObject(pBrush);
        this->MyGraph.AddNew(point.x,point.y);
	}
	if(this->SetPath==true)
	{
		if(this->Start==true)
		{	
			xStart=point.x;
            yStart=point.y;
            this->Start=false;
		}
		else
		{
            xEnd=point.x;
            yEnd=point.y;
			if(this->MyGraph.SetPath(xStart,yStart,xEnd,yEnd)==true)
			{
                StartPoint.x=xStart;
				StartPoint.y=yStart;
				EndPoint.x=xEnd;
				EndPoint.y=yEnd;
             	dc.MoveTo(StartPoint);
		        dc.LineTo(EndPoint);
			}
			this->Start=true;
		}
	}
	if(this->StartToEnd==true)
	{
		if(this->Start==true)
		{	
			xStart=point.x;
            yStart=point.y;
            this->Start=false;
		}
		else
		{
            xEnd=point.x;
            yEnd=point.y;
			if(this->MyGraph.StartToEnd(xStart,yStart,xEnd,yEnd)==true)
			{	
                int Pre=this->MyGraph.EndIndex;
                     CPen NewPen;
					 switch(this->PenNum%10)
					 {
					   case 0:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(255, 25, 5));break;
					   case 1:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(255, 16, 247));break;
					   case 2:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(255, 239, 26));break;
					   case 3:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(151, 236, 244));break;
					   case 4:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(151, 12, 250));break;
					   case 5:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(122, 237, 19));break;
					   case 6:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(0, 81, 42));break;
					   case 7:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(0, 121, 149));break;
					   case 8:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(254, 208, 251));break;
					   default:NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(3, 4, 252));break;
					 }
               
                    this->PenNum++;
					while(Pre!=0&&Pre!=this->MyGraph.StartIndex)
					{
						this->MyGraph.Now=this->MyGraph.Head->Next;
						while(this->MyGraph.Now->Index!=Pre)
						{
                           this->MyGraph.Now=this->MyGraph.Now->Next;
						}
						StartPoint.x=this->MyGraph.Now->x;
                        StartPoint.y=this->MyGraph.Now->y;
                        Pre=this->MyGraph.P[Pre];
                        this->MyGraph.Now=this->MyGraph.Head->Next;
						while(this->MyGraph.Now->Index!=Pre)
						{
                           this->MyGraph.Now=this->MyGraph.Now->Next;
						}
						EndPoint.x=this->MyGraph.Now->x;
                        EndPoint.y=this->MyGraph.Now->y;
						  CPen* pPen = dc.SelectObject(&NewPen);
						  dc.MoveTo(StartPoint);
		                  dc.LineTo(EndPoint);
				        	dc.SelectObject(pPen);
						
					}
			}
			this->Start=true;
		}
		
	}
	CDialog::OnLButtonDown(nFlags, point);
}

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

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

void CMapGuidDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	/*int i;
	this->MyGraph.StartIndex=0;
	this->MyGraph.SearchPath();
	CClientDC dc(this);
	CPen NewPen;
	NewPen.CreatePen(PS_DASHDOTDOT,4, RGB(24,8,208));
		for(i=1;i<this->MyGraph.Max;i++)
		{
			 int Pre=i;
                     
					while(Pre!=0)
					{
						this->MyGraph.Now=this->MyGraph.Head->Next;
						while(this->MyGraph.Now!=0)
						{
							if(this->MyGraph.Now->Index!=Pre)
                           this->MyGraph.Now=this->MyGraph.Now->Next;
							else 
							{
									StartPoint.x=this->MyGraph.Now->x;
                                    StartPoint.y=this->MyGraph.Now->y;
                                    break;
							}
						}
					
                        Pre=this->MyGraph.P[Pre];
                        this->MyGraph.Now=this->MyGraph.Head->Next;
						while(this->MyGraph.Now!=0)
						{
							if(this->MyGraph.Now->Index!=Pre)
                           this->MyGraph.Now=this->MyGraph.Now->Next;
							else
							{
								EndPoint.x=this->MyGraph.Now->x;
                                EndPoint.y=this->MyGraph.Now->y;
								break;
							}
						}
					
						CPen* pPen = dc.SelectObject(&NewPen);
						dc.MoveTo(StartPoint);
		                dc.LineTo(EndPoint);
						dc.SelectObject(pPen);
					}
	 	}*/
        int i;
    	this->MyGraph.Prim();
    	CClientDC dc(this);
    	CPen NewPen;
    	NewPen.CreatePen(PS_DASHDOTDOT,4, RGB(24,8,208));
		for(i=1;i<this->MyGraph.Max;i++)
		{
			 int Pre;        	
			 this->MyGraph.Now=this->MyGraph.Head->Next;
		    	while(this->MyGraph.Now!=0)
				{
			    	if(this->MyGraph.Now->Index!=i)
                        this->MyGraph.Now=this->MyGraph.Now->Next;
			    	else 
					{
			    		StartPoint.x=this->MyGraph.Now->x;
                        StartPoint.y=this->MyGraph.Now->y;
                        break;
					}
				}	
               Pre=this->MyGraph.CloseVertex[i];
               this->MyGraph.Now=this->MyGraph.Head->Next;
		    	while(this->MyGraph.Now!=0)
				{
			    	if(this->MyGraph.Now->Index!=Pre)
                       this->MyGraph.Now=this->MyGraph.Now->Next;
		    		else
					{
			    		EndPoint.x=this->MyGraph.Now->x;
                        EndPoint.y=this->MyGraph.Now->y;
			    		break;
					}
				}				
				CPen* pPen = dc.SelectObject(&NewPen);
				dc.MoveTo(StartPoint);
				dc.LineTo(EndPoint);
				dc.SelectObject(pPen);
		}

}

void CMapGuidDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	this->ChoosePoint=false;
	this->SetPath=true;
	this->StartToEnd=false;
}

void CMapGuidDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	this->ChoosePoint=true;
	this->SetPath=false;
	this->StartToEnd=false;
	this->RedrawWindow();
    this->MyGraph.ReDraw();
}

void CMapGuidDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	this->ChoosePoint=false;
	this->SetPath=false;
	this->StartToEnd=true;
	this->Start=true;
}
/*
void CMapGuidDlg::OnCheck3() 
{
	// TODO: Add your control notification handler code here
	if(this->MyGraph.Orient=true)
      this->MyGraph.Orient=false;
	if(this->MyGraph.Orient=false)
      this->MyGraph.Orient=true;
}*/

⌨️ 快捷键说明

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