2010-05-13 22 views
12

Mi aplicación inicia otra aplicación externa.Eliminar la barra de título de la aplicación externa usando C#

Quiero eliminar la barra de título de esta aplicación externa una vez que ha comenzado.

¿Es esto factible? De ser así, ¿cómo se haría?

Sobre la base de los comentarios que estoy usando el código de trabajo por debajo de

//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); 

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 


//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 

public void WindowsReStyle() 
{ 
    Process[] Procs = Process.GetProcesses(); 
    foreach (Process proc in Procs) 
    { 
     if (proc.ProcessName.StartsWith("notepad")) 
     { 
      IntPtr pFoundWindow = proc.MainWindowHandle; 
      int style = GetWindowLong(pFoundWindow, GWL_STYLE); 
      SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION)); 
     } 
    } 
} 
+0

'public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; 'es incorrecto ya que WS_DLGFRAME no forma parte de la leyenda. Referencia: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx –

Respuesta

9

No hay necesidad de inyectar nada, sólo puede modificar los bits de estilo ventanas como usar la API, por ejemplo esto funciona para el Bloc de notas, sin embargo, YMMV depende de la aplicación con la que juegues.

alt text http://img297.imageshack.us/img297/8580/40498359.png

//Get current style 
lCurStyle = GetWindowLong(hwnd, GWL_STYLE) 

//remove titlebar elements 
lCurStyle = lCurStyle And Not WS_CAPTION 
lCurStyle = lCurStyle And Not WS_SYSMENU 
lCurStyle = lCurStyle And Not WS_THICKFRAME 
lCurStyle = lCurStyle And Not WS_MINIMIZE 
lCurStyle = lCurStyle And Not WS_MAXIMIZEBOX 

//apply new style 
SetWindowLong hwnd, GWL_STYLE, lCurStyle 

//reapply a 3d border 
lCurStyle = GetWindowLong(hwnd, GWL_EXSTYLE) 

SetWindowLong hwnd, GWL_EXSTYLE, lCurStyle Or WS_EX_DLGMODALFRAME 

//redraw 
SetWindowPos hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED 
+0

puede ser más elaborado con el código. Gracias. – Anuya

+0

Sure; http://stackoverflow.com/questions/2832828/how-to-remove-the-menubar-of-an-application-using-windows-api;) –

+0

La URL de imageshack parece estar "muerta"? –

2

En general, no se puede hacer eso a menos que haya un apoyo directo en la aplicación va a iniciar (por ejemplo, si se necesita un cambio de línea de comando para eliminar la barra de título).

Solo puede controlar las cosas que ya están presentes en la clase ProcessStartInfo (es decir, abrir una nueva ventana, iniciar minimizado/maximizado, etc.).

+0

¿Hay alguna otra forma de ocultar la barra de título de esta aplicación de terceros? Solo tiene una forma para la cual necesito eliminarla. – Anuya

+0

@karthik: solo si ya admite esta funcionalidad. Esto no es algo que usted pueda controlar directamente. – Oded

1

Esto es muy similar a una pregunta anterior, y estoy bastante seguro de que la respuesta es que no puede hacerlo. (O, si es posible, es necesario profundizar en la API de Windows, que puede ser un reto, dependiendo de su experiencia.)

How to add button to other apps window title bar (XP/Vista)

+1

Esto no es correcto. Puede hacerlo, y las soluciones enumeradas aquí funcionan. –

2

Bueno, Alex nunca se elaboró ​​con el código, bueno al menos no era una solución plug-n-play, pero aún así el crédito principal de esto va a él ... ES un tipo de buggy a menos que use "SetParent" para ponerlo en algún tipo de contenedor (como un formulario o panel) Solo pensé en compartir el resultado.

de Visual Basic:

Option Strict On 
Public Class Form1 
    Const WS_BORDER As Integer = 8388608 
    Const WS_DLGFRAME As Integer = 4194304 
    Const WS_CAPTION As Integer = WS_BORDER Or WS_DLGFRAME 
    Const WS_SYSMENU As Integer = 524288 
    Const WS_THICKFRAME As Integer = 262144 
    Const WS_MINIMIZE As Integer = 536870912 
    Const WS_MAXIMIZEBOX As Integer = 65536 
    Const GWL_STYLE As Integer = -16& 
    Const GWL_EXSTYLE As Integer = -20& 
    Const WS_EX_DLGMODALFRAME As Integer = &H1L 
    Const SWP_NOMOVE As Integer = &H2 
    Const SWP_NOSIZE As Integer = &H1 
    Const SWP_FRAMECHANGED As Integer = &H20 
    Const MF_BYPOSITION As UInteger = &H400 
    Const MF_REMOVE As UInteger = &H1000 
    Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer 
    Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer 
    Declare Auto Function SetWindowPos Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean 
    Public Sub MakeExternalWindowBorderless(ByVal MainWindowHandle As IntPtr) 
     Dim Style As Integer 
     Style = GetWindowLong(MainWindowHandle, GWL_STYLE) 
     Style = Style And Not WS_CAPTION 
     Style = Style And Not WS_SYSMENU 
     Style = Style And Not WS_THICKFRAME 
     Style = Style And Not WS_MINIMIZE 
     Style = Style And Not WS_MAXIMIZEBOX 
     SetWindowLong(MainWindowHandle, GWL_STYLE, Style) 
     Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE) 
     SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style Or WS_EX_DLGMODALFRAME) 
     SetWindowPos(MainWindowHandle, New IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED) 
    End Sub 
End Class 

C Sharp (C#)

using System.Runtime.InteropServices; 
public class Form1 
{ 
    const int WS_BORDER = 8388608; 
    const int WS_DLGFRAME = 4194304; 
    const int WS_CAPTION = WS_BORDER | WS_DLGFRAME; 
    const int WS_SYSMENU = 524288; 
    const int WS_THICKFRAME = 262144; 
    const int WS_MINIMIZE = 536870912; 
    const int WS_MAXIMIZEBOX = 65536; 
    const int GWL_STYLE = -16L; 
    const int GWL_EXSTYLE = -20L; 
    const int WS_EX_DLGMODALFRAME = 0x1L; 
    const int SWP_NOMOVE = 0x2; 
    const int SWP_NOSIZE = 0x1; 
    const int SWP_FRAMECHANGED = 0x20; 
    const uint MF_BYPOSITION = 0x400; 
    const uint MF_REMOVE = 0x1000; 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
    public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); 
    public void MakeExternalWindowBorderless(IntPtr MainWindowHandle) 
    { 
     int Style = 0; 
     Style = GetWindowLong(MainWindowHandle, GWL_STYLE); 
     Style = Style & !WS_CAPTION; 
     Style = Style & !WS_SYSMENU; 
     Style = Style & !WS_THICKFRAME; 
     Style = Style & !WS_MINIMIZE; 
     Style = Style & !WS_MAXIMIZEBOX; 
     SetWindowLong(MainWindowHandle, GWL_STYLE, Style); 
     Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE); 
     SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style | WS_EX_DLGMODALFRAME); 
     SetWindowPos(MainWindowHandle, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); 
    } 
} 

Una vez más, gracias a que Alex

+1

Hola, gracias por tu código, me funciona después de corregir algunos errores en la sección C#. Style &! WS_CAPTION no funciona. Necesita escribir Style & ~ WS_CAPTION. –

5
Process[] processes = Process.GetProcessesByName("notepad"); 
IntPtr windowHandle = processes[0].MainWindowHandle; 

const int GWL_STYLE = (-16); 
const UInt32 WS_VISIBLE = 0x10000000; 
SetWindowLong(windowHandle, GWL_STYLE, (WS_VISIBLE));` 
Cuestiones relacionadas