2011-09-03 34 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();   
    }