#include <windows.h> #include <stdio.h> #include <math.h> #include <time.h> #include <conio.h> SMALL_RECT SizeOfWindow(HANDLE hConsoleOutput)//1 { CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(hConsoleOutput, &info); return info.srWindow; } void ClearWind(HDC hdc,HPEN hPen,HBRUSH hBrush,SMALL_RECT rect)//2 { hBrush=CreateSolidBrush(RGB(255,255,255)); hPen=CreatePen(PS_SOLID,0,RGB(255,255,255)); SelectObject(hdc,hBrush); SelectObject(hdc,hPen); Rectangle(hdc,0,50,rect.Right+1,rect.Bottom+1); } int main() { HDC hdc=0; HPEN hPen=0; HBRUSH hBrush=0; HWND hwnd; TCHAR title[256]; GetConsoleTitle(title, 256);//3 hwnd=FindWindow(0,title);//4 hdc = GetDC(hwnd);//5 HANDLE handle_out; CONSOLE_SCREEN_BUFFER_INFO info; handle_out = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(handle_out, &info);//6 SMALL_RECT rc =SizeOfWindow(handle_out); //7 ClearWind(hdc,hPen,hBrush,rc);//8 DeleteObject(hBrush);//9 DeleteObject(hPen);//10 ReleaseDC(hwnd,hdc);//11 CloseHandle(handle_out);//12 return 0; }
1---->获取控制台窗口的缓冲区尺寸。
2---->在屏幕去绘制一个矩形框来清屏。
3---->获取控制台窗口标题。
4---->通过标题来获取控制台窗口句柄。
5---->通过控制台窗口句柄获取设备句柄。
6---->获取控制台相关信息,主要是屏幕缓冲区大小。
7---->获取屏幕缓冲区大小。
8---->绘制清屏矩形,参数分别是设备句柄,画笔,画刷以及矩形区域尺寸。
9---->删除画刷句柄,释放系统资源。
10---->删除画笔句柄,释放系统资源。
11---->释放设备上下文环境。
12---->关闭句柄。
问题:我的画刷和画笔资源真的已经全部释放了吗?还是这样做比较好一些:
#include <windows.h> #include <stdio.h> #include <math.h> #include <time.h> #include <conio.h> SMALL_RECT SizeOfWindow(HANDLE hConsoleOutput) { CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(hConsoleOutput, &info); return info.srWindow; }
void ClearWind(HDC hdc,SMALL_RECT rect) { HPEN hPen=0; HBRUSH hBrush=0; hBrush=CreateSolidBrush(RGB(20,11,40)); hPen=CreatePen(PS_SOLID,0,RGB(20,11,40)); SelectObject(hdc,hBrush); SelectObject(hdc,hPen); Rectangle(hdc,0,50,rect.Right+1,rect.Bottom+1); DeleteObject(hBrush); DeleteObject(hPen); }
int main() { HDC hdc=0; HWND hwnd; TCHAR title[256]; GetConsoleTitle(title, 256); hwnd=FindWindow(0,title); hdc = GetDC(hwnd); HANDLE handle_out; CONSOLE_SCREEN_BUFFER_INFO info; handle_out = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(handle_out, &info); SMALL_RECT rc =SizeOfWindow(handle_out); ClearWind(hdc,rc); ReleaseDC(hwnd,hdc); CloseHandle(handle_out); return 0; }
运行结果:
然后,改了一种画法
#include <windows.h> #include <stdio.h> #include <math.h> #include <time.h> #include <conio.h> SMALL_RECT SizeOfWindow(HANDLE hConsoleOutput) { CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(hConsoleOutput, &info); return info.srWindow; }
void ClearWind(HWND hwnd,RECT rect) { HDC hdc=0; HPEN hPen=0; HBRUSH hBrush=0; hBrush=CreateSolidBrush(RGB(20,11,40)); hPen=CreatePen(PS_SOLID,0,RGB(20,11,40)); hdc = GetDC(hwnd); SelectObject(hdc,hBrush); SelectObject(hdc,hPen); Rectangle(hdc,0,0,rect.right-rect.left+1,rect.bottom-rect.top+1); DeleteObject(hBrush); DeleteObject(hPen); ReleaseDC(hwnd,hdc); }
int main() { HWND hwnd; RECT rc; TCHAR title[256]; GetConsoleTitle(title, 256); hwnd=FindWindow(0,title); GetWindowRect(hwnd,&rc); HANDLE handle_out; CONSOLE_SCREEN_BUFFER_INFO info; handle_out = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(handle_out, &info); ClearWind(hwnd,rc); CloseHandle(handle_out); return 0; }
结果如图:
无滚动条填充方式:
#include <windows.h> #include <stdio.h> #include <math.h> #include <time.h> #include <conio.h> SMALL_RECT SizeOfWindow(HANDLE hConsoleOutput) { CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(hConsoleOutput, &info); return info.srWindow; }
HANDLE CreateConsole(HANDLE handle_out) { CONSOLE_SCREEN_BUFFER_INFO info; handle_out = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(handle_out, &info); SMALL_RECT rect =SizeOfWindow(handle_out); COORD size={rect.Right+1,rect.Bottom+1}; SetConsoleScreenBufferSize(handle_out, size); return handle_out; }
void ClearWind(HWND hwnd,RECT rect) { HDC hdc=0; HPEN hPen=0; HBRUSH hBrush=0; hBrush=CreateSolidBrush(RGB(255,11,40)); hPen=CreatePen(PS_SOLID,0,RGB(255,11,40)); hdc = GetDC(hwnd); SelectObject(hdc,hBrush); SelectObject(hdc,hPen); Rectangle(hdc,0,0,rect.right-rect.left+1,rect.bottom-rect.top+1); DeleteObject(hBrush); DeleteObject(hPen); ReleaseDC(hwnd,hdc); }
int main() { HWND hwnd; RECT rc; TCHAR title[256]; HANDLE handle_out=0; CreateConsole(handle_out); GetConsoleTitle(title, 256); hwnd=FindWindow(0,title); GetWindowRect(hwnd,&rc); while (!kbhit()||getch() != 0x1b) { ClearWind(hwnd,rc); } CloseHandle(handle_out); return 0; }
结果如图:
问题:为什么去掉循环就会出现问题呢?试了一下下面的方法:
13---->程序挂起10ms
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复