2011-09-03 13 views
4

有沒有辦法在Windows 7和Windows Vista中單擊的通知圖標上方放置表格?在點擊的通知圖標上方的位置表格

+1

這對你有幫助嗎? http://codetechnic.blogspot.com/2009/03/set-windows-forms-start-position-to.html – Zenwalker

+0

沒有,它將窗體放置在任務欄上方的右下角。我需要將它放在通知圖標上方。 – blejzz

+0

可能的重複:http://stackoverflow.com/questions/272778/how-to-find-the-location-of-the-icon-in-the-system-tray –

回答

1

關於您的評論:「我怎麼知道任務欄是如何定位的?」

看看下面的文章包含暴露用於檢索托盤Rectangle Structure的方法的類:[c#] NotifyIcon - Detect MouseOut

使用這個類可以檢索Rectangle Structure的托盤,像這樣:

Rectangle trayRectangle = WinAPI.GetTrayRectangle(); 

這將爲您提供托盤的頂部,左側,右側和底部座標及其寬度和高度。

我已經包含下面的類:

using System; 
using System.Runtime.InteropServices; 
using System.Drawing; 
using System.ComponentModel; 

public class WinAPI 
{ 
    public struct RECT 
    { 
     public int left; 
     public int top; 
     public int right; 
     public int bottom; 

     public override string ToString() 
     { 
      return "(" + left + ", " + top + ") --> (" + right + ", " + bottom + ")"; 
     } 
    } 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    public static extern IntPtr FindWindow(string strClassName, string strWindowName); 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle); 

    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); 


    public static IntPtr GetTrayHandle() 
    { 
     IntPtr taskBarHandle = WinAPI.FindWindow("Shell_TrayWnd", null); 
     if (!taskBarHandle.Equals(IntPtr.Zero)) 
     { 
      return WinAPI.FindWindowEx(taskBarHandle, IntPtr.Zero, "TrayNotifyWnd", IntPtr.Zero); 
     } 
     return IntPtr.Zero; 
    } 

    public static Rectangle GetTrayRectangle() 
    { 
     WinAPI.RECT rect; 
     WinAPI.GetWindowRect(WinAPI.GetTrayHandle(), out rect); 
     return new Rectangle(new Point(rect.left, rect.top), new Size((rect.right - rect.left) + 1, (rect.bottom - rect.top) + 1)); 
    } 
} 

希望這有助於。

+1

這是一個怪誕的黑客。爲什麼你會這樣做,而不是從右鍵單擊事件讀出光標pos?爲什麼訴諸黑客,當你可以正確地做到這一點? –

+0

@David Heffernan什麼是正確的方式來獲取任務欄的位置?我只需要知道它是在左/右/上/下。 – blejzz

+1

@blejzz漢斯已經解釋說你找不到你的圖標。你可以做的是獲得調用你的圖標上下文菜單的事件的光標pos。從那你可以找出哪個角落。 –

5

這是一個更簡單的方法。

當OnClick事件被觸發時,您可以獲得鼠標的X,Y位置。 您還可以通過這些對象的某些檢查來獲取任務欄位置Screen.PrimaryScreen.Bounds,Screen.PrimaryScreen.WorkingArea

private void OnTrayClick(object sender, EventArgs e) 
    { 
     _frmMain.Left = Cursor.Position.X; 
     _frmMain.Top = Screen.PrimaryScreen.WorkingArea.Bottom -_frmMain.Height; 
     _frmMain.Show();   
    }