Link to home
Start Free TrialLog in
Avatar of shmitnols
shmitnols

asked on

using Help.ShowHelp ... how to maximize and or close help window??

I am opening Help using C# built in help, such as
Help.ShowHelp(this, "help.chm");

Is there a way to mazimize the help window that opens, and how do I close it?
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

I don't think there's an easy way because the method does not return a window handle.
You could use the findwindow api asing the lpClassName of "HH Parent" which is what the help uses.
once you have the window handle you should be able to maximise and/or close it.
DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Sorry forgot the other two :

[DllImport("user32.dll")]
static extern bool SetWindowPlacement(IntPtr hWnd,   [In] ref WINDOWPLACEMENT lpwndpl);

Can be used to maximise window.

[DllImport("user32.dll")]
static extern bool CloseWindow(IntPtr hWnd);


Can be used to close window.


Fisrt you need to define these :

      [StructLayout(LayoutKind.Sequential)]
      public struct WINDOWPLACEMENT {
            public int length;
            public int flags;
            public int showCmd;
            public POINT ptMinPosition;
            public POINT ptMaxPosition;
            public RECT rcNormalPosition;
      }

      [StructLayout( LayoutKind.Sequential )]
      public struct POINT {
            public int X;
            public int Y;

            public POINT( int x, int y ) {
                  this.X = x;
                  this.Y = y;
            }

            public static implicit operator System.Drawing.Point( POINT p ) {
                  return new System.Drawing.Point( p.X,  p.Y );
            }

            public static implicit operator POINT( System.Drawing.Point p ) {
                  return new POINT( p.X, p.Y );
            }
      }

      [Serializable, StructLayout(LayoutKind.Sequential)]
      public struct RECT {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;

            public RECT(int left_, int top_, int right_, int bottom_) {
                  Left = left_;
                  Top = top_;
                  Right = right_;
                  Bottom = bottom_;
            }

            public int Height { get { return Bottom - Top; } }
            public int Width { get { return Right - Left; } }
            public Size Size { get { return new Size(Width, Height); } }

            public Point Location { get { return new Point(Left, Top); } }

            public Rectangle ToRectangle() {
          return Rectangle.FromLTRB(Left, Top, Right, Bottom); }

            public static RECT FromRectangle(Rectangle rectangle) {
                  return new RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
            }

            public override int GetHashCode() {
                  return Left ^ ((Top << 13) | (Top >> 0x13))
                        ^ ((Width << 0x1a) | (Width >> 6))
                        ^ ((Height << 7) | (Height >> 0x19));
            }

            #region Operator overloads

            public static implicit operator Rectangle( RECT rect ) {
                  return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom);
            }

            public static implicit operator RECT( Rectangle rect ) {
                  return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
            }

            #endregion
      }

      public enum SHOWWINDOW : uint {
            SW_HIDE         = 0,
            SW_SHOWNORMAL       = 1,
            SW_NORMAL       = 1,
            SW_SHOWMINIMIZED    = 2,
            SW_SHOWMAXIMIZED    = 3,
            SW_MAXIMIZE     = 3,
            SW_SHOWNOACTIVATE   = 4,
            SW_SHOW         = 5,
            SW_MINIMIZE     = 6,
            SW_SHOWMINNOACTIVE  = 7,
            SW_SHOWNA       = 8,
            SW_RESTORE      = 9,
            SW_SHOWDEFAULT      = 10,
            SW_FORCEMINIMIZE    = 11,
            SW_MAX          = 11,
      }
Finally these three functions :

            private void ShowHelp_Click(object sender, System.EventArgs e) {
                  Help.ShowHelp(this, @"path_to_your_chm_file");
            }

            private void CloseHelp_Click(object sender, System.EventArgs e) {
                  IntPtr hWnd = FindWindow("HH Parent", null);
                  if (hWnd.ToInt32() != 0){
                        CloseWindow(hWnd);
                  }
            }

            private void MaxHelp_Click(object sender, System.EventArgs e) {
                  IntPtr hWnd = FindWindow("HH Parent", null);
                  if (hWnd.ToInt32() != 0){
                        WINDOWPLACEMENT WP = new WINDOWPLACEMENT();
                        WP.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
                        WP.showCmd = (int)SHOWWINDOW.SW_MAXIMIZE;
                        SetWindowPlacement(hWnd, ref WP);
                  }
            }
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial