通过获取窗口句柄,然后获取窗口DC,然后画图
1 #include <Windows.h> 2 #include <stdio.h> 3 4 //桌面画线 5 void drawLine() 6 { 7 //获取控制台窗口 8 //HWND hwnd = GetConsoleWindow(); 9 //获取输出 10 //HDC hdc = GetDC(hwnd); 11 //获取桌面窗口 12 HWND hwnd = GetDesktopWindow(); 13 //获取输出 14 HDC hdc = GetWindowDC(hwnd); 15 HPEN hpen = CreatePen(PS_SOLID, 2, RGB(255, 255, 255)); 16 SelectObject(hdc, hpen);//选中笔 17 while (1) 18 { 19 LineTo(hdc, 10, 10); 20 Sleep(20); 21 LineTo(hdc, 100, 100); 22 Sleep(20); 23 LineTo(hdc, 100, 300); 24 Sleep(20); 25 LineTo(hdc, 600, 300); 26 Sleep(20); 27 LineTo(hdc, 900, 300); 28 Sleep(20); 29 LineTo(hdc, 900, 900); 30 } 31 32 33 system("pause"); 34 } 35 36 //桌面或者控制台显示图片 37 void drawPic() 38 { 39 //获取控制台窗口 40 //HWND hwnd = GetConsoleWindow(); 41 //获取输出 42 //HDC hdc = GetDC(hwnd); 43 //获取桌面窗口 44 HWND hwnd = GetDesktopWindow(); 45 //获取输出 46 HDC hdc = GetWindowDC(hwnd); 47 HBRUSH brush = CreateSolidBrush(RGB(255, 0, 0)); 48 SelectObject(hdc, brush);//选中笔 49 while (1) 50 { 51 Rectangle(hdc, 50, 50, 400, 400); 52 Sleep(20); 53 } 54 system("pause"); 55 56 } 57 58 //桌面输出文字 59 void drawText() 60 { 61 HWND hwnd = GetDesktopWindow(); 62 HDC hdc = GetWindowDC(hwnd); 63 SetTextColor(hdc, RGB(0, 0, 255)); 64 //设置背景 65 SetBkColor(hdc, RGB(255, 0, 0)); 66 67 while (1) 68 { 69 HFONT hfont, hfontold; 70 hfont = CreateFontA( 71 180, 72 140, 73 0, 74 0, 75 FW_REGULAR, 76 FALSE, 77 FALSE, 78 FALSE, 79 GB2312_CHARSET, 80 OUT_DEFAULT_PRECIS, 81 CLIP_DEFAULT_PRECIS, 82 PROOF_QUALITY, 83 FIXED_PITCH | FF_MODERN, 84 "隶书" 85 ); 86 87 //选择字体 88 hfontold = SelectObject(hdc, hfont); 89 //在区域输出文字 90 /*RECT rctext; 91 rctext.left = 10; 92 rctext.top = 10; 93 rctext.right = 800; 94 rctext.bottom = 800; 95 DrawTextA(hdc, "桌面文字输出测试", strlen("桌面文字输出测试"), &rctext, DT_CENTER);*/ 96 TextOutA(hdc, 0, 0, "桌面文字输出测试", strlen("桌面文字输出测试")); 97 SelectObject(hdc, hfontold); 98 Sleep(100); 99 } 100 } 101 102 void main() 103 { 104 //drawPic(); 105 //drawLine(); 106 drawText(); 107 system("pause"); 108 }
原文地址:https://www.cnblogs.com/xiaochi/p/8526031.html
时间: 2024-11-10 07:39:29