图形界面程序使用控制台窗口
来源:互联网 发布:c语言怎么自定义函数 编辑:程序博客网 时间:2024/05/20 04:50
图形界面程序使用控制台窗口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_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass 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// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CConsoleTestDlg dialogCConsoleTestDlg::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 Win32m_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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CConsoleTestDlg message handlersBOOL 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 dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small icon// TODO: Add extra initialization herereturn 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 paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.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 hereCDialog::OnOK();}void CConsoleTestDlg::OnButtonTest() {// TODO: Add your control notification handler code hereConWin.Open(); //打开控制台窗口 ConWin.Caption = "控制台视窗测试程序";ConWin.Execute("dir"); //在控制台视窗里面执行 DOS 命令ConWin.printf("在控制台视窗里面显示文字\n");}void CConsoleTestDlg::OnBUTTONcls() {// TODO: Add your control notification handler code hereConWin.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 hereConWin.Close();}
TConsoleWindow.cpp
// TConsoleWindow.cpp: implementation of the CTConsoleWindow class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "ConsoleTest.h"#include "TConsoleWindow.h"#ifdef _DEBUG#undef THIS_FILEstatic 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 > 1000typedef 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_)
- 图形界面程序使用控制台窗口
- 在图形界面程序里使用控制台窗口
- 怎样让Java图形界面程序启动时没有控制台窗口?
- GUI程序也能使用控制台窗口
- 如何在控制台程序中使用窗口
- C语言控制台窗口图形界面编程(
- C语言控制台窗口图形界面编程
- 控制台程序窗口隐藏
- 控制台程序生成窗口
- 控制台程序和窗口程序
- WinForm程序中使用控制台作为输出窗口
- WinForm程序中使用控制台(console)作为输出窗口
- 如何在窗口程序中使用控制台(/subsystem /entry)
- 窗口程序中使用控制台实现信息跟踪
- ASM使用控制台窗口
- 在控制台程序中隐藏控制台窗口!
- 在控制台程序中隐藏控制台窗口
- 在控制台程序中隐藏控制台窗口
- 《互联网产品的价值》习惯是一种无处不在的体验
- Flex4中的皮肤(1)
- Android Developers:获取当前的位置
- Flex4中的皮肤(2)
- Flex4中的皮肤(3)
- 图形界面程序使用控制台窗口
- PHP+HTML
- Flex4中的皮肤(4)
- VC6.0编写BHO 提示unresolved external symbol _main
- Activity嵌套Fragment应用放置后台很久,被系统回收,出现crash
- Flex4中的皮肤(5)
- MSSQL应用系列---NOLOCK 和ROWLOCK 的秘密
- PHP+HTML
- Android如何实现音频合成