Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / Windows Forms

C# WinForms Application Full Integration with HTMLHelp ( .chm ) - Help Topics, Context Sensitive Help, and Tooltips

Rate me:
Please Sign up or sign in to vote.
4.67/5 (12 votes)
4 May 2011CPOL5 min read 64K   2.2K   48  
Use HTMLHelp (.chm) to display help topics, context sensitive help and tooltips in C# Winform application
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace WinHelpLib
{


    /// <summary>
    /*
        TableOfContents - opens table of contents tab only - keyword is unused
        Topic - opens a topic in the current tab - keyword is the path to the topic
        TopicId - opens a topic in the current tab - keyword is the id (numeric only) of the topic in the [MAP] section (.h file)
        Index - opens the index tab - keyword fils the edit and selects accordingsly in the list below withouth chaning the displayed topic 
        Find - opens the find tab - keyword is unused
        AssociateIndex - does not seem to work properly 
        KeywordIndex - opens a topic in the current tab (sometimes changes to index tab) - keyword (sometimes changes the textbox and list index in index tab) changes the displayed topic to the selected one 
    */
    /// </summary>


    class HTMLHelpEx
    {

        const int HH_DISPLAY_TEXT_POPUP = 0x000E;

        [StructLayout(LayoutKind.Sequential)]
        struct Point
        {
            public Int32 x;
            public Int32 y;
        };

        [StructLayout(LayoutKind.Sequential)]
        struct Rect
        {
            public Int32 left;
            public Int32 top;
            public Int32 right;
            public Int32 bottom;
        };


        [StructLayout(LayoutKind.Sequential)]
        struct HH_POPUP
        {
            public Int32 cbStruct;
            public IntPtr hinst;              //4 bytes on x86, 8 on x64
            public UInt32 idString;
            [MarshalAs(UnmanagedType.LPWStr)] //Unicode
            public String pszText;
            public Point pt;
            public UInt32 clrForeground;
            public UInt32 clrBackground;
            public Rect rcMargins;
            [MarshalAs(UnmanagedType.LPWStr)] //Unicode
            public String pszFont;
        };


        [DllImport("hhctrl.ocx", CharSet = CharSet.Unicode, EntryPoint = "HtmlHelpW")]
        static extern IntPtr HtmlHelp(
           IntPtr caller,   //4 bytes on x86, 8 on x64
           String file,
           uint command,
           IntPtr data      //4 bytes on x86, 8 on x64
        );


        string sHelpFileName;
        string sCSStreamName;

        public HTMLHelpEx(string sName,string sCSName)
        {
            sHelpFileName = sName;
            sCSStreamName = sCSName;
        }

        public void ShowHelp(Control ctrl, HelpNavigator hn, object oKeyword)
        {
            Help.ShowHelp(ctrl, sHelpFileName, hn, oKeyword);
        }


        public void ShowContextHelp(uint idString, Control ctrl)
        {
            HH_POPUP sPUp = new HH_POPUP();

            sPUp.cbStruct = Marshal.SizeOf(sPUp);
            sPUp.hinst = IntPtr.Zero;
            sPUp.idString = idString;
            sPUp.pszText = String.Empty;
            sPUp.pt = new Point()
            {
                x = ctrl.PointToScreen(new System.Drawing.Point()).X,
                y = ctrl.PointToScreen(new System.Drawing.Point()).Y
            };
            sPUp.clrForeground = 0;
            sPUp.clrBackground = 0;
            sPUp.rcMargins = new Rect()
            {
                left = 10,
                top = 10,
                right = 10,
                bottom = 10
            };

            IntPtr ptrData = Marshal.AllocHGlobal(Marshal.SizeOf(sPUp));
            Marshal.StructureToPtr(sPUp, ptrData, false);
            IntPtr Result = HtmlHelp(ctrl.Handle, sHelpFileName + "::/" + sCSStreamName, HH_DISPLAY_TEXT_POPUP, ptrData);//(UInt32)
            Marshal.FreeHGlobal(ptrData);

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions