Tags: background, current, desktop, fileand, image, microsoft, msdn, software, text, visual, wallpaper, write
how to write text to desktop wallpaper
On Microsoft » Microsoft Visual C#
9,548 words with 7 Comments; publish: Sun, 23 Dec 2007 08:23:00 GMT; (30062.50, « »)
Hi,
Does anybody know how to write some text to the desktop background.
Currently, I can write some text to the current wallpaper image file
and then use SystemParametersInfo to display the new image...but that
doesn't seem to be a good way to do so because the text format will be
changed according to the setting of the screen resolution and the
position property (center, stretch, tile).
Is there any way to output the text directly to the desktop without touching the background image?
Thanks.
http://visual-csharp.itags.org/q_visual-csharp_55692.html
All Comments
Leave a comment...
- 7 Comments

-
Hi,
You need to get the handle from the desktop. You need to call the unmanaged method "GetDesktopWindow". Check the following example:
[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgn, uint flags);
IntPtr hdc = GetDCEx(GetDesktopWindow(), IntPtr.Zero, 1027);
using(Graphics g = Graphics.FromHdc(hdc))
{
g.FillEllipse(Brushes.Red, 0, 0, 400, 400); // Or draw a string or whatever you want
}
Hope you find the post useful,
Best regards
#1; Thu, 30 Aug 2007 17:34:00 GMT

That method won't be persistent, for example; if another window is maximized the results of the draw operation will be erased.Hi,
You need to get the handle from the desktop. You need to call the unmanaged method "GetDesktopWindow". Check the following example:
[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgn, uint flags);
IntPtr hdc = GetDCEx(GetDesktopWindow(), IntPtr.Zero, 1027);
using(Graphics g = Graphics.FromHdc(hdc))
{
g.FillEllipse(Brushes.Red, 0, 0, 400, 400); // Or draw a string or whatever you want
}
Hope you find the post useful,
Best regards
#2; Thu, 30 Aug 2007 17:35:00 GMT

- You may also want to consider creating a form that just has the text, with every other part transparent. There is a way to make sure that the window stays underneath other windows. I'm not sure what it is, but desktop widget programs can do it, so it is possible.#3; Thu, 30 Aug 2007 17:36:00 GMT

Hi Peter,
Thanks for the point, I thought it was obvious that eveytime that you paint something on windows you will need to re paint once the region is invalidated.
As for the desktop you don't have the OnPaint event as you will have on a .net component you need to peek the message. You can do this intercepting the message WM_PAINT using the PeekMessage function.
BOOL PeekMessage(
LPMSG lpMsg, // Pointer to the message
HWND hWnd, // Send the desktop handle
UINT wMsgFilterMin, // here use WM_PAINT
UINT wMsgFilterMax, // Same one as we are peeking only one message
UINT wRemoveMsg // send PM_NOREMOVE
);
Hope this helps, thanks for the point Peter.
Cheers
#4; Thu, 30 Aug 2007 17:37:00 GMT

Thanks for the point, I thought it was obvious that eveytime that you paint something on windows you will need to re paint once the region is invalidated. As for the desktop you don't have the OnPaint event as you will have on a .net component you need to peek the message. You can do this intercepting the message WM_PAINT using the PeekMessage function.
BOOL PeekMessage(
LPMSG lpMsg, // Pointer to the message
HWND hWnd, // Send the desktop handle
UINT wMsgFilterMin, // here use WM_PAINT
UINT wMsgFilterMax, // Same one as we are peeking only one message
UINT wRemoveMsg // send PM_NOREMOVE
);
You're suggesting that he write an application to intercept all windows messages for the Desktop and provide additional processing for some. This suggestion is downright scary. First of all, the question was simply regarding writing to the desktop wallpaper, otherwise modifying a bitmap. Second, PeekMessage is considered bad form and a serious design flaw. You might want to check out the PeekMessage entry in The n Habits of Highly Defective Windows Applications. As well, Raymond Chen has one of many other comments about the consequense of using PeekMessage incorrectly: http://blogs.msdn.com/oldnewthing/archive/2005/02/09/369804.aspx which details how much more complicated it is than simply calling PeekMessage
#5; Thu, 30 Aug 2007 17:38:00 GMT

Hi,
It is true, reading the first post again he wants to write text on an image file (my fault ). Thanks for the links, I will have a look.
Cheers
#6; Thu, 30 Aug 2007 17:39:00 GMT

- Hi,
Thank all for your suggestions !!!
I think making a transparent form is a good idea. I actually played
around with it yesterday and it worked very well. Also, if the form
uses a web browser to display text, you will have more controls on the
text format. And yes, making a window stay underneath the others
is quite possible
#7; Thu, 30 Aug 2007 17:40:00 GMT