2010-05-13 72 views
12

Ứng dụng của tôi khởi động một ứng dụng bên ngoài khác.Xóa thanh Tiêu đề của ứng dụng bên ngoài bằng cách sử dụng C#

Tôi muốn xóa thanh tiêu đề của ứng dụng bên ngoài này khi ứng dụng đã bắt đầu.

Điều này có khả thi không và nếu có thì làm thế nào?

Căn cứ ý kiến ​​Tôi đang sử dụng đoạn code làm việc dưới

//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; 'không chính xác vì WS_DLGFRAME không phải là một phần của chú thích. Tham khảo: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx –

Trả lời

9

Không cần phải tiêm bất cứ điều gì, bạn chỉ có thể sửa đổi các bit cửa sổ phong cách khi sử dụng API, ví dụ điều này làm việc cho Notepad, tuy nhiên YMMV tùy thuộc vào ứng dụng bạn chơi với.

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

u có thể phức tạp hơn với mã. Cảm ơn. – Anuya

+0

Chắc chắn; http://stackoverflow.com/questions/2832828/how-to-remove-the-menubar-of-an-application-using-windows-api;) –

+0

URL imageshack có vẻ là "đã chết"? –

2

Nói chung, bạn không thể làm điều đó trừ khi có sự ủng hộ trực tiếp tại bởi ứng dụng bạn đang bắt đầu (ví dụ, nếu cần chuyển đổi dòng lệnh để xóa thanh tiêu đề).

Bạn chỉ có thể kiểm soát những thứ đã có trên lớp ProcessStartInfo (ví dụ: mở cửa sổ mới, bắt đầu thu nhỏ/phóng to, v.v ...).

+0

Có cách nào khác để ẩn thanh tiêu đề của ứng dụng bên thứ ba này không? Nó chỉ có một hình thức mà tôi cần phải loại bỏ nó. – Anuya

+0

@karthik - Chỉ khi nó đã hỗ trợ chức năng này. Đây không phải là một cái gì đó _you_ có thể kiểm soát trực tiếp. – Oded

1

Điều này rất giống với câu hỏi đã được hỏi trước đây và tôi chắc rằng câu trả lời là bạn không thể làm điều đó. (Hoặc, nếu bạn có thể, bạn cần phải thâm nhập vào các API Windows, mà có thể là một thách thức, tùy thuộc vào kinh nghiệm của bạn.)

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

+1

Điều này không chính xác. Bạn có thể làm điều đó, và các giải pháp được liệt kê ở đây làm việc. –

2

Vâng, Alex không bao giờ xây dựng với mã, cũng ít nhất nó không phải là một giải pháp plug-n-play, nhưng vẫn là tín dụng chính của việc này đi với anh ... Nó IS loại buggy trừ khi bạn sử dụng "SetParent" để đặt nó trong một số loại container (chẳng hạn như một hình thức hoặc bảng điều khiển) Chỉ cần nghĩ rằng tôi sẽ chia sẻ kết quả.

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

Một lần nữa, cảm ơn bạn Alex

+1

Xin chào, cảm ơn mã của bạn, nó hoạt động cho tôi sau khi sửa một số lỗi trong phần C#. Style &! WS_CAPTION không hoạt động. Bạn cần phải viết 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));` 
Các vấn đề liên quan