1

When i googled to find the way to change the window style i could find the code in "C" language How can i use the snippet below in my c# application, so that i can hide the Title Bar of external application ? I have not used "C" before..

    //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));
}
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • You want to change a windows style? - is this for your application or all windows? Winforms, ASP, WPF? I note that this is the same as another post that you have. – ChrisBD May 14 '10 at 06:31
  • @ChrisBD, i want to hide the Title bar of an application [Third party] which has only one form. And this application is started by my c# windows application. It's not for all windows. – Anuya May 14 '10 at 06:35

2 Answers2

2

I'm not a P/Invoke exprert, but looking at: http://www.pinvoke.net/default.aspx/coredll/SetWindowLong.html I guess you can call SetWindowLong to change window style without reparent.

For the window search you can try this:

Process[] processes = Process.GetProcessesByName("notepad.exe");
foreach (Process p in processes)
{
    IntPtr pFoundWindow = p.MainWindowHandle;
    // now you have the window handle
}
digEmAll
  • 56,430
  • 9
  • 115
  • 140
1

The snippet you have posted is in C#, not in C. You should be able to just add that code to your form and call the Reparent method. (Assuming you are using WinForms)

Note that the Reparent method will not only change the window style, but will also attempt to parent the window as a child of your window.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • How can i pass the application name to this function. Say "NotePad.exe", so that it removes the title bar of notepad. Also help me to avoid the Reparent. Thanks. – Anuya May 14 '10 at 07:10
  • @karthik you need to change the `FindWindow` (which looks up a window by its class) with the @digEmAll way of finding the main window for particular process. – Franci Penov May 14 '10 at 15:25