绘制和Graphics.FromHwnd清除屏幕上 [英] Drawing and clearing on screen with Graphics.FromHwnd

查看:1464
本文介绍了绘制和Graphics.FromHwnd清除屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,获取窗口的句柄光标下,展会是关于它的一些数据,并在整个窗口的顶部绘制一个填充矩形(具有非常低的阿尔法)。我使用C#和WinForms。

I am trying to create a program which gets the handle of the window under your cursor, show's some data about it and draws a filled rectangle (with very low alpha) on top of the whole window. I am using C# and winforms.

我已经做得很成功,但问题是我的画法是一个BackgroundWorker的循环,它不断使得越来越多的矩形( - >高阿尔法的窗口矩形),或移动鼠标时到另一个窗口旧的仍然存在。

I have succeeded in doing so, but the problem is my draw method is in a BackgroundWorker's loop and it keeps making more and more rectangles (-> rectangle with higher alpha) on the window or when moving mouse to another window the old one still exists.

我没有设法找到清除的方法。绘制的矩形,因为它只是是在屏幕上,而不是绑定到图形对象或任何东西。

I haven't managed to find a method to clear the drawn rectangle as it just "is" on the screen and isn't bound to the graphics object or anything.

我一直在使用某些本地方法尝试,如

I have tried using certain native methods such as

[DllImport("User32.dll")]
public static extern Int64 SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);

[DllImport("user32.dll")]
public static extern bool UpdateWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, RedrawWindowFlags flags);



但没有上面已经工作正常。他们中的一些工作,但因为这些消息在队列中得到重绘不会立即发生,或者很慢,glitched(闪烁等)。

but none of the above has worked correctly. Some of them do work but as the messages get in the queue the redrawing doesn't occur immediately or is very slow and glitched (flickering etc).

所以,问题是,我将如何删除我一直在使用Graphics.FromHwnd(handleOfWindowUnderCursor)绘制的矩形?其实,我觉得也没关系,它是绘制在其他窗口,我有同样的问题,先前试图让我自己的形式摆脱图纸太当(从来没有说固定要么!)。

So, the question is, how would I "remove" the rectangle I have drawn using Graphics.FromHwnd(handleOfWindowUnderCursor)? I actually think it doesn't matter that it is drawn on other window as I have had the very same problem earlier when trying to get rid of the drawings on my own form too (never got that fixed either!).

另外,我如何能完成绘制,并且不使用我现在的方法去除下光标窗口上的矩形什么建议?

Alternatively, any suggestions on how I could accomplish drawing and removing the rectangle on the window under cursor without using the methods I am now?

推荐答案

我注意到,使用

Graphics g = Graphics.FromHwnd(form.Handle);



借鉴了形式背景下,在其控制。它是whatyou要acomplish?

draws on the form background, under its controls. Is it whatyou want to acomplish?

// draw the rectangle
Brush b = new SolidBrush(Color.FromArgb(20, 0, 0, 255));
g.FillRectangle(b, new Rectangle(5, 5, 200, 200));

// clear the rectangle
g.Clear(this.BackColor);

如果我在屏幕上绘制直接,这一点:

If I draw on the screen directly, with this:

Graphics g = Graphics.FromHwnd(IntPtr.Zero);



立即矩形在前看不见的Windows刷新屏幕后。

the rectangle disapears immediately after Windows refreshes the screen.

有第三种选择,这是不是真的strightforward。

There is a third option, which is not realy strightforward.

相反绘制矩形,创建具有降低不透明度形式,TopMost属性设置为true,无国界。然后使其透明的事件:

Instead of drawing a rectangle, create a form with lowered opacity, TopMost property set to true and without borders. Then make it transparent to events:

protected override void WndProc(ref Message m)
        {
            const int WM_NCHITTEST = 0x0084;
            const int HTTRANSPARENT = (-1);

            if (m.Msg == WM_NCHITTEST)
            {
                m.Result = (IntPtr)HTTRANSPARENT;
            }
            else
            {
                base.WndProc(ref m);
            }
        }

您必须照顾唯一的东西后,也就是这种形式的可见性,位置和大小等属性。

The only things you have to take care after that is this form's Visible, Location and Size properties.

这篇关于绘制和Graphics.FromHwnd清除屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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