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 General / February 2005

Tip: Looking for answers? Try searching our database.

Need Graphical Tooltip

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RichM - 09 Feb 2005 22:29 GMT
I need to make a control that behaves like a tool tip, but I need to be able
to draw on it and I also need to show it on demand, not necessarily during a
mousehover event.

The problem with deriving from Control, is that you get the window frame
when you make the control a top level control. Can I make the frame go away?
I could do something with NativeWindow, but I wanted to check here for
something easier. I'd like to avoid the windows API if possible.
Matt Garven - 10 Feb 2005 02:21 GMT
We investigated Tooltips a while back and found (sadly) that the .NET Tooltip
classes are not designed with extensibility in mind. We wrote our own in the
end.

You could try something like this to start with...

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication34
{
   public class ToolTip : System.Windows.Forms.Form
   {
       public ToolTip()
       {
           FormBorderStyle = FormBorderStyle.None;
           BackColor = SystemColors.Info;
           ForeColor = SystemColors.InfoText;

           SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
           ShowInTaskbar = false;
           StartPosition = FormStartPosition.Manual;

           paintSize = new Size(120, 15);
           Size = paintSize;
           Region = new Region(new Rectangle(new Point(0, 0), paintSize));
// can't actually have a form this small, set the paint region
       }

       private Size paintSize;

       public new void Show()
       {
           ShowWindow(new HandleRef(this, Handle), 4);
           SetWindowPos(new HandleRef(this, Handle), new IntPtr(-2), Left,
Top, Width, Height, 0x10);
       }

       protected override void OnPaint(PaintEventArgs e)
       {
           base.OnPaint(e);
           e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0, 0,
paintSize.Width - 1, paintSize.Height - 1));
           using (SolidBrush brush = new SolidBrush(ForeColor))
           {
               e.Graphics.DrawString("My Tip", Font, brush, 0, 0);
           }
       }

       [DllImportAttribute("user32.dll", ExactSpelling=true,
CharSet=CharSet.Auto)]
       static extern bool SetWindowPos(HandleRef hWnd, IntPtr
hWndInsertAfter, int x, int y, int cx, int cy, int flags);

       [DllImportAttribute("user32.dll", ExactSpelling=true,
CharSet=CharSet.Auto)]
       static extern bool ShowWindow(HandleRef hWnd, int nCmdShow);
   }
}

Hope this helps.

Regards,
Matt Garven

> I need to make a control that behaves like a tool tip, but I need to be able
> to draw on it and I also need to show it on demand, not necessarily during a
[quoted text clipped - 4 lines]
> I could do something with NativeWindow, but I wanted to check here for
> something easier. I'd like to avoid the windows API if possible.
Matt Garven - 10 Feb 2005 02:21 GMT
We investigated Tooltips a while back and found (sadly) that the .NET Tooltip
classes are not designed with extensibility in mind. We wrote our own in the
end.

You could try something like this to start with...

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication34
{
   public class ToolTip : System.Windows.Forms.Form
   {
       public ToolTip()
       {
           FormBorderStyle = FormBorderStyle.None;
           BackColor = SystemColors.Info;
           ForeColor = SystemColors.InfoText;

           SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
           ShowInTaskbar = false;
           StartPosition = FormStartPosition.Manual;

           paintSize = new Size(120, 15);
           Size = paintSize;
           Region = new Region(new Rectangle(new Point(0, 0), paintSize));
// can't actually have a form this small, set the paint region
       }

       private Size paintSize;

       public new void Show()
       {
           ShowWindow(new HandleRef(this, Handle), 4);
           SetWindowPos(new HandleRef(this, Handle), new IntPtr(-2), Left,
Top, Width, Height, 0x10);
       }

       protected override void OnPaint(PaintEventArgs e)
       {
           base.OnPaint(e);
           e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0, 0,
paintSize.Width - 1, paintSize.Height - 1));
           using (SolidBrush brush = new SolidBrush(ForeColor))
           {
               e.Graphics.DrawString("My Tip", Font, brush, 0, 0);
           }
       }

       [DllImportAttribute("user32.dll", ExactSpelling=true,
CharSet=CharSet.Auto)]
       static extern bool SetWindowPos(HandleRef hWnd, IntPtr
hWndInsertAfter, int x, int y, int cx, int cy, int flags);

       [DllImportAttribute("user32.dll", ExactSpelling=true,
CharSet=CharSet.Auto)]
       static extern bool ShowWindow(HandleRef hWnd, int nCmdShow);
   }
}

Hope this helps.

Regards,
Matt Garven

> I need to make a control that behaves like a tool tip, but I need to be able
> to draw on it and I also need to show it on demand, not necessarily during a
[quoted text clipped - 4 lines]
> I could do something with NativeWindow, but I wanted to check here for
> something easier. I'd like to avoid the windows API if possible.
RichM - 10 Feb 2005 04:03 GMT
Hmm, deriving from Form is interesting. I didn't know about the
FormBorderStyle property.

Why did you have to override Show() with the API functions? Why did the
default not work for you?

> We investigated Tooltips a while back and found (sadly) that the .NET Tooltip
> classes are not designed with extensibility in mind. We wrote our own in the
[quoted text clipped - 72 lines]
> > I could do something with NativeWindow, but I wanted to check here for
> > something easier. I'd like to avoid the windows API if possible.
Matt Garven - 10 Feb 2005 04:13 GMT
Well sadly you can't override Show(), I just redeclared it for clarity. You
need to use the API functions so that showing the tooltip form doesn't steal
focus from the form the user is currently using.

You need to use a Form instead of Control because your tooltips may show
over the edge of the form that the tooltip is appearing on - it would look
strange to have the tooltip truncated at the edge of the form.

Hope this helps.

Regards,
Matt

> Hmm, deriving from Form is interesting. I didn't know about the
> FormBorderStyle property.
[quoted text clipped - 78 lines]
> > > I could do something with NativeWindow, but I wanted to check here for
> > > something easier. I'd like to avoid the windows API if possible.
Steve Austin - 10 Feb 2005 16:32 GMT
Rich,

Take a look at www.tooltips.net, it may have exactly what you need.

Steve

>I need to make a control that behaves like a tool tip, but I need to be
>able
[quoted text clipped - 7 lines]
> I could do something with NativeWindow, but I wanted to check here for
> something easier. I'd like to avoid the windows API if possible.

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.