美文网首页
windows-c++ 半透明text子窗口

windows-c++ 半透明text子窗口

作者: wolfaherd | 来源:发表于2020-06-11 17:32 被阅读0次

    创建

    通过CreateWindowEx创建子窗口(注意窗口风格设置为WS_CHILD,并传入父窗口的句柄)

    添加WS_EX_LAYERED属性

    法一:在通过CreateWindowEx创建窗口时传入第一个传参。

    法二:在窗口创建完成后调用SetWindowLong方法设置。
    SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);

    设置透明度

    SetLayeredWindowAttributes(hWnd,0, alpha, LWA_ALPHA)

    置顶子窗口

    RECT rect;
    GetClientRect(hWnd, &rect);
    SetWindowPos(hWnd, HWND_TOP, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_SHOWWINDOW);

    输出Text

    使用TextOut输出即可。
    若需要修改背景或字体颜色,需要在处理WM_PAINT消息上实现
    SetTextColor :修改文字颜色; SetBkMode :可设置透明度。【TRANSPARENT】

    令WS_EX_LAYERED属性在子窗口上生效(因为该特性是在WIN8以上才会支持)

    修改.exe相对应的.manifest。(https://www.jianshu.com/p/60a76a7c8388
    .manifest需添加内容:原文地址(https://www.cnblogs.com/snowbook/p/5363990.html

    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
            <application>
                <!-- Windows 10 -->
                <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
                <!-- Windows 8.1 -->
                <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
                <!-- Windows Vista -->
                <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
                <!-- Windows 7 -->
                <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
                <!-- Windows 8 -->
                <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
            </application>
        </compatibility>
    

    相关文章

      网友评论

          本文标题:windows-c++ 半透明text子窗口

          本文链接:https://www.haomeiwen.com/subject/dmvbtktx.html