1. Computing & Technology

Discuss in my forum

Execute a Custom Action on the Form's Help button Click

By , About.com Guide

Custom biHelp Action

If the form's BorderStyle property is bsDialog and biHelp is included in BorderIcons, a question mark appears in the form's title bar and when clicked, the cursor changes to crHelp.

If you want to execute some custom code when the user clicks the Help button, you need to handle two Windows messages: WM_NCLBUTTONUP and WM_NCLBUTTONDOWN. Those messages are posted to the window when the user presses and releases the left mouse button while the cursor is within the non-client area of a window.

Here's a simple implementation of the custom biHelp-click action:

 type
    THelpForm = class(TForm)
    private
      procedure WMNCLBUTTONDOWN(var Msg: TWMNCLButtonDown) ; message WM_NCLBUTTONDOWN;
      procedure WMNCLBUTTONUP(var Msg: TWMNCLButtonUp) ; message WM_NCLBUTTONUP;
    end;
 
 var
    HelpForm: THelpForm;
 
 implementation
 {$R *.dfm}
 
 procedure THelpForm.WMNCLBUTTONDOWN(var Msg: TWMNCLButtonDown) ;
 begin
    if Msg.HitTest = HTHELP then
      Msg.Result := 0 // "eat" the message
    else
      inherited;
 end;
 
 procedure THelpForm.WMNCLBUTTONUP(var Msg: TWMNCLButtonUp) ;
 begin
    if Msg.HitTest = HTHELP then
    begin
      Msg.Result := 0;
      ShowMessage('Need help?') ;
    end
    else
      inherited;
 end; 

Delphi tips navigator:
» How to Draw Custom Text on a Form's Caption Bar
« How to "Pan" an Image inside a ScrollBox

©2012 About.com. All rights reserved.

A part of The New York Times Company.