Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Hide window caption in c#?

Back to Forum: Tech Off
  • User profile image
    quantass

    Im trying to figure out how to hide the caption bar of my window once maximized.  After using an external tool and using Spy++ i see the mention of WM_STYLECHANGED when the CAPTION is hidden.  Im completely unfamkilar with making win32api calls from my c# code but im guessing SendMessage is required so that i can hide the caption on a widow programmatically.

    Can someone provide me with the win32 api command to hide the caption of a window for c#

  • User profile image
    JPeless

    If you are working with C# you can probably do this without a win32 call....the following did it (although you might need to show or not show minimized buttons, etc. as you desire...

    if ( WindowState != FormWindowState.Maximized )
    {
       this.ControlBox = false;
       this.Text = String.Empty;
       this.WindowState = FormWindowState.Maximized;
    }
    else
    {
       this.ControlBox = true;
       this.Text = "Hey";
       this.WindowState = FormWindowState.Normal;
    }

  • User profile image
    Cannot​Resolve​Symbol

    JPeless wrote:
    

    If you are working with C# you can probably do this without a win32 call....the following did it (although you might need to show or not show minimized buttons, etc. as you desire...

    if ( WindowState != FormWindowState.Maximized )
    {
       this.ControlBox = false;
       this.Text = String.Empty;
       this.WindowState = FormWindowState.Maximized;
    }
    else
    {
       this.ControlBox = true;
       this.Text = "Hey";
       this.WindowState = FormWindowState.Normal;
    }



    Or, if you want to get rid of the whole title bar, just change the border style of the form (can't think of what the exact name of the property is or what you need to set it to, but it's in the designer and the possible values are self-explanatory).

  • User profile image
    Ion Todirel

    CannotResolveSymbol wrote:
    
    JPeless wrote: 

    If you are working with C# you can probably do this without a win32 call....the following did it (although you might need to show or not show minimized buttons, etc. as you desire...

    if ( WindowState != FormWindowState.Maximized )
    {
       this.ControlBox = false;
       this.Text = String.Empty;
       this.WindowState = FormWindowState.Maximized;
    }
    else
    {
       this.ControlBox = true;
       this.Text = "Hey";
       this.WindowState = FormWindowState.Normal;
    }



    Or, if you want to get rid of the whole title bar, just change the border style of the form (can't think of what the exact name of the property is or what you need to set it to, but it's in the designer and the possible values are self-explanatory).
    FormBorderStyle to None

  • User profile image
    Cannot​Resolve​Symbol

    Ion Todirel wrote:
    
    CannotResolveSymbol wrote:  Or, if you want to get rid of the whole title bar, just change the border style of the form (can't think of what the exact name of the property is or what you need to set it to, but it's in the designer and the possible values are self-explanatory).
    FormBorderStyle to None


    That one.  Tongue Out

  • User profile image
    quantass

    Excellent stuff. Definietly something i will use.

    One thing i neglected to add is Im trying to get the MS Word COM control embedded into my form.  It currently is embedding but i can see the titlebar and resizing icon.  I was hoping to remove both to give the illusion that MS Word is properly embedded inside my form instead of appearing as a hovering window.

    Because of this, i only have a handle integer to the Word object and soi that is why i was considering Win32 to come to the rescue to modify the window's appearance.

     

     

  • User profile image
    Cannot​Resolve​Symbol

    quantass wrote:
    

    Excellent stuff. Definietly something i will use.

    One thing i neglected to add is Im trying to get the MS Word COM control embedded into my form.  It currently is embedding but i can see the titlebar and resizing icon.  I was hoping to remove both to give the illusion that MS Word is properly embedded inside my form instead of appearing as a hovering window.

    Because of this, i only have a handle integer to the Word object and soi that is why i was considering Win32 to come to the rescue to modify the window's appearance.



    Try:

    //Finds a window by class name
    [DllImport("USER32.DLL")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    //Sets a window to be a child window of another window
    [DllImport("USER32.DLL")]
    public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    //Sets window attributes
    [DllImport("USER32.DLL")]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    //Gets window attributes
    [DllImport("USER32.DLL")]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    //assorted constants needed
    public static int GWL_STYLE = -16;
    public static int WS_CHILD = 0x40000000; //child window
    public static int WS_BORDER = 0x00800000; //window with border
    public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
    public static int WS_CAPTION= WS_BORDER | WS_DLGFRAME; //window with a title bar

    /*
     This function sets the parent of the window with class
     ClassClass to the form/control the method is in.
    */
    public void Reparent()
    {
    //get handle of parent form (.net property)
    IntPtr par = this.Handle;
    //get handle of child form (win32)
    IntPtr child = FindWindow("ClassClass", null);
    //set parent of child form
    SetParent(child, par);
    //get current window style of child form
    int style = GetWindowLong(child, GWL_STYLE);

    //take current window style and remove WS_CAPTION from it
    SetWindowLong(child, GWL_STYLE, (style & ~WS_CAPTION));
    }

    I'm also reparenting the form here, but it's up to you to maximize it so it fills up the parent form.

    [edit] added documentation

  • User profile image
    seanzaphod

    Great code sample - it was very helpful to me as I was able to get rid of a titlebar/caption on another application.

    I have another issue you might be able to help with:

    Is there a way in WIN32/MFC to hide an external applications menubar?


Conversation locked

This conversation has been locked by the site admins. No new comments can be made.