Click here to Skip to main content
15,895,709 members
Articles / Desktop Programming / MFC

Three Windows Demo

Rate me:
Please Sign up or sign in to vote.
2.29/5 (8 votes)
18 Apr 20042 min read 36K   1K   15  
Creates other processes (windows).
// ConsoleDemoDlg.cpp :
//

#include "stdafx.h"
#include "ConsoleDemo.h"
#include "ConsoleDemoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
//
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard 
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV
	//}}AFX_VIRTUAL

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

/////////////////////////////////////////////////////////////////////////////
// CConsoleDemoDlg

CConsoleDemoDlg::CConsoleDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConsoleDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConsoleDemoDlg)
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CConsoleDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConsoleDemoDlg)
	//}}AFX_DATA_MAP
}

LRESULT CConsoleDemoDlg::OnCommandHandler(WPARAM wp, LPARAM lp)
{
	CString m_window = "MyWindow";
	CWnd *pOtherWnd = CWnd::FindWindow(NULL,
		m_window.GetBuffer(m_window.GetLength()));
	m_window.ReleaseBuffer();
	CString str((LPCTSTR)lp);
	m_cbConsole.CarryReturn();
	m_cbConsole.PrintF(_T("%s"), (LPCTSTR)str);
	if(str == "dir")
	{
		CreateConsoleWindow("Command.exe DIR");
		return 0;
	}
	if(str == "exit")
	{
		if(pOtherWnd)
			// Close Other Window
			pOtherWnd->SendMessage(WM_DESTROY, 0, 0);
		// Close this Window
		SendMessage(WM_CLOSE);
		return 1;
	}
	if(str == "new")
	{
		if(pOtherWnd)
			pOtherWnd->SendMessage(WM_USER_APPLY, 0, 0);
		return 0;
	}
	if(str == "ver")
	{
		CreateConsoleWindow("Command.exe VER");
		return 0;
	}
	if(pOtherWnd)
	{
		LRESULT lRes;
		COPYDATASTRUCT cpd;
		cpd.dwData = 0;
		cpd.cbData = str.GetLength();
		cpd.lpData = (void*)str.GetBuffer(str.GetLength());
		lRes = pOtherWnd->SendMessage(WM_COPYDATA, 0, (LPARAM)&cpd);
		str.ReleaseBuffer();
	}
	else
		AfxMessageBox("Couldn't obtain pointer to other window");

	return 0;
}

BEGIN_MESSAGE_MAP(CConsoleDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CConsoleDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER+1, OnCommandHandler)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConsoleDemoDlg

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

	// IDM_ABOUTBOX 
	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);
		}
	}

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);
	
	// TODO
	m_cbConsole.SubclassDlgItem(IDC_BOX, this);
	m_cbConsole.SetCommandReceiver(GetSafeHwnd(), WM_USER+1);
	m_cbConsole.SetPromptText(_T("C:\\> "));
	m_cbConsole.SetHistorySize(20);

	return TRUE;
}

void CConsoleDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CConsoleDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		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;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CConsoleDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CConsoleDemoDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO
	if (::IsWindow(m_cbConsole.GetSafeHwnd())) {
		m_cbConsole.SetWindowPos(0,0,0,cx,cy, SWP_NOZORDER|SWP_NOMOVE);
	}
}

void CConsoleDemoDlg::CreateConsoleWindow(LPTSTR lpszCmd)
{
	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory(&si, sizeof(si));
	si.cb = sizeof(si);
	ZeroMemory(&pi, sizeof(pi));

	// Start the process
	if(!CreateProcess(
		NULL,					// Module name
		lpszCmd,				// Command line
		NULL,					// Process handle not inheritable
		NULL,					// Thread handle not inheritable
		FALSE,					// Set handle inheritance to FALSE
		NORMAL_PRIORITY_CLASS,	// No creation flag
		NULL,					// Use parent's environment block
		NULL,					// Use parent's starting directory
		&si,					// Pointer to STARTUPINFO structure
		&pi)					// Pointer to PROCESS_INFORMATION structure
		)
	{
		AfxMessageBox("CreateProcess failed.");
	}

	CloseHandle(pi.hThread);
	// Wait until child process exits.
	WaitForInputIdle(pi.hProcess, INFINITE);
	// Close process and thread handles.
	CloseHandle(pi.hProcess);

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions