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

📄 myidedlg.cpp

📁 我自己写的蚁群算法IDE
💻 CPP
字号:
// myIDEDlg.cpp : implementation file
//

#include "stdafx.h"
#include "myIDE.h"
#include "myIDEDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyIDEDlg dialog

CMyIDEDlg::CMyIDEDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyIDEDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CMyIDEDlg)
        // 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 CMyIDEDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMyIDEDlg)
//	DDX_Control(pDX, IDC_PROGRESS_CALCU, m_CalcuProgress);
    DDX_Control(pDX, IDC_EDIT1, m_edit1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyIDEDlg, CDialog)
    //{{AFX_MSG_MAP(CMyIDEDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyIDEDlg message handlers

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

    // 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
//    m_CalcuProgress.SetLower(0);
    return TRUE;  // return TRUE  unless you set the focus to a control
}

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

UINT CMyIDEDlg::myThread(LPVOID pParam)
{
    PROCESS_INFORMATION pi;
    STARTUPINFO siStartInfo;
    SECURITY_ATTRIBUTES saAttr;
    CString Output, tmp;
    char command_line[200];
    DWORD dwRead;
    char* buf; int len;
    HANDLE hRead, hWrite;

    CMyIDEDlg* pDlg = (CMyIDEDlg*)pParam;

    // 创建与wSpawn.exe通讯的可继承的匿名管道
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;
    if (!CreatePipe(&hRead, &hWrite, &saAttr, 0))
    {
        AfxMessageBox("创建管道失败");
        return 0;
    }

    // 准备wSpawn的命令行,在命令行给出写管道句柄和要wSpawn执行的命令
    memset(&pi, 0, sizeof(pi));
    sprintf(command_line, "wspawn -h %d antcalcu /?", (unsigned int)hWrite);

	AfxMessageBox(command_line);
    // 子进程以隐藏方式运行
    ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
    siStartInfo.cb = sizeof(STARTUPINFO);
    siStartInfo.wShowWindow = SW_HIDE;
    siStartInfo.dwFlags = STARTF_USESHOWWINDOW;
    // 创建wSpawn子进程
    if (!CreateProcess( NULL, command_line, NULL, NULL, TRUE,
                        0, NULL, NULL, &siStartInfo, &pi))
    {
        AfxMessageBox("调用wSpawn时失败");
        return 0;
    }

    // 读管道,并显示wSpawn从管道中返回的输出信息
    if(!ReadFile( hRead, &len, sizeof(int), &dwRead, NULL) || dwRead == 0)
        return 0;
    while(len)
    {
        buf = new char[len + 1];
        memset(buf, 0, len + 1);

        if(!ReadFile( hRead, buf, len, &dwRead, NULL) || dwRead == 0)
            return 0;

        // 将返回信息中的"\n"替换为Edit Box可识别的"\r\n"
        tmp = buf;
        tmp.Replace("\n", "\r\n");
        Output += tmp;

        // 将结果显示在Edit Box中,并刷新对话框
        pDlg->m_edit1.SetWindowText(Output);
 //       pDlg->InvalidateRect(NULL);
        pDlg->UpdateWindow();

        delete[] buf;
        if(!ReadFile( hRead, &len, sizeof(int), &dwRead, NULL) || dwRead == 0)
            return 0;
    }

    // 等待wSpawn结束
    WaitForSingleObject(pi.hProcess, 30000);

    // 关闭管道句柄
    CloseHandle(hRead);
    CloseHandle(hWrite);

    return 0;
}

void CMyIDEDlg::OnOK()
{
	CWinThread* p;
    p=AfxBeginThread(myThread, this);
	m_ThreadID=p->m_nThreadID;
//    InvalidateRect(NULL);
    UpdateWindow();
}

void CMyIDEDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
//	AfxEndThread(m_ThreadID);

	CDialog::OnCancel();
}

void CMyIDEDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
//	m_CalcuProgress.SetPos(10);
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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