【新年散分】送一个小代码:控制台下绘图、键盘、鼠标控制
这儿高手有的是 就不多解释了 我用VC6调试了下 没问题:
程序代码:#include <stdio.h>
#include <windows.h>
#include <math.h>
// 来自:<span style="color: #008000; text-decoration: underline;">http://support.[/color]
// 获取控制台窗口句柄 微软官方网站的程序 直接拿来用了
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
wsprintf(pszNewWindowTitle,"%d/%d",
GetTickCount(),
GetCurrentProcessId());
SetConsoleTitle(pszNewWindowTitle);
Sleep(40);
hwndFound=FindWindow(NULL, pszNewWindowTitle);
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
int main (void)
{
HWND hConsole = GetConsoleHwnd(); // 获得控制台窗口句柄
int nPos = 0;
while (1)
{
HDC hDC = GetDC(hConsole); // 获得控制台窗口绘图DC
// 随意绘制的一些内容
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(200, nPos%256, (nPos*3)%256)); // 创建一个画笔 相关知识查找MSDN 输入GDI方面的知识 创建填充用的笔刷用Brush
HPEN hOldPen = (HPEN)SelectObject(hDC, hPen); // 让DC选择此画笔
for (int i=0; i<300; i++)
{
// 画线
MoveToEx(hDC, (i+nPos)%800, (int)(sin(i/10.0)*100+200), NULL);
LineTo(hDC, (i+nPos)%800, (int)(sin((i+1)/10.0)*100+100));
}
if (nPos%70 == 0)
InvalidateRect(hConsole, NULL, TRUE); // 刷新窗口
SelectObject(hDC, hOldPen); // 恢复原有画笔
nPos = (nPos+20)%800;
// 停顿时间
Sleep(200);
ReleaseDC(hConsole, hDC);// 释放DC
}
return 0;
}









大哥啊以后不懂的群里照应照应哈