Scripts 学盟

标题: 一些可能一辈子都用不上的WindowsAPI及其使用方法 [打印本页]

作者: 俊俊    时间: 2011-7-13 18:32:33     标题: 一些可能一辈子都用不上的WindowsAPI及其使用方法

本帖最后由 cjqq0218 于 2011-7-13 18:44 编辑

更换墙纸
  1. const int SPI_SETDESKWALLPAPER = 20  ;
  2. const int SPIF_UPDATEINIFILE = 0x01;
  3. const int SPIF_SENDWININICHANGE = 0x02;

  4. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  5. private  static  extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;

  6. /// <summary>
  7. /// 更换墙纸
  8. /// </summary>
  9. /// <param name="Path">临时文件路径</param>
  10. internal static void ChangePaper(string Path)
  11. {
  12.         //System.Windows.Forms.MessageBox.Show("ApI qian");
  13.         SystemParametersInfo( SPI_SETDESKWALLPAPER,
  14.                                 0,
  15.                                 Path,
  16.                                 SPIF_SENDWININICHANGE|SPIF_UPDATEINIFILE );
  17.                        
  18. }
复制代码
移动无标题窗体
  1. private const int WM_SysCommand = 0x0112;
  2. private const int OneMsgNum = 0xf017;
  3. [DllImport("user32")]
  4. private static extern bool ReleaseCapture();
  5. [DllImport("user32")]
  6. private static extern bool PostMessage( IntPtr hWnd, int Mwg, int wParam, int lParam);

  7. /// <summary>
  8. /// 鼠标拖动窗体
  9. /// </summary>
  10. /// <param name="hwnd">窗体句柄</param>
  11. internal static void MovieWindow(IntPtr hwnd)
  12. {
  13.         ReleaseCapture();
  14.         PostMessage(hwnd, WM_SysCommand, OneMsgNum, 0);
  15. }
复制代码
透明窗体常量定义以及函数的声明
  1. const int GWL_EXSTYLE = (-20);
  2. const int WS_EX_TRANSPARENT = 0x20;
  3. const uint WS_EX_LAYERED = 0x80000;
  4. const int LWA_ALPHA = 0x2;

  5. [DllImport("user32",   EntryPoint="SetWindowLong")]   
  6. private   static   extern   uint   SetWindowLong   (   
  7.                 IntPtr   hwnd,   
  8.                 int   nIndex,   
  9.                 uint   dwNewLong   
  10.                 );   
  11. [DllImport("user32",   EntryPoint="GetWindowLong")]   
  12. private   static   extern   uint   GetWindowLong   (   
  13.         IntPtr   hwnd,   
  14.         int   nIndex   
  15.         );   

  16. [DllImport("user32",   EntryPoint="SetLayeredWindowAttributes")]   
  17. private   static   extern   int   SetLayeredWindowAttributes   (   
  18.         IntPtr   hwnd,   
  19.         int   crKey,   
  20.         int   bAlpha,   
  21.         int   dwFlags   
  22.         );

  23. /// <summary>
  24. /// 透明窗体
  25. /// </summary>
  26. /// <param name="hwnd">窗体句柄</param>
  27. /// <param name="Transparent">透明度(0~255)</param>
  28. internal static void TransparentWindow(IntPtr hwnd,int Transparent)   
  29. {   
  30.         uint intExTemp = GetWindowLong( hwnd , GWL_EXSTYLE );
  31.         uint oldGWLEx = SetWindowLong( hwnd , GWL_EXSTYLE , intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED );
  32.         SetLayeredWindowAttributes( hwnd , 0 , Transparent , LWA_ALPHA);
  33. }
复制代码
注册系统热键及释放
  1. [System.Runtime.InteropServices.DllImport("user32.dll",EntryPoint = "RegisterHotKey")]
  2. private static extern bool RegisterHotKey(IntPtr wnd, int id, int mode, System.Windows.Forms.Keys vk);
  3. [System.Runtime.InteropServices.DllImport("user32.dll" ,EntryPoint = "UnregisterHotKey")]
  4. private static extern bool UnregisterHotKey(IntPtr wnd, int id);


  5. /// <summary>
  6. /// 注册热键
  7. /// </summary>
  8. /// <param name="hwnd">要注册热键的窗体的句柄</param>
  9. /// <returns>bool,true:成功,false:失败</returns>
  10. internal static bool RegHotKey(IntPtr hwnd)
  11. {
  12.         return RegisterHotKey(hwnd,159,3,System.Windows.Forms.Keys.Oemtilde);
  13. }

  14. /// <summary>
  15. /// 释放热键
  16. /// </summary>
  17. /// <param name="hwnd">要取消热键的窗体的句柄</param>
  18. /// <returns>bool,true:成功,false:失败</returns>
  19. internal static bool unRegHotKey(IntPtr hwnd)
  20. {
  21.         return UnregisterHotKey(hwnd,159);
  22. }
复制代码
用默认程序打开(文件、程序、URL)
  1. [DllImport("shell32.dll")]
  2. private static extern int ShellExecute(IntPtr hwnd,string lpszOp,string lpszFile,string lpszParams,string lpszDir,int FsShowCmd);
  3.                
  4. /// <summary>
  5. /// 用默认程序打开地址
  6. /// </summary>
  7. /// <param name="Address"></param>
  8. internal static int OpenInterExploer(string UrlAddress)
  9. {
  10. return ShellExecute(IntPtr.Zero,"Open",UrlAddress,"","", 1);
  11. }
复制代码
发送消息到指定句柄的窗口
  1. [DllImport("user32")]
  2. private static extern int SendMessage(IntPtr   hWnd,   uint   Msg,   uint   wParam,   uint   lParam);
  3. /// <summary>
  4. /// 发送消息到指定句柄的窗口
  5. /// </summary>
  6. /// <param name="hWnd"></param>
  7. /// <param name="Msg"></param>
  8. /// <returns></returns>
  9. internal static int SendMessageToWindow(IntPtr   hWnd , uint   Msg)
  10. {
  11.         int i = SendMessage( hWnd,Msg,0,0);
  12.         return i ;
  13. }
  14. /// <summary>
  15. /// 发送消息到指定句柄的窗口(无需回应)
  16. /// </summary>
  17. /// <param name="hWnd"></param>
  18. /// <param name="Msg"></param>
  19. /// <returns></returns>
  20. internal static bool PostMessageToWindow(IntPtr   hWnd , int   Msg)
  21. {
  22.         bool i = PostMessage( hWnd,Msg,0,0);
  23.                 return i ;
  24. }
复制代码
隐藏和显示桌面图标
  1. [DllImport("user32.dll")]
  2. public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);
  3. [DllImport("user32.dll",SetLastError=true)]
  4. private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  5. [DllImport("user32.dll",SetLastError=true)]
  6. private static extern IntPtr GetDesktopWindow();
  7. const int SW_SHOW = 5;
  8. const int SW_HIDE = 0;
  9.                
  10. /// <summary>
  11. /// 隐藏和显示桌面图标
  12. /// </summary>
  13. /// <param name="YesOrNo">true表示显示,false表示隐藏</param>
  14. internal static void ShowDiskTop(bool YesOrNo)
  15. {
  16.         IntPtr Wnd = IntPtr.Zero;
  17.         Wnd = GetDesktopWindow();
  18.         Wnd = FindWindowEx(Wnd, IntPtr.Zero, "Progman",null);
  19.         if(YesOrNo)
  20.         {
  21.                 ShowWindow(Wnd,SW_SHOW);
  22.         }
  23.         else
  24.         {
  25.                 ShowWindow(Wnd,SW_HIDE);
  26.         }
  27.         Wnd = IntPtr.Zero;
  28. }
复制代码
刷新桌面
  1. [DllImport("user32.dll", EntryPoint = "RedrawWindow")]
  2. private static extern bool RedrawWindow(int hWnd, IntPtr prect, IntPtr hrgnUpdate, uint flags);
  3. /// <summary>
  4. /// 刷新桌面
  5. /// </summary>
  6. /// <returns></returns>
  7. internal static bool DiskTableReflsh()
  8. {
  9.         return RedrawWindow(0, IntPtr.Zero, IntPtr.Zero, 4 | 1 | 128);
  10. }
复制代码

作者: 混混@普宁.中国    时间: 2011-7-13 23:10:55

现在很少会用到 win api
作者: 混混@普宁.中国    时间: 2011-7-13 23:11:33

自己还是只能做一些很表层的开发
作者: 俊俊    时间: 2011-7-14 08:18:11

混混@普宁.中国 发表于 2011-7-13 23:10
现在很少会用到 win api

所以我说可能一辈子都用不上
作者: 那个谁    时间: 2011-7-14 08:33:20

不一定哦。。。。
作者: 嘟嘟    时间: 2011-7-17 09:33:59

我 表示路过 看看。。。
作者: 混混@普宁.中国    时间: 2011-7-18 18:08:19

较少用吧。。。觉得如果开发要用到 windows api

就不如直接 mfc 了,,不要用 .net
作者: 俊俊    时间: 2011-7-19 16:35:14

用api是因为.net中没有这些功能或用.net的方法实现起来比较麻烦,要不然微软干嘛要提供这个接口呢??




欢迎光临 Scripts 学盟 (http://www.iscripts.org/) Powered by Discuz! X2