使用AllocConsole()添加调试用控制台
AllocConsole 函数
为调用进程分配一个新的控制台。
使用步骤:
1. AllocConsole(); //分配控制台
2. HANDLE g_hOutput=GetStdHandle( STD_OUTPUT_HANDLE ); //获取标准输出设备句柄
3. WriteConsole(g_hOupput,lpBuffer,lpNumberofcharswritten,lpReserved); //写入字符串到控制台缓冲区
代码演示:
OS:WIN32
在vc 6.0中建一个空的win32项目,把代码复制进去进行。
// WinMain.cpp : Defines the entry point for the application. // #include "stdafx.h" #include<windows.h> HINSTANCE g_hInstance=0; HANDLE g_hOutput=0; LONG g_xPos=150,g_yPos=350; //圆心位置 int dirtx=10,dirty=-10; //圆移动方向 int flag=0; //是否启动画圆 int cr=142,cg=54,cb=128; void CALLBACK MyTimer(HWND hWnd,UINT nMsg,UINT idEvent,DWORD dvTime) { RECT rc={0}; GetClientRect(hWnd,&rc); if(g_xPos<=rc.left+50) dirtx=10; if(g_xPos>=rc.right-50) dirtx=-10; if(g_yPos<=rc.top+50) dirty=10; if(g_yPos>=rc.bottom-50) dirty=-10; g_xPos+=dirtx; g_yPos+=dirty; CHAR *mystr="计时器运行\n"; WriteConsole(g_hOutput,mystr,strlen(mystr),NULL,NULL); cg+=3; cb+=2; cr+=4; InvalidateRect(hWnd,0,FALSE); } void OnPaint(HWND hWnd) { PAINTSTRUCT ps={0}; LOGBRUSH logbrush={0}; HBRUSH MyBrush1=CreateSolidBrush(RGB((cr*244+cb*13-cg)%256,(cg*14+cr*3-cb)%256,(cg*34+23*cb/cr)%256)); HDC hdc=BeginPaint(hWnd,&ps); SelectObject(hdc,MyBrush1); Ellipse(hdc,g_xPos-50,g_yPos-50,g_xPos+50,g_yPos+50); EndPaint(hWnd,&ps); CHAR *mystr="OnPaint()启动\n"; WriteConsole(g_hOutput,mystr,strlen(mystr),NULL,NULL); } void OnLButtonDown(HWND hWnd,LPARAM lParam) { g_xPos=LOWORD(lParam); g_yPos=HIWORD(lParam); if(flag==0){ SetTimer(hWnd,14,20,MyTimer); flag=1; } else{ KillTimer(hWnd,14); flag=0; } } //窗口处理函数 LRESULT CALLBACK WndProc(HWND hWnd,UINT nMsg, WPARAM wParam,LPARAM lParam) { switch(nMsg) { case WM_PAINT: if(flag==1) OnPaint(hWnd); break; case WM_LBUTTONDOWN: OnLButtonDown(hWnd,lParam); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd,nMsg,wParam,lParam); } //注册窗口类 BOOL Register(LPSTR lpClassName,WNDPROC wndProc) { 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=lpClassName; wce.lpszMenuName=NULL; wce.style=CS_HREDRAW|CS_VREDRAW; ATOM nAtom=RegisterClassEx(&wce); if(nAtom==0) return FALSE; return TRUE; } //创建主窗口 HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName) { HWND hWnd=CreateWindowEx(0,lpClassName,lpWndName, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,NULL); return hWnd; } //显示窗口 void Display(HWND hWnd) { ShowWindow(hWnd,SW_SHOW); UpdateWindow(hWnd); } //消息循环 void Message() { MSG nMsg={0}; while(GetMessage(&nMsg,NULL,0,0)) { TranslateMessage(&nMsg); DispatchMessage(&nMsg); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. AllocConsole(); g_hOutput=GetStdHandle(STD_OUTPUT_HANDLE); g_hInstance=hInstance; if(!Register("Main",WndProc)) { MessageBox(NULL,"注册失败","Infor",MB_OK); return 0; } HWND hWnd=CreateMain("Main","window"); Display(hWnd); Message(); return 0; }
附带控制台的一些函数:
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
// 获取标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口缓冲区信息
GetConsoleScreenBufferInfo(hOut, & bInfo );
// 获取窗口缓冲区信息
char strTitle[255];
GetConsoleTitle(strTitle, 255); // 获取窗口标题
//printf("当前窗口标题是:%s\n", strTitle);
//_getch();
SetConsoleTitle("变形实验"); // 获取窗口标题
//_getch();
COORD size = {1024, 1024};
SetConsoleScreenBufferSize(hOut,size); // 重新设置缓冲区大小
//_getch();
SMALL_RECT rc = {0,0, 1024-1, 1024-1}; // 重置窗口位置和大小
SetConsoleWindowInfo(hOut,true ,&rc);
COLORREF color = RGB(192,192,192);
SetConsoleTextAttribute(hOut,FOREGROUND_INTENSITY | FOREGROUND_BLUE);