源码:
官网: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;
}
运行结果: