all groups > dotnet drawing api > december 2005 >
You're in the

dotnet drawing api

group:

How to correctly invalidate region on DesktopWindow?


How to correctly invalidate region on DesktopWindow? Gabriel Lozano-Morán
12/20/2005 5:10:13 PM
dotnet drawing api: Using a mousehook I am drawing the mouse coordinates on the desktopwindow
when a user hovers the mouse over a certain control. The problem I am having
know I need to invalidate the region where I am drawing the coordinates,
thus I need to invalidate a specified region on the desktopwindow. I have
tried it P/Invoke calls to InvalidateRect and RedrawWindow but I guess I am
stuck on the correct params. Can someone also help me with any lead to how
to use double buffering when drawing on the desktop window.

Subset of the code:

============================================================================
============
[SuppressUnmanagedCodeSecurity]
public class SafeNativeMethods
{
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool InvalidateRect(HandleRef hWnd, ref RECT rect,
bool erase);

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool RedrawWindow(HandleRef hwnd, ref RECT rect,
HandleRef hrgnUpdate, int flags);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;

public RECT(int left, int top, int right, int bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
}
============================================================================
============
[SuppressUnmanagedCodeSecurity]
public sealed class UnsafeNativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll")]
public static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgn, uint
flags);

[DllImport("user32.dll", EntryPoint="ReleaseDC", CharSet=CharSet.Auto,
ExactSpelling=true)]
public static extern int ReleaseDC(HandleRef hWnd, HandleRef hDC);
}
============================================================================
============
....
// I left out the constructors and finalizers
IntPtr hdc =
UnsafeNativeMethods.GetDCEx(UnsafeNativeMethods.GetDesktopWindow(),
IntPtr.Zero, 1027);
Graphics graphics = Graphics.FromHdc(hdc);
....
// The drawing of the string is called from within a mousemove event
graphics.DrawString(...);
....
============================================================================
============

I am not very experienced in using GDI, so hopefully someone here can help
me. Any info, lead is strongly appreciated.

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be

Re: How to correctly invalidate region on DesktopWindow? Bob Powell [MVP]
12/21/2005 6:50:10 PM
I would suggest that using the LayeredWindow API would render far better
results than trying to invalidate chunks of the desktop. Top level windows
that implement this API integrate far better with the shell and changes such
as progress bars and animations are visible through these windows without
having to mess around with desktop regions.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



[quoted text, click to view]

AddThis Social Bookmark Button