📄 newprocdlg.cpp
字号:
// NewProcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NewProc.h"
#include "NewProcDlg.h"
#include <process.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNewProcDlg dialog
CNewProcDlg::CNewProcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNewProcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNewProcDlg)
// 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);
memset (&m_pi, '\0', sizeof (PROCESS_INFORMATION));
}
void CNewProcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNewProcDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNewProcDlg, CDialog)
//{{AFX_MSG_MAP(CNewProcDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATEPROCESS, OnCreateprocess)
ON_BN_CLICKED(IDC_EXECNOTEPAD, OnExecnotepad)
ON_BN_CLICKED(IDC_SPAWNNOTEPAD, OnSpawnnotepad)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNewProcDlg message handlers
BOOL CNewProcDlg::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
}
// 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 CNewProcDlg::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 CNewProcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CNewProcDlg::OnCreateprocess()
{
STARTUPINFO si;
static PROCESS_INFORMATION pi;
if (pi.dwProcessId)
{
DWORD dwCode;
GetExitCodeProcess ((HANDLE) pi.hProcess, &dwCode);
if (dwCode == STILL_ACTIVE)
{
AfxMessageBox ("Another copy of NotePad.exe has been"
" started by CreateProcess\n"
"It will be stopped first.");
// TerminateProcess (pi.hProcess, 0);
PostThreadMessage (pi.dwThreadId, WM_QUIT, 0, 0);
}
}
memset ((char *) &si, '\0', sizeof (STARTUPINFO));
si.cb = sizeof (STARTUPINFO);
TCHAR szDir[_MAX_PATH];
GetWindowsDirectory (szDir, _MAX_PATH);
CString strProgPath, strProgParms;
strProgPath.Format ("%s\\NotePad.exe", szDir);
GetCurrentDirectory (_MAX_PATH, szDir);
strProgParms.Format ("notepad %s\\readme.txt", szDir);
if (!CreateProcess((LPCTSTR) strProgPath,
(char *)(LPCSTR) strProgParms,
(LPSECURITY_ATTRIBUTES) NULL,
(LPSECURITY_ATTRIBUTES) NULL,
false,
DETACHED_PROCESS,
(LPVOID) NULL,
szDir,
(LPSTARTUPINFO) &si,
(LPPROCESS_INFORMATION) &pi))
{
AfxMessageBox ("CreateProcess failed", MB_ICONSTOP);
}
}
void CNewProcDlg::OnExecnotepad()
{
if (AfxMessageBox ("This will terminate your application.\n"
"Do you want to continue?", MB_YESNO) == IDNO)
return;
if (_execlp ("NotePad", "NotePad.Exe", "readme.txt", NULL) < 0)
{
CString Message;
Message.Format ("Error %d exec'ing process", errno);
AfxMessageBox (Message);
}
}
void CNewProcDlg::OnSpawnnotepad()
{
static int nPID = 0;
CString Message;
if (nPID)
{
DWORD dwCode;
GetExitCodeProcess ((HANDLE) nPID, &dwCode);
if (dwCode == STILL_ACTIVE)
{
switch (AfxMessageBox ("Another copy of NotePad has been started by spawn\n"
"and will be terminated. Do you want to continue?",
MB_YESNO))
{
case IDNO:
return;
case IDYES:
// TerminateProcess ((HANDLE) nPID, 0);
::SendMessage ((HWND) nPID, WM_QUIT, 0, 0);
break;
default:
return;
}
}
}
nPID = _spawnlp (_P_NOWAIT, "NotePad", "NotePad.Exe", "readme.txt", NULL);
if (nPID < 0)
{
CString Message;
Message.Format ("Error %d exec'ing process", errno);
AfxMessageBox (Message);
nPID = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -