如何在桌面上保留矩形 [英] How to keep rectangle on desktop

查看:147
本文介绍了如何在桌面上保留矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在桌面上绘制了矩形,但现在我遇到了一些问题。

当我刷新桌面或移动到某个窗口之上时,它正在消失。我需要在桌面上有这个矩形,直到我按下一些按钮(例如刷新按钮)。



我尝试过:



所以,我在桌面上使用以下代码绘制矩形:



钢笔= 笔(Color.Yellow, 2 ); 
IntPtr desktop = GetDC( IntPtr .Zero);
使用(图形g = Graphics.FromHdc(桌面))
{
g.DrawRectangle(pen,x,y,width ,身高);
}
ReleaseDC( IntPtr .Zero,桌面);

解决方案

在Windows中临时绘制任何内容,除非它被绘制到必要时重绘的位图或类似物的表面上。因此,如果你没有重新绘制你的矩形,当另一个应用程序覆盖它,然后它再次可见它将消失 - 你真的没有任何关于它的事情。



您的应用程序并非拥有桌面,因此您的应用程序无法通知您的矩形需要重新绘制。

你可以做它有一个计时器 - 但这是一个混乱和讨厌的kludge - 但除此之外,你可以复制桌面,创建一个临时壁纸并绘制,以便系统维护你的绘图。

这可能有所帮助: c# - 使用.NET中的代码更改桌面墙纸 - Stack Overflow [ ^

Hello, I have draw Rectangle on Desktop but now I have some Problems with ths.
It is desapearing when I refresh desktop or moving above some window. I need do have this Rectangle on Desktop until I Press some Button(for example Refresh Button).

What I have tried:

So, I am drawing Rectangle on Desktop with following code:

Pen pen = new Pen(Color.Yellow, 2);
            IntPtr desktop = GetDC(IntPtr.Zero);
            using (Graphics g = Graphics.FromHdc(desktop))
            {
                g.DrawRectangle(pen, x, y, width, height);
            }
            ReleaseDC(IntPtr.Zero, desktop);

解决方案

Drawing anything in Windows ia transitory, unless it is drawn onto the surface of a bitmap or similar that is redrawn when necessary. As a result, if you don't re-draw your rectangle when another app covers it up and then it becomes visible again it will disappear - there really isn't anything you can do about that.

And your app doesn't "own" the desktop, so there is no mechanism for your app to be notified that your rectangle needs re-drawing.
You could do it with a Timer - but that's a messy and nasty kludge - but other than that, you can copy the desktop, create a temporary wallpaper and draw on that so the system "maintains" your drawing for you.
This may help: c# - Change desktop wallpaper using code in .NET - Stack Overflow[^]


这篇关于如何在桌面上保留矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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