2010-05-14 76 views
1

當我用Google搜索找到改變窗口的風格我能找到「C」的代碼語言 我怎麼能在我的C#應用​​程序中使用下面的代碼片段的方式,這樣我可以隱藏標題外部應用程序?我沒有用「C」之前..用C代碼

//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)); 
} 
+0

你想改變一個窗口的風格? - 這是你的應用程序還是所有窗口? Winforms,ASP,WPF? 我注意到,這是一樣的,你有另一篇文章。 – ChrisBD 2010-05-14 06:31:40

+0

@ChrisBD,我想隱藏的應用[第三方]它只有一個窗體的標題欄。而這個應用程序是由我的C#窗口應用程序啓動的。它不適用於所有窗口。 – Anuya 2010-05-14 06:35:14

回答

2

我不是P/Invoke exprert,但看着:http://www.pinvoke.net/default.aspx/coredll/SetWindowLong.html我想你可以調用SetWindowLong來改變窗口樣式而不用重新打開。

對於窗口搜索,你可以試試這個:

Process[] processes = Process.GetProcessesByName("notepad.exe"); 
foreach (Process p in processes) 
{ 
    IntPtr pFoundWindow = p.MainWindowHandle; 
    // now you have the window handle 
} 
1

您發佈的片段是在C#中,而不是在C.你應該能夠做到這代碼添加到您的形式和調用Reparent方法。 (假設你正在使用WinForms

注意,Reparent方法不僅會改變窗口的風格,同時也將嘗試父窗口的窗口的子窗口。

+0

如何傳遞的應用程序名稱此功能。說「NotePad.exe」,以便它刪除記事本的標題欄。 也幫助我避免重新設置父級。謝謝。 – Anuya 2010-05-14 07:10:43

+0

@karthik你需要尋找特定進程的主窗口的@digEmAll辦法改變'FindWindow'(它通過它的類查找窗口)。 – 2010-05-14 15:25:45