您的位置:首页 > 其它

图形界面程序使用控制台窗口

2012-10-25 23:34 507 查看


图形界面程序使用控制台窗口http://www.cppfans.com/articles/ideprog/consolewin.asp

看到这篇文章,很不爽,自己在vc上重新搞了一把:

// ConsoleTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ConsoleTest.h"
#include "ConsoleTestDlg.h"
#include "TConsoleWindow.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

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

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

// Implementation
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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConsoleTestDlg dialog

CConsoleTestDlg::CConsoleTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CConsoleTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CConsoleTestDlg)
// 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 CConsoleTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConsoleTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CConsoleTestDlg, CDialog)
//{{AFX_MSG_MAP(CConsoleTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
ON_BN_CLICKED(IDC_BUTTON_cls, OnBUTTONcls)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConsoleTestDlg message handlers

BOOL CConsoleTestDlg::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
}

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

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

void CConsoleTestDlg::OnOK()
{
// TODO: Add extra validation here

CDialog::OnOK();
}

void CConsoleTestDlg::OnButtonTest()
{
// TODO: Add your control notification handler code here
ConWin.Open(); //打开控制台窗口
ConWin.Caption = "控制台视窗测试程序";
ConWin.Execute("dir"); //在控制台视窗里面执行 DOS 命令
ConWin.printf("在控制台视窗里面显示文字\n");

}

void CConsoleTestDlg::OnBUTTONcls()
{
// TODO: Add your control notification handler code here
ConWin.ClrScr(); //清屏

SMALL_RECT r;
ConWin.GetRect(r);
ConWin.MoveTo((r.Right+r.Left-30)/2, (r.Bottom+r.Top)/2);
ConWin.SetColor(FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_BLUE);
ConWin.printf("屏幕中央显示蓝色背景的绿色文字");

ConWin.MoveTo(r.Left,r.Bottom); //光标移动到左下角
ConWin.SetColor(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
}

void CConsoleTestDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
ConWin.Close();
}


TConsoleWindow.cpp

// TConsoleWindow.cpp: implementation of the CTConsoleWindow class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ConsoleTest.h"
#include "TConsoleWindow.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTConsoleWindow::~CTConsoleWindow()
{

}

void CTConsoleWindow::Open(void)
{
if(!hCon)
{
AllocConsole();
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
}
}
//---------------------------------------------------------------------------
void CTConsoleWindow::Close(void)
{
if(hCon)
{
FreeConsole();
hCon = NULL;
}
}
//---------------------------------------------------------------------------
AnsiString CTConsoleWindow::fGetCaption(void)
{
char s[260];
GetConsoleTitle(s,260);
return AnsiString(s);
}
//---------------------------------------------------------------------------
void CTConsoleWindow::fSetCaption(AnsiString s)
{
SetConsoleTitle(s.c_str());
}
//---------------------------------------------------------------------------
DWORD CTConsoleWindow::printf(const char *f,...)
{
AnsiString s;
va_list argptr;
va_start(argptr, f);
vprintf(f, argptr);
s = f;
va_end(argptr);
DWORD dwWritten=0;
if(WriteConsole(hCon,s.c_str(),s.length(),&dwWritten,NULL))
return dwWritten;
return 0;
}
//---------------------------------------------------------------------------
void CTConsoleWindow::MoveTo(short x, short y)
{
COORD CurPos = {x,y};
SetConsoleCursorPosition(hCon, CurPos);
}
//---------------------------------------------------------------------------
void CTConsoleWindow::SetColor(WORD c)
{
SetConsoleTextAttribute(hCon, c);
}
//---------------------------------------------------------------------------
void CTConsoleWindow::GetRect(SMALL_RECT &r)
{
CONSOLE_SCREEN_BUFFER_INFO ConInfo;
GetConsoleScreenBufferInfo(hCon, &ConInfo);
r = ConInfo.srWindow;
}
//---------------------------------------------------------------------------
int CTConsoleWindow::Execute(AnsiString s)
{
return system(s.c_str());
}
//---------------------------------------------------------------------------
void CTConsoleWindow::ClrScr(WORD c)
{
DWORD nBytesWrite;
CONSOLE_SCREEN_BUFFER_INFO ConInfo;
GetConsoleScreenBufferInfo(hCon, &ConInfo);
COORD CurPos = { CurPos.X = ConInfo.srWindow.Left, CurPos.Y = ConInfo.srWindow.Top };
FillConsoleOutputAttribute(hCon, c, ConInfo.dwSize.X*ConInfo.dwSize.Y, CurPos, &nBytesWrite);
FillConsoleOutputCharacter(hCon, 0, ConInfo.dwSize.X*ConInfo.dwSize.Y, CurPos, &nBytesWrite);
}
//---------------------------------------------------------------------------


TConsoleWindow.h

// TConsoleWindow.h: interface for the CTConsoleWindow class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_TCONSOLEWINDOW_H__C72FA5A2_EA5A_4BA3_97B4_7ED9F38D446D__INCLUDED_)
#define AFX_TCONSOLEWINDOW_H__C72FA5A2_EA5A_4BA3_97B4_7ED9F38D446D__INCLUDED_

#include <string>
using namespace std;

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

typedef string AnsiString;

class CTConsoleWindow
{
public:
CTConsoleWindow(){hCon = NULL;}
virtual ~CTConsoleWindow();
void Open(void);
void Close(void);
int Execute(AnsiString s);
DWORD printf(const char *f,...);
void MoveTo(short x, short y);
void SetColor(WORD c);
void GetRect(SMALL_RECT &r);
void ClrScr(WORD c=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);

HANDLE Handle ;//= { read = hCon };
CString Caption ;//= { read = fGetCaption, write = fSetCaption };

private:
HANDLE hCon;
AnsiString fGetCaption(void);
void fSetCaption(AnsiString);
};

#endif // !defined(AFX_TCONSOLEWINDOW_H__C72FA5A2_EA5A_4BA3_97B4_7ED9F38D446D__INCLUDED_)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: