透明的 win32 窗口和文本 [英] Transparent win32 window and text

查看:45
本文介绍了透明的 win32 窗口和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作全屏透明无边框窗口,并在其顶部完美显示文本.文本背景应该是透明的,但不是实际的字体.问题是当我不执行 SetWindowRgn 时,我只能看到显示的 TextOut.我不知道我做错了什么:(

I am trying to make fullscreen transparent borderless window with text that displayed perfectly on top of it. The text background should be transparent but not the actual font face ofcourse. The problem is that I can only see TextOut displayed when I do not do SetWindowRgn. I have no idea what I am doing wrong :(

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   hInst = hInstance;

   DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
   DWORD Flags2 = WS_POPUP;

   hWnd = CreateWindowEx(Flags1, szWindowClass, szTitle, Flags2, 0, 0, 1920, 1200, 0, 0, hInstance, 0);

   if(!hWnd)return FALSE;

   HRGN GGG = CreateRectRgn(0, 0, 0, 0);
   SetWindowRgn(hWnd, GGG, false);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   DeleteObject(GGG);

   return TRUE;
}

    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    SetBkMode(hdc, TRANSPARENT);
    TextOut(hdc, 50, 50, L"MY TEXT", lstrlen(L"MY TEXT"));

    EndPaint(hWnd, &ps);

推荐答案

这样解决:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   hInst = hInstance;

   DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
   DWORD Flags2 = WS_POPUP;

   hWnd = CreateWindowEx(Flags1, szWindowClass, szTitle, Flags2, 0, 0, 1920, 1200, 0, 0, hInstance, 0);

   if(!hWnd)return FALSE;

   HRGN GGG = CreateRectRgn(0, 0, 1920, 1200);
   InvertRgn(GetDC(hWnd), GGG);
   SetWindowRgn(hWnd, GGG, false);

   COLORREF RRR = RGB(255, 0, 255);
   SetLayeredWindowAttributes(hWnd, RRR, (BYTE)0, LWA_COLORKEY);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   DeleteObject(GGG);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    RECT rect;

    switch (message)
    {
    case WM_ERASEBKGND:

        GetClientRect(hWnd, &rect);
        FillRect((HDC)wParam, &rect, CreateSolidBrush(RGB(255, 0, 255)));

        break;

这篇关于透明的 win32 窗口和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆