Win32程序(例如MFC、后台服务等)使用AllocConsole()添加调试用控制台

一、AllocConsole 函数

为调用进程分配一个新的控制台。

使用步骤:

1. AllocConsole();//分配控制台

2. HANDLE g_hOutput=GetStdHandle(STD_OUTPUT_HANDLE );//获取标准输出设备句柄

3. WriteConsole(g_hOupput,lpBuffer,lpNumberofcharswritten,lpReserved);//写入字符串到控制台缓冲区

 

二、程序示例

#include <Windows.h>

#include <stdio.h>

 

HINSTANCE g_hInstance = 0;

HANDLE g_hOutput = 0;

 

void OnKeyDown( HWND hWnd, WPARAM wParam )

{

         //将日志信息输出到控制台窗口中

         CHAR szText[256] = { 0 };

         sprintf( szText, "You Push Down Key:%c\n", wParam );

         WriteConsole( g_hOutput, szText, strlen(szText), NULL, NULL );

}

 

LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam )

{

         switch( nMsg )

         {

         case WM_KEYDOWN:

                                     OnKeyDown( hWnd, wParam );

                                     break;

         case WM_DESTROY:

                   PostQuitMessage( 0 );

                   break;

         }

         return DefWindowProc( hWnd, nMsg, wParam, lParam );

}

 

int APIENTRY WinMain(HINSTANCE hInstance,

                                                HINSTANCE hPrevInstance,

                                                LPSTR     lpCmdLine,

                                                int     nCmdShow)

{

         g_hInstance = hInstance;

 

         //创建一个控制台窗口

         AllocConsole( );

         g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );

 

         //设置控制台窗口的属性

         CHAR title[]="Output Debug Window";

         SetConsoleTitle(title);

         SetConsoleTextAttribute((HANDLE)g_hOutput,FOREGROUND_RED| FOREGROUND_GREEN| FOREGROUND_INTENSITY);//| FOREGROUND_BLUE );

 

         //获取控制台窗口的句柄

         HWND hwnd=NULL;

         while(NULL==hwnd) hwnd=::FindWindow(NULL,(LPCTSTR)title);

 

         //屏蔽掉控制台窗口的关闭按钮,以防窗口被误删除

         HMENU hmenu = ::GetSystemMenu ( hwnd, FALSE );

         DeleteMenu ( hmenu, SC_CLOSE, MF_BYCOMMAND );

 

         WNDCLASSEX wce = { 0 };

         wce.cbSize = sizeof( wce );

         wce.cbClsExtra = 0;

         wce.cbWndExtra = 0;

         wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

         wce.hCursor = NULL;

         wce.hIcon = NULL;

         wce.hIconSm = NULL;

         wce.hInstance = g_hInstance;

         wce.lpfnWndProc = WndProc;

         wce.lpszClassName = "Main";

         wce.lpszMenuName = NULL;

         wce.style = CS_HREDRAW | CS_VREDRAW;

         ATOM nAtom = RegisterClassEx( &wce );

         if( nAtom == 0 )

         {

                   MessageBox( NULL, "Failed to RegisterClass()", "Info", MB_OK );

                   return 0;

         }

 

         HWND hWnd = CreateWindowEx( 0, "Main", "window",

                   WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,

                   CW_USEDEFAULT, CW_USEDEFAULT,

                   CW_USEDEFAULT, NULL, NULL, g_hInstance,

                   NULL );

 

         ShowWindow( hWnd, SW_SHOW );

         UpdateWindow( hWnd );

 

         MSG nMsg = { 0 };

         while( GetMessage( &nMsg, NULL, 0, 0 ) )

         {

                   TranslateMessage( &nMsg );

                   DispatchMessage( &nMsg );

         }

 

         return 0;

}

三、附带控制台的一些函数

        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

         //获取标准输出设备句柄

         CONSOLE_SCREEN_BUFFER_INFObInfo; // 窗口缓冲区信息

         GetConsoleScreenBufferInfo(hOut,& bInfo );

         //获取窗口缓冲区信息

         charstrTitle[255];

         GetConsoleTitle(strTitle,255); // 获取窗口标题

         //printf("当前窗口标题是:%s\n",strTitle);

         //_getch();

         SetConsoleTitle("变形实验");// 获取窗口标题

         //_getch();

         COORDsize = {1024, 1024};

         SetConsoleScreenBufferSize(hOut,size);// 重新设置缓冲区大小

         //_getch();

         SMALL_RECTrc = {0,0, 1024-1, 1024-1}; // 重置窗口位置和大小

         SetConsoleWindowInfo(hOut,true,&rc);

         COLORREFcolor = RGB(192,192,192);

         SetConsoleTextAttribute(hOut,FOREGROUND_INTENSITY| FOREGROUND_BLUE);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值