Using C code in c#
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 h开发者_Python百科WndChild, 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 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
}
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.
精彩评论