导航

yaosansi's Blog

当你背向太阳的时候,你只会看到自己的阴影!能力是有限的,努力无限的!

« .net与javascript时间的转换IIS 7.0比特率节流模块发布了 »

通过win32api让c#控制Windows任务栏

  • 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
  • 本站IM群,请自行选择。请各位朋友按照自己喜好加入。加入群后请及时发言,防止被清理。谢谢您的合作!!!
  • QQ群:Y①WEB开发(ASP.NET)号码:7351660 QQ群:Y②WEB开发(ASP+.NET)号码:11864905
  • QQ群:Y③WEB开发(DIV+CSS)号码:16610506 QQ群:Y④WEB开发(JS+AJAX)号码:16143998
  • QQ群:Y⑤WEB开发(新手)号码:12777715 MSN群:yaosansi[at]126.com

 

如果你要在你的C#程序中控制Windows的任务栏,有两个Windows api 可以帮到你!他们就是  FindWindowA 和 ShowWindow
C#中声明如下:
using System.Runtime.InteropServices;
[DllImport("user32.dll", EntryPoint = "FindWindowA")]
public static extern IntPtr FindWindowA(string lp1, string lp2);
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern IntPtr ShowWindow(IntPtr hWnd, int _value);
其实Windows的任务栏就是一个特殊的窗口,所以操作窗口的方法,对任务栏一样适合!控制代码如下:
//获取任务栏
IntPtr hTray = Form1.FindWindowA("Shell_TrayWnd", String.Empty);
//显示任务栏
Form1.ShowWindow(hTray, 5);
//隐藏任务栏
Form1.ShowWindow(hTray, 0);
 
 

C# Signature:

[DllImport("user32.dll")]
static extern bool DrawAnimatedRects(IntPtr hwnd, int idAni,
   [In] ref RECT lprcFrom, [In] ref RECT lprcTo);
 

User-Defined Types:

IDANI_

Notes:

Only the IDANI_CAPTION constant will result in any animation. Any other constants have not been implemented on any Windows platform!

Tips & Tricks:

Since only IDANI_CAPTION is implemented, to get the effect of IDANI_OPEN, simply swap the lprcFrom and lprcTo rectangles, but still specify the IDANI_CAPTION constant.

Sample Code:

    /// <summary>
    /// This constant is not implemented on any Windows platform!
    /// </summary>
    public const System.Int32 IDANI_OPEN = 1;

    /// <summary>
    /// The window caption will animate from lprcFrom to lprcTo.
    /// </summary>
    public const System.Int32 IDANI_CAPTION = 3;

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    struct RECT
    {
      public RECT(System.Drawing.Rectangle rectangle)
      {
        Left = rectangle.Left;
        Top = rectangle.Top;
        Right = rectangle.Right;
        Bottom = rectangle.Bottom;
      }
      public RECT(System.Drawing.Point location, System.Drawing.Size size)
      {
        Left = location.X;
        Top = location.Y;
        Right = location.X + size.Width;
        Bottom = location.Y + size.Height;
      }
      public System.Int32 Left;
      public System.Int32 Top;
      public System.Int32 Right;
      public System.Int32 Bottom;
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool DrawAnimatedRects(System.IntPtr hwnd, int idAni,
      [System.Runtime.InteropServices.In] ref RECT lprcFrom,
      [System.Runtime.InteropServices.In] ref RECT lprcTo);

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern System.IntPtr FindWindowEx(System.IntPtr hwndParent, System.IntPtr hwndChildAfter,
      string lpszClass, string lpszWindow);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool GetWindowRect(System.IntPtr hWnd, out RECT lpRect);

    /// <summary>
    /// Show or hide a form with animation.
    /// </summary>
    /// <param name="form">The form reference to be animated.</param>
    /// <param name="show">If true the form will be animated as if opening, otherwise as if closing.</param>
    static public void ShowHideAnimated(System.Windows.Forms.Form form, System.Boolean show)
    {
      RECT from = new RECT(form.Location, form.Size);
      RECT to;

      System.IntPtr hWnd =
        FindWindowEx(FindWindow("Shell_TrayWnd", null), System.IntPtr.Zero, "TrayNotifyWnd", null);

      if (hWnd != System.IntPtr.Zero)
      {
        GetWindowRect(hWnd, out to);
      }
      else
      {
        to.Left = System.Windows.Forms.SystemInformation.VirtualScreen.Right - form.Width;
        to.Top = System.Windows.Forms.SystemInformation.VirtualScreen.Bottom -
          System.Windows.Forms.SystemInformation.CaptionHeight -
            (System.Windows.Forms.SystemInformation.FrameBorderSize.Height * 2);
        to.Right = System.Windows.Forms.SystemInformation.VirtualScreen.Right;
        to.Bottom = System.Windows.Forms.SystemInformation.VirtualScreen.Bottom;
      }

      if (show)
      {
        DrawAnimatedRects(form.Handle, IDANI_CAPTION, ref to, ref from);
        form.Show();
      }
      else
      {
        form.Hide();
        DrawAnimatedRects(form.Handle, IDANI_CAPTION, ref from, ref to);
      }
    }

 

Alternative Managed API:

    [StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)]
    ref struct RECT {
    public:
    RECT() {
    }

    RECT(System::Drawing::Rectangle rectangle) {
        Left = rectangle.Left;
        Top = rectangle.Top;
        Right = rectangle.Right;
        Bottom = rectangle.Bottom;
    }

    RECT(System::Drawing::Point location, System::Drawing::Size size) {
        Left = location.X;
        Top = location.Y;
        Right = location.X + size.Width;
        Bottom = location.Y + size.Height;
    }

    System::Int32 Left;
    System::Int32 Top;
    System::Int32 Right;
    System::Int32 Bottom;
    };

    [DllImport("user32.dll")]
    bool DrawAnimatedRects(int hwnd, int idAni, RECT^ lprcFrom, RECT^ lprcTo);

    [DllImport("user32.dll", SetLastError = true)]
    int FindWindow(String^ lpClassName, String^ lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    int FindWindowEx(int hwndParent, int hwndChildAfter,
      String^ lpszClass, String^ lpszWindow);

    [DllImport("user32.dll")]
    bool GetWindowRect(int hWnd, RECT^ lpRect);

    static const int IDANI_OPEN = 1;
    static const int IDANI_CAPTION = 3;

    void ShowHideAnimated(bool show) {
    RECT^ from=gcnew RECT(this->Location, this->Size);
    RECT^ to=gcnew RECT();

    int hWnd=FindWindowEx(FindWindow("Shell_TrayWnd", nullptr), 0, "TrayNotifyWnd", nullptr);

    if (hWnd != 0) {
        GetWindowRect(hWnd, to);
    }
    else {
          to->Left = System::Windows::Forms::SystemInformation::VirtualScreen.Rig ht - 32;
          to->Top = System::Windows::Forms::SystemInformation::VirtualScreen.Bot tom -32;
          to->Right = System::Windows::Forms::SystemInformation::VirtualScreen.Rig ht;
          to->Bottom = System::Windows::Forms::SystemInformation::VirtualScreen.Bot tom;
    }

    if (show) {
        DrawAnimatedRects(this->Handle.ToInt32(), IDANI_CAPTION, to, from);
        this->Show();
    }
    else {
        this->Hide();
        DrawAnimatedRects(this->Handle.ToInt32(), IDANI_CAPTION, from, to);
    }
    }
 

-----------------------------------------------------------------------------------------

按照如下步骤可以实现:  
  1.定义API和相应的常量.  
    using   System.Runtime.InteropServices;  
   
  private   const   int   SWP_HIDEWINDOW   =   0x80;    
  private   const   int   SWP_SHOWWINDOW   =   0x40;    
   
  [DllImport("user32.dll")]  
  public   static   extern   bool   SetWindowPos(    
  int   hWnd,                           //   handle   to   window    
  int   hWndInsertAfter,     //   placement-order   handle    
  short   X,                                   //   horizontal   position    
  short   Y,                                   //   vertical   position    
  short   cx,                                 //   width    
  short   cy,                                 //   height    
  uint   uFlags                         //   window-positioning   options    
  );    
  [DllImport("user32.dll")]  
  public   static   extern   int   FindWindow(    
  string   lpClassName,     //   class   name    
  string   lpWindowName     //   window   name    
  );    
   
  2.在button的Click中,填写如下代码:  
  int   TaskBarHwnd;    
  TaskBarHwnd   =   FindWindow("Shell_traywnd",   "");    
  if   (button1.Text     ==   "隐藏")    
  {    
  SetWindowPos(TaskBarHwnd,   0,   0,   0,   0,   0,   SWP_HIDEWINDOW);    
  button1.Text   =   "显示";   }    
  else    
  {    
  SetWindowPos(TaskBarHwnd,   0,   0,   0,   0,   0,   SWP_SHOWWINDOW);    
  button1.Text   =   "隐藏";    
  }     
 

-----------------------------------------------------------------------------------------

[DllImport("User32.dll")]  
  public   static   extern   IntPtr   FindWindowEx(IntPtr   ph,   IntPtr   ch,   String   cn,   String   wn);  
  [DllImport("User32.dll")]  
  public   static   extern   bool   ShowWindow(IntPtr   hWnd,   long   nCmdShow   );  
   
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
      IntPtr   handle   =   FindWindowEx(IntPtr.Zero,IntPtr.Zero,"Shell_TrayWnd",null);  
      ShowWindow(handle,0);  
  }

 

 

函数参考:

-----------------------------------------------------------------------------------------

 

    函数功能:该函数设置指定窗口的显示状态。

    函数原型:BOOL ShowWindow(HWND hWnd,int nCmdShow);

    参数:

    hWnd:窗口句柄。

    nCmdShow:指定窗口如何显示。如果发送应用程序的程序提供了STARTUPINFO结构,则应用程序第一次调用ShowWindow时该参数被忽略。否则,在第一次调用ShowWindow函数时,该值应为在函数WinMain中nCmdShow参数。在随后的调用中,该参数可以为下列值之一:

    SW_FORCEMINIMIZE:在WindowNT5.0中最小化窗口,即使拥有窗口的线程被挂起也会最小化。在从其他线程最小化窗口时才使用这个参数。

    SW_MIOE:隐藏窗口并激活其他窗口。

    SW_MAXIMIZE:最大化指定的窗口。

    SW_MINIMIZE:最小化指定的窗口并且激活在Z序中的下一个顶层窗口。

    SW_RESTORE:激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志。

    SW_SHOW:在窗口原来的位置以原来的尺寸激活和显示窗口。

    SW_SHOWDEFAULT:依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的。

    SW_SHOWMAXIMIZED:激活窗口并将其最大化。

    SW_SHOWMINIMIZED:激活窗口并将其最小化。

    SW_SHOWMINNOACTIVATE:窗口最小化,激活窗口仍然维持激活状态。

    SW_SHOWNA:以窗口原来的状态显示窗口。激活窗口仍然维持激活状态。

    SW_SHOWNOACTIVATE:以窗口最近一次的大小和状态显示窗口。激活窗口仍然维持激活状态。

    SW_SHOWNOMAL:激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。

    返回值:如果窗口以前可见,则返回值为非零。如果窗口以前被隐藏,则返回值为零。

    备注:应用程序第一次调用ShowWindow时,应该使用WinMain函数的nCmdshow参数作为它的nCmdShow参数。在随后调用ShowWindow函数时,必须使用列表中的一个给定值,而不是由WinMain函数的nCmdSHow参数指定的值。

    正如在nCmdShow参数中声明的,如果调用应用程序的程序使用了在STARTUPINFO结构中指定的信息来显示窗口,则在第一次调用ShowWindow函数时nCmdShow参数就被忽略。在这种情况下,ShowWindow函数使用STARTUPINFO结构中的信息来显示窗口。在随后的调用中,应用程序必须调用ShowWindow 函数(将其中nCmdShow参数设为SW_SHOWDEFAULT)来使用由程序调用该应用程序时提供的启动信息。这个处理在下列情况下发生:

    应用程序通过调用带WS_VISIBLE标志的函数来创建它们的主窗口函数;

    应用程序通过调用清除了WS_VISIBLE标志的CteateWindow函数来创建主窗口函数,并且随后调用带SW_SHOW标志的ShowWindow函数来显示窗口;

    Windows CE:nCmdShow参数不支持下列值:

    SW_MAXIMINZE;SW_MINIMIZE;SW_RESTORE;SW_SHOWDEFAULT

    SW_SHOWMAXIMIZED;SW_SHOWMINIMIZED;SW_SHOWMININOACTIVATE

    速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本;头文件:winuw库文件:user32.lib。

-----------------------------------------------------------------------------------------

 函数功能:该函数获得一个顶层窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数不查找子窗口。在查找时不区分大小写。

    函数型:HWND FindWindow(LPCTSTR IpClassName,LPCTSTR IpWindowName);

    参数:

    IpClassName :指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobafAddAtom函数产生的全局成员。该成员为16位,必须位于IpClassName的低 16位,高位必须为 0。

    IpWindowName:指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为空,则为所有窗口全匹配。

    返回值:如果函数成功,返回值为具有指定类名和窗口名的窗口句柄;如果函数失败,返回值为NULL。

    若想获得更多错误信息,请调用GetLastError函数。

    备注:Windows CE:若类名是一个成员,它必须是从 RegisterClass返回的成员。

    速查:Windows NT:3.1以上版本;Windows:95以上版本;Windows CE:1.0以上版本;头文件:Winuser.h;库文件:user32.lib; Unicode:在 Windows NT上实现为 Unicode和 ANSI两种版本。
-----------------------------------------------------------------------------------------

函数功能:该函数获得一个窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数查找子窗口,从排在给定的子窗口后面的下一个子窗口开始。在查找时不区分大小写。

    函数原型:HWND FindWindowEx(HWND hwndParent,HWND hwndChildAfter,LPCTSTR lpszClass,LPCTSTR lpszWindow);

    参数;

    hwndParent:要查找子窗口的父窗口句柄。

    如果hwnjParent为NULL,则函数以桌面窗口为父窗口,查找桌面窗口的所有子窗口。

    Windows NT5.0 and later:如果hwndParent是HWND_MESSAGE,函数仅查找所有消息窗口。

    hwndChildAfter :子窗口句柄。查找从在Z序中的下一个子窗口开始。子窗口必须为hwndPareRt窗口的直接子窗口而非后代窗口。如果HwndChildAfter为NULL,查找从hwndParent的第一个子窗口开始。如果hwndParent 和 hwndChildAfter同时为NULL,则函数查找所有的顶层窗口及消息窗口。

    lpszClass:指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobaIAddAtom函数产生的全局成员。该成员为16位,必须位于lpClassName的低16位,高位必须为0。

    lpszWindow:指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为 NULL,则为所有窗口全匹配。返回值:如果函数成功,返回值为具有指定类名和窗口名的窗口句柄。如果函数失败,返回值为NULL。

    若想获得更多错误信息,请调用GetLastError函数。

    速查 NT:4.0对以上版本;Windows:95以上版本;Windows CE:不支持;头文件:winuser.h;库文件:user32.lib;Unicode:在Windows NT上实现为Unicode和ANSI两种版本。

 

 

原创文章如转载,请注明:转载自http://www.yaosansi.com
订阅本站,阅读更多文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog .Theme from Google黑板报 By Washun

Copyright 2005-2008 yaosansi'site All Rights Reserved.

感谢Denny·G 为本站提供FTP空间
辽ICP备05021434号

Search

  •  

赞助商广告

控制面板

最新评论及回复

最近发表