简体   繁体   English

如何在Windows桌面上绘制文本?

[英]How to draw text on the desktop in Windows?

How Would I go about placing text on the windows desktop? 我将如何在Windows桌面上放置文本? I've been told that GetDesktopWindow() is what I need but I need an example. 我被告知GetDesktopWindow()是我需要的,但我需要一个例子。

I'm assuming your ultimate goal is displaying some sort of status information on the desktop. 我假设您的最终目标是在桌面上显示某种状态信息。

You will have to do either: 您将不得不这样做:

  • Inject a DLL into Explorer's process and subclass the desktop window (the SysListView32 at the bottom of the Progman window's hierarchy) to paint your text directly onto it. 注入DLL资源管理器中的进程和子类的桌面窗口( SysListView32在底部Progman窗口的层次)绘制直接在文本到它。

  • Create a nonactivatable window whose background is painted using PaintDesktop and paint your text on it. 创建一个PaintDesktop窗口,其背景使用PaintDesktop绘制并在其上绘制文本。

First solution is the most intrusive, and quite hard to code, so I would not recommend it. 第一种解决方案是最具侵入性的,而且很难编码,因此我不推荐它。

Second solution allows the most flexibility. 第二种解决方案允许最灵活。 No "undocumented" or reliance on a specific implementation of Explorer, or even of just having Explorer as a shell. 没有“未记录”或依赖于Explorer的特定实现,甚至只是将Explorer作为shell。

In order to prevent a window from being brought to the top when clicked, you can use the extended window style WS_EX_NOACTIVATE on Windows 2000 and up. 为了防止窗口在单击时被置于顶部,您可以在Windows 2000及更高版本上使用扩展窗口样式WS_EX_NOACTIVATE On downlevel systems, you can handle the WM_MOUSEACTIVATE message and return MA_NOACTIVATE . 在下层系统中,您可以处理WM_MOUSEACTIVATE消息并返回MA_NOACTIVATE

You can get away with the PaintDesktop call if you need true transparency by using layered windows, but the concept stays the same. 如果通过使用分层窗口需要真正的透明度,您可以使用PaintDesktop调用,但概念保持不变。 I wrote another answer detailing how to properly do layered windows with alpha using GDI+. 我写了另一个回答,详细说明了如何使用GDI +正确地使用alpha进行分层窗口。

Why not just draw the text in the desktop wallpaper image file? 为什么不在桌面壁纸图像文件中绘制文字?

This solution would be feasible if you don't have to update the information too often and if you have a wallpaper image. 如果您不必经常更新信息并且有壁纸图像,则此解决方案是可行的。

One can easily use CImage class to load the wallpaper image, CImage::GetDC() to obtain a device context to draw into, then save the new image, and finally update the desktop wallpaper to the new image. 可以轻松使用CImage类加载壁纸图像, CImage :: GetDC()获取要绘制的设备上下文,然后保存新图像,最后将桌面壁纸更新为新图像。

i haven't tried but i assume you could do the following: 我没试过,但我认为你可以做到以下几点:

  1. use GetDesktopWindow to retrieve the handle of the desktop window 使用GetDesktopWindow检索桌面窗口的句柄
  2. use SetWindowLong to point the windows message handler to your own procedure 使用SetWindowLong将Windows消息处理程序指向您自己的过程
  3. in your proc, process the WM_PAINT message (or whatever) and draw what you need. 在你的过程中,处理WM_PAINT消息(或其他)并绘制你需要的东西。
  4. in your proc, call the original message handler (as returned by SetWindowLong). 在你的proc中,调用原始的消息处理程序(由SetWindowLong返回)。

not 100% sure it will work, but seems like it should as this is the normal way to subclass a window. 不是100%肯定它会工作,但似乎应该这样,因为这是子窗口的正常方式。

-don -don

If your intent is to produce something like the Sidebar, you probably just want to create one or more layered windows . 如果您的目的是生成侧边栏之类的东西,您可能只想创建一个或多个分层窗口 That will also allow you to process mouse clicks and other normal sources of input, and if you supply the alpha channel information, Windows will make sure that your window is drawn properly at all times. 这也将允许您处理鼠标点击和其他常规输入源,如果您提供Alpha通道信息,Windows将确保您的窗口始终正确绘制。 If you don't want the window to be interactive, use appropriate styles (such as WS_EX_NOACTIVATE) like Koro suggests. 如果您不希望窗口是交互式的,请使用适合的样式(例如WS_EX_NOACTIVATE),如Koro建议的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM