Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / January 2006

Tip: Looking for answers? Try searching our database.

Hit-test for controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Cheetham - 07 Jan 2006 11:48 GMT
Hi,

I have some controls descended from UserControl, and I would like to
override the Hittest functionality, so that I can control which areas of
my control are treated as the control, and which parts are ignored
(because they are transparent)
e.g. A control draws itself as a circle, although the shape of the
control is square. When the mouse is over (or is clicked on) one of the
corners outside the circle, I do not want it to be treated as part of
the control.

I can't find a hit-test function in the UserControl class, or any of
it's ancestors that I have looked at.

Can anyone tell me how I can achieve this?

Thankyou.

Paul Cheetham
Light - 11 Jan 2006 06:34 GMT
I can explain one procedure but I don't know the specifics if this
would be the best way to do this or the easiest.  You will need to
respond to the mouse click event and then you will need to get the
co-ordinate.  You may have to translate those co-ordinates to be proper
for the control.

You will then need to compare the background pixel of the clicked
location to an a hit map.  For example, for a circular control then you
will create a bitmap that is filled in with a single color (say pink).
If the color at that location in the bitmap is pink then you know you
are inside control.

You can use 2 sets of images so that the actual images you use can be
complex color wise. You can change the mouse cursor in the same way so
that it will respond properly to changes over controls.

I hope this helps.  It should give you a good start.
Curtis
http://www.ghostclip.com
The Premier Popup Notepad and Help System for Developers
Andrew Smith (Infragistics) - 17 Jan 2006 04:40 GMT
The best thing to do would be to create a Region for your control that only
includes the areas that are part of your control. Your control will not even
get mouse events for areas that are not part of the region.

> Hi,
>
[quoted text clipped - 14 lines]
>
> Paul Cheetham
Bob Powell [MVP] - 19 Jan 2006 18:35 GMT
I find that the region is ugly and has nasty jaggy edges. Transparent
controls are a pain...

(my 2c)

Signature

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.

> The best thing to do would be to create a Region for your control that
> only includes the areas that are part of your control. Your control will
[quoted text clipped - 19 lines]
>>
>> Paul Cheetham
NickBailey - 17 Jan 2006 14:52 GMT
One way of doing this is to override the default window proc and handle the
WM_NCHITTEST message appropriately. i.e. something like this:

protected override void WndProc(ref Message m)
{
 base.WndProc (ref m);
 switch (m.Msg)
 {
     case 0x0084: // WM_NCHITTEST
     Point p = PointToClient(new Point((int)m.LParam));
     if (!OverControl(p))
     {
       m.Result = new IntPtr(-1); // HTTRANSPARENT
       return;
     }
 }
}

private bool OverControl(Point p)
{
 // TODO Implement as appropriate
 return true;
}

Read about WM_NCHITTEST at MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/win
dowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputmessages/wm
_nchittest.asp


You can find what the various constants are in WinUser.h which should have
come with Visual Studio (usually in C:\Program Files\Microsoft Visual Studio .
NET 2003\Vc7\PlatformSDK\Include)

>Hi,
>
[quoted text clipped - 15 lines]
>
>Paul Cheetham

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.