设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1784|回复: 7
打印 上一主题 下一主题

一些可能一辈子都用不上的WindowsAPI及其使用方法 [复制链接]

Rank: 7Rank: 7Rank: 7

跳转到指定楼层
1#
俊俊 实名认证  发表于 2011-7-13 18:32:33 |只看该作者 |倒序浏览
本帖最后由 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. }
复制代码
1

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

2#
混混@普宁.中国 实名认证  发表于 2011-7-13 23:10:55 |只看该作者
现在很少会用到 win api

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

3#
混混@普宁.中国 实名认证  发表于 2011-7-13 23:11:33 |只看该作者
自己还是只能做一些很表层的开发

使用道具 举报

Rank: 7Rank: 7Rank: 7

4#
俊俊 实名认证  发表于 2011-7-14 08:18:11 |只看该作者
混混@普宁.中国 发表于 2011-7-13 23:10
现在很少会用到 win api

所以我说可能一辈子都用不上

使用道具 举报

Rank: 8Rank: 8

5#
那个谁 发表于 2011-7-14 08:33:20 |只看该作者
不一定哦。。。。

使用道具 举报

Rank: 2

6#
嘟嘟 发表于 2011-7-17 09:33:59 |只看该作者
我 表示路过 看看。。。
哈哈哈大笑三声!

使用道具 举报

管理员

超级大菜鸟

Rank: 9Rank: 9Rank: 9

7#
混混@普宁.中国 实名认证  发表于 2011-7-18 18:08:19 |只看该作者
较少用吧。。。觉得如果开发要用到 windows api

就不如直接 mfc 了,,不要用 .net

使用道具 举报

Rank: 7Rank: 7Rank: 7

8#
俊俊 实名认证  发表于 2011-7-19 16:35:14 |只看该作者
用api是因为.net中没有这些功能或用.net的方法实现起来比较麻烦,要不然微软干嘛要提供这个接口呢??

使用道具 举报

您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-6 19:47 , Processed in 1.072412 second(s), 12 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部