2010-05-14 4 views
1

Lorsque j'ai recherché sur Google comment changer le style de la fenêtre, je pouvais trouver le code en langage "C" Comment puis-je utiliser l'extrait ci-dessous dans mon application C#, pour cacher le titre Barre d'application externe? Je ne l'ai pas utilisé « C » avant ..Utilisation du code C dans 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

Vous voulez changer un style de fenêtres? - est-ce pour votre application ou toutes les fenêtres? Winforms, ASP, WPF? Je remarque que c'est la même chose qu'un autre post que vous avez. – ChrisBD

+0

@ChrisBD, je veux cacher la barre de titre d'une application [Third party] qui n'a qu'un seul formulaire. Et cette application est démarrée par mon application C# windows. Ce n'est pas pour toutes les fenêtres. – Anuya

Répondre

2

Je ne suis pas un P/Invoke exprert, mais en regardant: http://www.pinvoke.net/default.aspx/coredll/SetWindowLong.html Je suppose que vous pouvez appeler SetWindowLong pour changer le style de la fenêtre sans reparent.

Pour la recherche de la fenêtre, vous pouvez essayer ceci:

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

L'extrait que vous avez posté est en C#, pas C. Vous devriez être en mesure d'ajouter que le code à votre formulaire et appelez la méthode Reparent. (En supposant que vous utilisez WinForms)

Notez que la méthode Reparent modifiera non seulement le style de la fenêtre, mais tentera également d'afficher la fenêtre en tant qu'enfant de votre fenêtre.

+0

Comment puis-je transmettre le nom de l'application à cette fonction. Dites "NotePad.exe", de sorte qu'il supprime la barre de titre du bloc-notes. Aidez-moi aussi à éviter le Reparent. Merci. – Anuya

+0

@karthik vous devez changer le 'FindWindow' (qui recherche une fenêtre par sa classe) avec la méthode @digEmAll pour trouver la fenêtre principale pour un processus particulier. –