1. 程式人生 > 實用技巧 >C++顯示文字到桌面

C++顯示文字到桌面

原始碼:

官網:https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdesktopwindow

// couttest.cpp: 定義控制檯應用程式的入口點。
//

#include "stdafx.h"

#include <iostream>
#include <iomanip>

//#define _AFXDLL
#include <afxver_.h>
#include <afxwin.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{
CWnd* pDesktopWnd = CWnd::GetDesktopWindow();
CWindowDC dc(pDesktopWnd);

POINT p, temp;
/*int i = 0;
while (1) {
GetCursorPos(&p);//獲取滑鼠座標
//SetCursorPos(p.x + 3, p.y);//更改滑鼠座標
TextOutW(dc, p.x - i, p.y - i, _T("Hello World !!!"), strlen("Hello World !!!"));
TextOutW(dc, p.x + i, p.y - i, _T("Hello World !!!"), strlen("Hello World !!!"));

TextOutW(dc, p.x - i, p.y + i, _T("Hello World !!!"), strlen("Hello World !!!"));
TextOutW(dc, p.x + i, p.y + i, _T("Hello World !!!"), strlen("Hello World !!!"));
i = i + 10;

//Sleep(10);//控制移動時間間隔
}*/

HWND hd = GetDesktopWindow();
// 方法一
RECT rect;
// 只獲得視窗客戶區的大小

GetClientRect(hd, &rect);
int client_width = (rect.right - rect.left);
int client_height = (rect.bottom - rect.top);
std::cout << "client width:" << client_width << std::endl;
std::cout << "client height:" << client_height << std::endl;
// 獲取到的是視窗在螢幕上的位置
GetWindowRect(hd, &rect);
int window_width = (rect.right - rect.left);
int window_height = (rect.bottom - rect.top);
std::cout << "window width:" << window_width << std::endl;
std::cout << "window height:" << window_height << std::endl;

// 方法二
// 不帶選單欄的大小
int no_menu_bar_width = GetSystemMetrics(SM_CXFULLSCREEN);
int no_menu_bar__height = GetSystemMetrics(SM_CYFULLSCREEN);
std::cout << "no menu bar width:" << no_menu_bar_width << std::endl;
std::cout << "no menu bar height:" << no_menu_bar__height << std::endl;
// 帶標題欄和選單欄
int have_menu_bar_width = GetSystemMetrics(SM_CXSCREEN);
int have_menu_bar_height = GetSystemMetrics(SM_CYSCREEN);
std::cout << "have menu bar width:" << window_width << std::endl;
std::cout << "have menu bar height:" << window_height << std::endl;

//方法三
HDC hdc = GetDC(NULL); // 得到螢幕DC
client_width = GetDeviceCaps(hdc, HORZRES); // 寬
client_height = GetDeviceCaps(hdc, VERTRES); // 高
ReleaseDC(NULL, hdc); // 釋放DC
std::cout << "client width:" << client_width << std::endl;
std::cout << "client height:" << client_height << std::endl;

//方法四
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, SPIF_SENDCHANGE);
client_width = (rect.right - rect.left);
client_height = (rect.bottom - rect.top);
std::cout << "client width:" << client_width << std::endl;
std::cout << "client height:" << client_height << std::endl;

//方法五
hdc = GetDC(NULL);
client_width = GetDeviceCaps(hdc, DESKTOPHORZRES);
client_height = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(NULL, hdc);
std::cout << "client width:" << client_width << std::endl;
std::cout << "client height:" << client_height << std::endl;

//getchar();

/*TextOutW(dc, 300, 300, _T("Hello World !!!"), strlen("Hello World !!!"));
system("pause");
TextOut(dc, 500, 500, _T("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), strlen("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
system("pause");*/

int i = 0;
GetCursorPos(&p);
while (true)
{
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(255, 0, 0));
temp = p;
GetCursorPos(&p);
cout << "X:" << p.x << "\t" << "Y:" << p.y << endl;
if (temp.x == p.x && p.x + i <= client_width && p.x - i >= 0)
{
TextOutW(dc, p.x - i, p.y - i, _T("Hello World !!!"), strlen("Hello World !!!"));
TextOutW(dc, p.x + i, p.y - i, _T("Hello World !!!"), strlen("Hello World !!!"));
TextOutW(dc, p.x - i, p.y + i, _T("Hello World !!!"), strlen("Hello World !!!"));
TextOutW(dc, p.x + i, p.y + i, _T("Hello World !!!"), strlen("Hello World !!!"));
}
else
{
i = 0;
}
i = i + 10;
Sleep(1000);
}
cout << p.x << "\t" << p.y << endl;

return 0;
}

執行結果: