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=;
HANDLE g_hOutput=;
LONG g_xPos=,g_yPos=; //圆心位置
int dirtx=,dirty=-; //圆移动方向
int flag=; //是否启动画圆
int cr=,cg=,cb=;
void CALLBACK MyTimer(HWND hWnd,UINT nMsg,UINT idEvent,DWORD dvTime)
{
RECT rc={};
GetClientRect(hWnd,&rc);
if(g_xPos<=rc.left+)
dirtx=;
if(g_xPos>=rc.right-)
dirtx=-;
if(g_yPos<=rc.top+)
dirty=;
if(g_yPos>=rc.bottom-)
dirty=-;
g_xPos+=dirtx;
g_yPos+=dirty;
CHAR *mystr="计时器运行\n";
WriteConsole(g_hOutput,mystr,strlen(mystr),NULL,NULL);
cg+=;
cb+=;
cr+=;
InvalidateRect(hWnd,,FALSE);
}
void OnPaint(HWND hWnd)
{
PAINTSTRUCT ps={};
LOGBRUSH logbrush={};
HBRUSH MyBrush1=CreateSolidBrush(RGB((cr*+cb*-cg)%,(cg*+cr*-cb)%,(cg*+*cb/cr)%));
HDC hdc=BeginPaint(hWnd,&ps);
SelectObject(hdc,MyBrush1);
Ellipse(hdc,g_xPos-,g_yPos-,g_xPos+,g_yPos+);
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==){
SetTimer(hWnd,,,MyTimer);
flag=;
}
else{
KillTimer(hWnd,);
flag=;
}
}
//窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd,UINT nMsg,
WPARAM wParam,LPARAM lParam)
{
switch(nMsg)
{
case WM_PAINT:
if(flag==)
OnPaint(hWnd);
break;
case WM_LBUTTONDOWN:
OnLButtonDown(hWnd,lParam);
break;
case WM_DESTROY:
PostQuitMessage();
break;
}
return DefWindowProc(hWnd,nMsg,wParam,lParam);
}
//注册窗口类
BOOL Register(LPSTR lpClassName,WNDPROC wndProc)
{
WNDCLASSEX wce={};
wce.cbSize=sizeof(wce);
wce.cbClsExtra=;
wce.cbWndExtra=;
wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+);
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==)
return FALSE;
return TRUE;
}
//创建主窗口
HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName)
{
HWND hWnd=CreateWindowEx(,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={};
while(GetMessage(&nMsg,NULL,,))
{
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 ;
}
HWND hWnd=CreateMain("Main","window");
Display(hWnd);
Message();
return ;
}
附带控制台的一些函数:
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);