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);