2013-09-06 30 views
7

Tôi có danh sách các Ứng dụng đang mở. Để có được danh sách này tôi đã sử dụng đoạn mã sauCách thu nhỏ/tối đa hóa các Ứng dụng đã mở

internal static class NativeMethods 
{ 
    public static readonly Int32 GWL_STYLE = -16; 
    public static readonly UInt64 WS_VISIBLE = 0x10000000L; 
    public static readonly UInt64 WS_BORDER = 0x00800000L; 
    public static readonly UInt64 DESIRED_WS = WS_BORDER | WS_VISIBLE; 

    public delegate Boolean EnumWindowsCallback(IntPtr hwnd, Int32 lParam); 

    public static List<WindowWrapper> GetAllWindows() 
    { 
     List<WindowWrapper> windows = new List<WindowWrapper>(); 
     StringBuilder buffer = new StringBuilder(100); 
     EnumWindows(delegate(IntPtr hwnd, Int32 lParam) 
     { 
      if ((GetWindowLongA(hwnd, GWL_STYLE) & DESIRED_WS) == DESIRED_WS) 
      { 
       GetWindowText(hwnd, buffer, buffer.Capacity); 
       WindowWrapper wnd = new WindowWrapper(); 
       wnd.handle = hwnd; 
       wnd.title = buffer.ToString(); 
       windows.Add(wnd); 
      } 
      return true; 
     }, 0); 

     return windows; 
    } 

    [DllImport("user32.dll")] 
    static extern Int32 EnumWindows(EnumWindowsCallback lpEnumFunc, Int32 lParam); 

    [DllImport("user32.dll")] 
    public static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount); 

    [DllImport("user32.dll")] 
    static extern UInt64 GetWindowLongA(IntPtr hWnd, Int32 nIndex); 
} 

public class WindowWrapper : IWin32Window 
{ 
    internal IntPtr handle; 
    internal String title; 

    public IntPtr Handle 
    { 
     get { return handle; } 
    } 

    public String Title 
    { 
     get { return title; } 
    } 
} 

gọi này tôi đã sử dụng đoạn mã sau

foreach (var wnd in NativeMethods.GetAllWindows()) 
     { 
       string caption = wnd.title; 
       string handle = wnd.Handle 
       // Add this caption and handle to list 
     } 

Bây giờ, người dùng sẽ chọn bất kỳ cửa sổ mở từ danh sách và nhiệm vụ của tôi là phải đọc chú thích của cửa sổ được chọn, xử lý quy trình và tối đa hóa/thu nhỏ hoặc đóng cửa sổ. Tôi có thể làm cái này như thế nào.

Trả lời

10

Bạn có thể sử dụng findwindowbycaption để có được xử lý sau đó tối đa hóa hoặc giảm thiểu với showwindow

private const int SW_MAXIMIZE = 3; 
private const int SW_MINIMIZE = 6; 
// more here: http://www.pinvoke.net/default.aspx/user32.showwindow 

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

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow); 

Sau đó trong mã của bạn, bạn sử dụng này:

IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "The window title"); 
ShowWindow(hwnd, SW_MAXIMIZE); 

Mặc dù có vẻ như bạn đã có chú thích cửa sổ bằng cách sử dụng EnumWindows trong trường hợp đó, bạn chỉ cần:

ShowWindow(windows[i].handle, SW_MAXIMIZE); 

i là chỉ mục của cửa sổ.

để đóng cửa sổ bạn sẽ sử dụng:

[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError=true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool DestroyWindow(IntPtr hwnd); 

trong các mã:

DestroyWindow(hwnd) //or DestroyWindow(windows[i].handle) 

này là phiên bản không được quản lý của system.windows.forms.form.close()

hoặc bạn có thể sử dụng:

Process [] proc Process.GetProcessesByName("process name"); 
proc[0].Kill(); 

hoặc bạn có thể sử dụng:

static uint WM_CLOSE = 0x0010; 
[return: MarshalAs(UnmanagedType.Bool)] 
[DllImport("user32.dll", SetLastError = true)] 
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 

trong mã:

PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); 
+0

Ok, cảm ơn. Bây giờ để đóng cửa sổ những gì tôi nên mã –

+0

cập nhật câu trả lời –

+0

DestroyWindow/CloseWindow không đóng cửa sổ, tại sao? –

5

Bạn có thể sử dụng phương pháp ShowWindow mẹ đẻ với SW_MAXIMIZE, SW_MINIMIZE cho ncmdShow Hãy xem http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx

private const int SW_MAXIMIZE = 3; 
private const int SW_MINIMIZE = 6; 

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow); 


// in your code 
ShowWindow(wnd.Handle, SW_MAXIMIZE); 
+0

bạn có thể vui lòng cho tôi biết làm thế nào để sử dụng? –

+0

Câu trả lời của bạn là đúng.chúng ta có thể nhận được xử lý hoặc cửa sổ nếu chúng ta chỉ biết chú thích của nó –

+0

Bạn có các chốt trong WindowWrapper của bạn, nó sẽ hiệu quả hơn để lưu trữ chúng dưới dạng chú thích cửa sổ có thể được thay đổi bởi chính ứng dụng đang chạy ở chế độ nền. –

2

bạn có thể sử dụng ShowWindowAsync

private const int SW_SHOWNORMAL = 1; 
private const int SW_SHOWMINIMIZED = 2; 
private const int SW_SHOWMAXIMIZED = 3; 

[DllImport("user32.dll")] 
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 


ShowWindowAsync(wnd.Handle, SW_SHOWMINIMIZED); 

và nó tốt hơn và sử dụng

var openWindows = Process.GetProcesses().Where(process=> String.IsNullOrEmpty(process.MainWindowTitle)==false); 

để mở các cửa sổ

Tôi đã thử nghiệm MainWindowTitle trong Porcess và nó giúp tìm kiếm trên cửa sổ cho chú thích của nó.

var handles = Process.GetProcesses().Where(x => x.MainWindowTitle == "Untitled - Notepad").Select(y=>y.Handle).ToList(); 
+0

bạn có biết mã để xử lý cửa sổ không nếu chúng tôi chỉ biết chú thích –

+2

xem chỉnh sửa giúp đỡ – Alyafey

+0

Cách duy nhất có thể làm việc cho ứng dụng WPF của tôi, cảm ơn! – Lee

Các vấn đề liên quan