【VC开源代码栏目提醒】:网学会员在VC开源代码频道为大家收集整理了Ex15_1View.cpp提供大家参考,希望对大家有所帮助!
// Ex15_1View.cpp : implementation of the CEx15_1View class
//
#include "stdafx.h"
#include "Ex15_1.h"
#include "Ex15_1Doc.h"
#include "Ex15_1View.h"
#include "ProcessViewDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx15_1View
IMPLEMENT_DYNCREATE(CEx15_1View, CView)
BEGIN_MESSAGE_MAP(CEx15_1View, CView)
//{{AFX_MSG_MAP(CEx15_1View)
ON_COMMAND(ID_PROCESS_CREAT_NOTPAD, OnProcessCreatNotpad)
ON_COMMAND(ID_PROCESS_CREAT_CAL, OnProcessCreatCal)
ON_COMMAND(ID_PROCESS_NOWPROCESS, OnProcessNowprocess)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CEx15_1View construction/destruction
CEx15_1View::CEx15_1View()
{
// TODO: add construction code here
}
CEx15_1View::~CEx15_1View()
{
}
BOOL CEx15_1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx15_1View drawing
void CEx15_1View::OnDraw(CDC* pDC)
{
CEx15_1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CEx15_1View printing
BOOL CEx15_1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx15_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx15_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx15_1View diagnostics
#ifdef _DEBUG
void CEx15_1View::AssertValid() const
{
CView::AssertValid();
}
void CEx15_1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CEx15_1Doc* CEx15_1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx15_1Doc)));
return (CEx15_1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx15_1View message handlers
void CEx15_1View::OnProcessCreatNotpad() //打开记事本
{
// TODO: Add your command handler code here
// 临时变量
CString sCommandLine;
char cWindowsDirectory[MAX_PATH];
char cCommandLine[MAX_PATH];
DWORD dwExitCode;
PROCESS_INFORMATION pi;
STARTUPINFO si = {sizeof(si)};
// 得到Windows目录
GetWindowsDirectory(cWindowsDirectory, MAX_PATH);
// 启动"记事本"程序的命令行
sCommandLine = CString(cWindowsDirectory) + "\\NotePad.exe";
::strcpy(cCommandLine, sCommandLine);
// 启动"记事本"作为子进程
BOOL ret = CreateProcess(NULL, cCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (ret)
{
// 关闭子进程的主线程句柄
CloseHandle(pi.hThread);
// 等待子进程的退出
WaitForSingleObject(pi.hProcess, INFINITE);
// 获取子进程的退出码
GetExitCodeProcess(pi.hProcess, &dwExitCode);
// 关闭子进程句柄
CloseHandle(pi.hProcess);
}
else
{
AfxMessageBox("打开记事本失败!\r\n请确认在windows目录下有NotePad.exe文件。" ,MB_OK|MB_ICONERROR);
}
}
void CEx15_1View::OnProcessCreatCal() //打开计算器
{
// TODO: Add your command handler code here
// 临时变量
CString sCommandLine;
char cWindowsDirectory[MAX_PATH];
char cCommandLine[MAX_PATH];
DWORD dwExitCode;
PROCESS_INFORMATION pi;
STARTUPINFO si = {sizeof(si)};
// 得到Windows目录
GetWindowsDirectory(cWindowsDirectory, MAX_PATH);
// 启动"记事本"
程序的命令行
sCommandLine = CString(cWindowsDirectory) +"\\system32"+"\\Calc.exe";
::strcpy(cCommandLine, sCommandLine);
// 启动"记事本"作为子进程
BOOL ret = CreateProcess(NULL, cCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (ret)
{
// 关闭子进