创建半透明的窗口,以及在窗口上绘制不透明的文本

方法:

  1. 创建Bitmap具有PixelFormat32bppPARGB像素格式的GDI + 对象。
  2. 创建一个Graphics对象以绘制该Bitmap对象。
  3. 使用GDI +将所有图形绘制到该对象中。
  4. 销毁Graphics在步骤2中创建的对象。
  5. GetHBITMAPBitmap对象上调用方法以获取Windows HBITMAP
  6. 销毁Bitmap对象。
  7. 使用创建内存DC,CreateCompatibleDC然后HBITMAP从步骤5中选择。
  8. 使用内存DC作为源调用UpdateLayeredWindow。
  9. 选择以前的位图并删除内存DC。
  10. 销毁HBITMAP在步骤5中创建的。

代码示例:

#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <ObjIdl.h>
#include <gdiplus.h>
#include <gdiplusheaders.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib") #define MAX_WIDTH 800
#define MAX_HEIGHT 600 using namespace std; void Drawtext(HWND hwnd, HDC hdc); LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_DESTROY) { PostQuitMessage(0);
}
return DefWindowProc(hwnd, message, wParam, lParam);
}; HINSTANCE hinst; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) {
HWND hWnd;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken; //Initialize GDI+
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
hinst = GetModuleHandle(NULL);
// create a window class:
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hinst;
wc.lpszClassName = L"win32"; // register class with operating system:
RegisterClass(&wc); // create and show window:
hWnd = CreateWindowExW(
WS_EX_LAYERED | WS_EX_TOPMOST,
L"win32",
L"WinSoup",
WS_POPUP,
0, 0, 1000, 500,
nullptr,
nullptr,
hInstance,
nullptr
); if (hWnd == NULL) {
return 0;
} Drawtext(hWnd, GetDC(hWnd)); ShowWindow(hWnd, SW_SHOW); MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} } void Drawtext(HWND hwnd, HDC hdc)
{
FontFamily fontFamily(L"Times New Roman");
Font font(&fontFamily, 32, FontStyleRegular, UnitPixel);
PointF pointF(30.0f, 10.0f);
SolidBrush solidBrush(Color(255, 0, 0, 0)); Bitmap softwareBitmap(MAX_WIDTH, MAX_HEIGHT, PixelFormat32bppARGB);
Graphics g(&softwareBitmap); g.Clear(Gdiplus::Color(30, 0, 0, 0)); // 30: alpha value g.DrawString(L"Hello Hello Hello Hello Hello Hello Hello Hello", -1, &font, pointF, &solidBrush); HBITMAP bmp;
softwareBitmap.GetHBITMAP(Color(0, 0, 0, 0), &bmp); HDC memdc = CreateCompatibleDC(hdc);
HGDIOBJ original = SelectObject(memdc, bmp); BLENDFUNCTION blend = { 0 };
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptLocation = { 200, 300 };
SIZE szWnd = { MAX_WIDTH, MAX_HEIGHT };
POINT ptSrc = { 0, 0 };
BOOL l = UpdateLayeredWindow(hwnd, hdc, &ptLocation, &szWnd, memdc, &ptSrc, 0, &blend, ULW_ALPHA);
int err = GetLastError();
SelectObject(hdc, original); DeleteObject(bmp);
DeleteObject(memdc);
}

win32-UpdateLayeredWindow的更多相关文章

  1. c# UpdateLayeredWindow异形窗口

    #region UpdateLayeredWindow #region 重写窗体的 CreateParams 属性 protected override CreateParams CreatePara ...

  2. (转)教你实现Winform窗体的四边阴影效果

    1.首先我们得有这样一张阴影图片. 2.然后分别有两个窗体去实现这个阴影效果. SkinForm - 用于实现阴影的绘制,特性:鼠标可穿透,无法点击,跟随窗体. SkinMain - 主窗体,也是承载 ...

  3. C# Winform实现炫酷的透明动画界面

    做过.NET Winform窗体美化的人应该都很熟悉UpdateLayeredWindow吧,UpdateLayeredWindow可以实现窗体的任意透明,效果很好,不会有毛边.不过使用这个API之后 ...

  4. (五十一)c#Winform自定义控件-文字提示-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  5. C#中可直接调用WIN32的API函数--USER32.DLL

    Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...

  6. 使用Win32 API创建不规则形状&带透明色的窗口

    前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本 ...

  7. 专题:DUILIB Win32 透明效果

    Win32 透明效果相关基础知识 Layered Windows 分层窗口.这是Windows2000开始引入的概念,重新定义了窗口的Hit Testing方法,以前都是把窗口按rectangle的方 ...

  8. UpdateLayeredWindow后,使用Gdi DrawText文字透明的解决办法

    来源:http://stackoverflow.com/questions/5309914/updatelayeredwindow-and-drawtext 要点就是在先在memDc DrawText ...

  9. C#调用Win32 的API函数--User32.dll ----转载

    Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候我们之直接调用Win32 的API,可以很高效的实现想要的效果. using System; ...

  10. Win32窗口设置为透明

    可以使用以下方法将Win32窗口设置为透明: 定义窗口类时,在WNDCLASSEX结构体中设置hbrBackground成员为NULL. 在窗口创建时,使用WS_EX_LAYERED风格和SetLay ...

随机推荐

  1. [转帖]Java 8 要过时了?从JDK8飞升到JDK17,一次性给你讲明白

    https://blog.csdn.net/agonie201218/article/details/127916729?spm=1001.2101.3001.6650.2&utm_mediu ...

  2. 【转帖】nginx变量使用方法详解-7

    https://www.diewufeiyang.com/post/581.html   在 (一) 中我们提到过,Nginx 变量的值只有一种类型,那就是字符串,但是变量也有可能压根就不存在有意义的 ...

  3. [转帖]【测试】 FIO:ceph/磁盘IO测试工具 fio(iodepth深度)

    目录 随看随用 NAS文件系统测试 块系统测试 FIO用法 FIO介绍 FIO 工具常用参数: FIO结果说明 I/O 的重放('录'下实际工况的IO,用fio'重放') fio工作参数可以写入配置文 ...

  4. [转帖]ipset详解

    https://zhuanlan.zhihu.com/p/489103374 ipset创建:create 创建一个新的ipset集合:ipset create SETNAME TYPENAME SE ...

  5. [转帖]AMD第四代宵龙 9174F 亮眼

    https://www.amd.com/zh-hans/processors/epyc-9004-series#%E8%A7%84%E6%A0%BC 型号规格   型号 CPU 核心数量 线程数量 最 ...

  6. [转帖] jq实现json文本对比

      原创:打码日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介# 近期,为了给一个核心系统减负,组内决定将一些调用量大的查询接口迁移到另一个系统,由于接口逻辑比较复杂,为了保 ...

  7. MacBook m2 笔记本 + k8s容器环境开发笔记

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 为最近两周在 MacBook m2 + k8s 容器环境的 ...

  8. 设计模式学习-使用go实现迭代器模式

    迭代器模式 定义 优点 缺点 适用范围 代码实现 参考 迭代器模式 定义 迭代器模式(Iterator Design Pattern),也叫作游标模式(Cursor Design Pattern). ...

  9. Linux的守护进程 [补档-2023-08-10]

    12-1守护进程 12-1-1介绍 ​   Daemom是Linux中的后台服务进程,通常独立于控制终端并且周期性地执行某种任务或者事件.这些进 程一般不直接和用户交互,不受用户的登录,注销等影响.没 ...

  10. PHP使用cookie做浏览历史记录

    /** * @param $article文章详情 * @param int $count记录数 * tp须引入cookie类 */ function addHistory($article,$cou ...