Click here to Skip to main content
Click here to Skip to main content

Tagged as

Help Button Emulation

, 25 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Help button emulation using Win32 API function

Introduction

Some time ago, while I was writing an application, I required a Help Button. The functionality of this button is widely known: by clicking on this first and then on any control of the client area, you will receive a tooltip including help and information about the element itself.
Adding a Help Button in a C# program is relatively simple. 

For this purpose, there is a property named Form.HelpButton, which when true, makes a small button with a question mark appear in the caption bar to the left of the Close button. At this point, however, a problem arises, the property is ignored if one (or both), of these two properties, Form.MinimizeBox and Form.MaximizeBox are true. In other words, to have a Help Button, we should renounce to Window's minimization and maximization.
So, is there a way to avoid this? The answer is yes, by using help button emulation!

The Code

To resolve this task we will use a function, from the Win32 API called SendMessage. This function, which sends a message to a window, called with some specific parameters is all we need. That said, the first thing to do is to import the DLL which contains SendMessage and declare two constants we need:

[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )]
private static extern IntPtr SendMessage
	( IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam );

private const int WM_SYSCOMMAND = 0x112;
private const int SC_CONTEXTHELP = 0xf180;

Once this is done, we must use a HelpProvider object whose job is to intercept the help request and show the appropriate tooltip:

private System.Windows.Forms.HelpProvider helpProvider1;

// Set the Help string associated with the textBox1.
helpProvider1.SetHelpString( textBox1, "Sample help string." );
helpProvider1.SetShowHelp( textBox1, true );

Finally, we deal with the real help button's emulation. In the Click event, we must call the SendMessage function with appropriate parameters:

private void btnHelp_Click( object sender, EventArgs e )
{
 // Setting Capture to false, will make the control 
 // not receive all mouse events: the window procedure doesn't receive
 // messages when the mouse is outside the window's client area.
 btnHelp.Capture = false;
 // Simulate the help button press with SendMessage function.
 SendMessage( this.Handle, WM_SYSCOMMAND, ( IntPtr )SC_CONTEXTHELP, IntPtr.Zero );
}

That's all! In these simple steps, we have a window containing both a Help Button and minimize and maximize.

History

  • 25th March, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Salvatore Tipaldi
Software Developer
Italy Italy
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionAny way to make it work for a ToolStripButton? PinmemberNev16-Sep-09 22:14 
AnswerRe: Any way to make it work for a ToolStripButton? PinmemberSalvatore Tipaldi17-Sep-09 2:42 
GeneralRe: Any way to make it work for a ToolStripButton? PinmemberNev17-Sep-09 14:10 
GeneralRe: Any way to make it work for a ToolStripButton? PinmemberMember 372698110-Feb-12 23:36 
GeneralThanks :) Pinmemberspif200130-Mar-09 22:13 
GeneralRe: Thanks :) PinmemberSalvatore Tipaldi31-Mar-09 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 25 Mar 2009
Article Copyright 2009 by Salvatore Tipaldi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid