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

📄 travellerappdlg.cpp

📁 旅行商问题
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值

    return TRUE;//CDialog::OnEraseBkgnd(pDC);
}

void CTravellerAppDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);

    // TODO: 在此处添加消息处理程序代码
    this->Invalidate(false);
}

void CTravellerAppDlg::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    CRect rect;
    this->GetClientRect(&rect);
    rect.bottom -= 20;
    if(nFlags & 1 && m_iSelected >= 0 && m_iSelected < m_cityMap.GetCityNum() && ::GetKeyState(VK_LSHIFT) & 0x80)
    {
        m_cityMap.SetCitySite(
            m_iSelected,
            double(point.x - rect.left) / rect.Width(),
            double(point.y - rect.top) / rect.Height());
        this->Invalidate(false);
    }
    else
    {
        double x, y, dx, dy;
        x = double(point.x - rect.left) / rect.Width();
        y = double(point.y - rect.top) / rect.Height();
        dx = double(CM_CITY_DRAW_SIZE + 1) / rect.Width();
        dy = double(CM_CITY_DRAW_SIZE + 1) / rect.Height();
        int index = this->m_cityMap.HitTest(x, y, dx, dy);
        if(index != this->m_iHighLight)
        {
            this->m_iHighLight = index;
            this->Invalidate(false);
        }
    }

    CDialog::OnMouseMove(nFlags, point);
}

void CTravellerAppDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if(::GetKeyState(VK_LSHIFT) & 0x80)
    {
        switch(nChar)
        {
        case 'D':
        case 'd':
            {
                this->m_cityMap.DeleteCity(this->m_iSelected);
                m_iSelected--;
                this->Invalidate(false);
            }
            break;
        default:
            break;
        }
    }

    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CTravellerAppDlg::OnMOpen()
{
    // TODO: 在此添加命令处理程序代码
    if(this->m_cityMap.IsComputing())
    {
        return;
    }
    this->m_iSelected = -1;
    char szFilters[]=
        "地图文件 (*.map)|*.map||";

    // Create an Open dialog; the default file name extension is ".my".
    CFileDialog fileDlg (TRUE, "map", "*.map",
        OFN_FILEMUSTEXIST, szFilters, this);

    // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
    // returns IDOK.
    if( fileDlg.DoModal ()==IDOK )
    {
        this->m_cityMap.ReadFile(fileDlg.GetPathName());
        this->Invalidate(false);
    }
}

void CTravellerAppDlg::OnMSave()
{
    // TODO: 在此添加命令处理程序代码
    if(this->m_cityMap.IsComputing())
    {
        return;
    }
    this->m_iSelected = -1;
    char szFilters[]=
        "地图文件 (*.map)|*.map||";

    // Create an Open dialog; the default file name extension is ".my".
    CFileDialog fileDlg (FALSE, "map", "*.map",
        0, szFilters, this);

    // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
    // returns IDOK.
    if( fileDlg.DoModal () == IDOK )
    {
        this->m_cityMap.WriteFile(fileDlg.GetPathName());
        this->Invalidate(false);
    }
}

void CTravellerAppDlg::OnMClear()
{
    // TODO: 在此添加命令处理程序代码
    if(this->m_cityMap.IsComputing())
    {
        return;
    }
    if(this->MessageBox("是否删除所有城市坐标?", "清空", MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
    {
        this->m_cityMap.Clear();
        this->Invalidate(false);
    }
}

void CTravellerAppDlg::OnMDelete()
{
    // TODO: 在此添加命令处理程序代码
    this->m_cityMap.DeleteCity(this->m_iSelected);
    m_iSelected--;
    this->Invalidate(false);
}

void CTravellerAppDlg::OnMSetBk()
{
    // TODO: 在此添加命令处理程序代码
	CString strFilter;
	CSimpleArray<GUID> aguidFileTypes;
    this->m_iSelected = -1;

    HRESULT hResult = CImage::GetExporterFilterString(strFilter, aguidFileTypes);
	if (FAILED(hResult))
    {
		CString fmt;
		fmt.Format("GetExporterFilter failed:\n%x", hResult);
		this->MessageBox(fmt);
		return;
	}

	CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter);
	dlg.m_ofn.nFilterIndex = nFilterLoad;
	hResult = (int)dlg.DoModal();
	if(hResult == IDOK)
    {
        if(!m_imgBackground.IsNull())
        {
            m_imgBackground.Destroy();
            //m_imgBackground.Detach();
        }
        m_imgBackground.Load(dlg.GetPathName());
	}

	nFilterLoad = dlg.m_ofn.nFilterIndex;
    this->Invalidate(false);
}

void CTravellerAppDlg::OnMShowBk()
{
    // TODO: 在此添加命令处理程序代码
    m_bShowBackground = !m_bShowBackground;
    this->GetMenu()->CheckMenuItem(ID_M_SHOW_BK, m_bShowBackground ? MF_CHECKED : MF_UNCHECKED);
    this->Invalidate(false);
}

void CTravellerAppDlg::OnMBeginCompute()
{
    // TODO: 在此添加命令处理程序代码
    this->BeginWaitCursor();
    if(this->m_cityMap.StartCompute())
    {
        this->SetTimer(0, 100, 0);
        OnMShowTrend();
    }
    else
    {
        this->MessageBox("启动计算失败!", "错误", MB_OK | MB_ICONERROR);
    }
    this->EndWaitCursor();
}

void CTravellerAppDlg::OnMEndCompute()
{
    // TODO: 在此添加命令处理程序代码
    this->BeginWaitCursor();
    if(this->m_cityMap.StopCompute())
    {
        this->KillTimer(0);
        this->SetWindowText("GA旅行商问题");
        this->Invalidate(false);
        this->m_dlgTrend.Invalidate(false);
    }
    else
    {
        this->MessageBox("停止计算失败!", "错误", MB_OK | MB_ICONERROR);
    }
    this->EndWaitCursor();
}

void CTravellerAppDlg::OnTimer(UINT nIDEvent)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    this->Invalidate(false);
    this->m_dlgTrend.Invalidate(false);
    this->SetWindowText(this->m_cityMap.GetStateSimpleDescription());

    CDialog::OnTimer(nIDEvent);
}

void CTravellerAppDlg::OnClose()
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    this->BeginWaitCursor();
    if(this->m_cityMap.StopCompute())
    {
        this->KillTimer(0);
        this->m_cityMap.SaveSetting();
        CDialog::OnClose();
    }
    else
    {
        this->MessageBox("停止计算失败!", "错误", MB_OK | MB_ICONERROR);
    }
    this->EndWaitCursor();
}

void CTravellerAppDlg::OnMQuit()
{
    // TODO: 在此添加命令处理程序代码
    this->SendMessage(WM_CLOSE);
}

int CTravellerAppDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;

    return 0;
}

void CTravellerAppDlg::OnMShowTrend()
{
    // TODO: 在此添加命令处理程序代码
    m_dlgTrend.ShowWindow(SW_SHOW);
}

void CTravellerAppDlg::OnMRand()
{
    // TODO: 在此添加命令处理程序代码
    if(!this->m_cityMap.IsComputing())
    {
        CRandCreateDlg dlg;
        if(dlg.DoModal() == IDOK)
        {
            this->BeginWaitCursor();
            this->m_cityMap.Clear();
            for(int i = 0; i < dlg.m_iCityNum; i++)
            {
                this->m_cityMap.AddCity((100 + rand() % 9800) / 10000.0, (100 + rand() % 9800) / 10000.0);
            }
            this->EndWaitCursor();
            this->Invalidate(false);
        }
    }
}

⌨️ 快捷键说明

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