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;
}

运行结果:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值