2010-12-14 68 views
6

Có cách nào để đưa con trỏ HWnd đến cửa sổ trên cùng của Visual Studio 2010 từ phần mở rộng VSIX không? (Tôi muốn thay đổi tiêu đề của cửa sổ).HWnd của Visual Studio 2010

Trả lời

3

Vì có cơ hội tốt mà mở rộng VSIX của bạn sẽ được chạy trong quá trình với Visual Studio, bạn nên cố gắng này:

System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle 

(Lưu ý nếu bạn làm điều này quá sớm, bạn sẽ nhận được VS Splash màn hình ...)

3

Tôi giả sử bạn muốn thực hiện điều này một cách có lập trình trong C#?

Bạn sẽ cần phải xác định P/Invoke bên trong lớp học của bạn:

[DllImport("user32.dll")] 
static extern int SetWindowText(IntPtr hWnd, string text); 

Sau đó, có một số mã trông tương tự như sau:

Process visualStudioProcess = null; 
//Process[] allProcesses = Process.GetProcessesByName("VCSExpress"); // Only do this if you know the exact process name 
// Grab all of the currently running processes 
Process[] allProcesses = Process.GetProcesses(); 
foreach (Process process in allProcesses) 
{ 
    // My process is called "VCSExpress" because I have C# Express, but for as long as I've known, it's been called "devenv". Change this as required 
    if (process.ProcessName.ToLower() == "vcsexpress" || 
     process.ProcessName.ToLower() == "devenv" 
     /* Other possibilities*/) 
    { 
     // We have found the process we were looking for 
     visualStudioProcess = process; 
     break; 
    } 
} 

// This is done outside of the loop because I'm assuming you may want to do other things with the process 
if (visualStudioProcess != null) 
{ 
    SetWindowText(visualStudioProcess.MainWindowHandle, "Hello World"); 
} 

Doc trên Process: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

Tài liệu trên P/Gọi: http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx

. Thử mã này trên máy cục bộ của tôi, có vẻ như đặt tiêu đề cửa sổ, nhưng Visual Studio ghi đè nó trong nhiều điều kiện: tăng tiêu điểm, vào/rời khỏi chế độ gỡ lỗi ... Điều này có thể rắc rối.

Lưu ý: Bạn có thể TẢI tiêu đề cửa sổ ngay từ đối tượng Quy trình, nhưng bạn không thể đặt nó.

+0

Cảm ơn thông tin. Điều này đã dẫn tôi đến một giải pháp đơn giản hơn. Process.GetCurrentProcess() MainWindowHandle –

+1

VS sẽ tiếp tục ghi đè lên tiêu đề. Nó được thảo luận ở đây: http://stackoverflow.com/questions/577188/how-to-change-the-title-bar-text-of-visual-studio –

3

Bạn có thể sử dụng EnvDTE API để có được những hWnd của cửa sổ chính:

var hwndMainWindow = (IntPtr) dte.MainWindow.HWnd; 

trong Visual Studio 2010/2012, cửa sổ chính và một phần của điều khiển người dùng được triển khai bằng WPF. Bạn có thể ngay lập tức nhận được cửa sổ WPF của cửa sổ chính và làm việc với nó. Tôi đã viết phương pháp mở rộng sau cho việc này:

public static Window GetWpfMainWindow(this EnvDTE.DTE dte) 
{ 
    if (dte == null) 
    { 
    throw new ArgumentNullException("dte"); 
    } 

    var hwndMainWindow = (IntPtr)dte.MainWindow.HWnd; 
    if (hwndMainWindow == IntPtr.Zero) 
    { 
    throw new NullReferenceException("DTE.MainWindow.HWnd is null."); 
    } 

    var hwndSource = HwndSource.FromHwnd(hwndMainWindow); 
    if (hwndSource == null) 
    { 
    throw new NullReferenceException("HwndSource for DTE.MainWindow is null."); 
    } 

    return (Window) hwndSource.RootVisual; 
}