C#调用Win32 的API函数User32.dll (转)

 

win32的api函数是微软自己的东西,可以直接在c#中直接调用,在做winform时还是很有帮助的。有时候我们之直接调用win32 的api,可以很高效的实现想要的效果。

using  system;
using  system.collections.generic;
using  system.linq;
using  system.text;
using  system.runtime.interopservices;

namespace  windowsapi
{
    
class  csharp_win32api
    {
        
#region  user32.dll 函数
        
///   <summary>
        
///  该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在gdi函数中使用该句柄来在设备上下文环境中绘图。hwnd:设备上下文环境被检索的窗口的句柄
        
///   </summary>
        [dllimport( " user32.dll " , charset  =  charset.auto)]
        
public   static   extern  intptr getdc(intptr hwnd);
        
///   <summary>
        
///  函数释放设备上下文环境(dc)供其他应用程序使用。
        
///   </summary>
         public   static   extern   int  releasedc(intptr hwnd, intptr hdc);
        
///   <summary>
        
///  该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。
        
///   </summary>
         static   public   extern  intptr getdesktopwindow();
        
///   <summary>
        
///  该函数设置指定窗口的显示状态。
        
///   </summary>
         static   public   extern   bool  showwindow(intptr hwnd,  short  state);
        
///   <summary>
        
///  通过发送重绘消息 wm_paint 给目标窗体来更新目标窗体客户区的无效区域。
        
///   </summary>
         static   public   extern   bool  updatewindow(intptr hwnd);
        
///   <summary>
        
///  该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
        
///   </summary>
         static   public   extern   bool  setforegroundwindow(intptr hwnd);
        
///   <summary>
        
///  该函数改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和z序。
        
///   </summary>
         static   public   extern   bool  setwindowpos(intptr hwnd, intptr hwndinsertafter,  int  x,  int  y,  int  width,  int  height,  uint  flags);
        
///   <summary>
        
///  打开剪切板
        
///   </summary>
         static   public   extern   bool  openclipboard(intptr hwndnewowner);
        
///   <summary>
        
///  关闭剪切板
        
///   </summary>
         static   public   extern   bool  closeclipboard();
        
///   <summary>
        
///  打开清空 </summary>
         static   public   extern   bool  emptyclipboard();
        
///   <summary>
        
///  将存放有数据的内存块放入剪切板的资源管理中
        
///   </summary>
         static   public   extern  intptr setclipboarddata( uint  format, intptr hdata);
        
///   <summary>
        
///  在一个矩形中装载指定菜单条目的屏幕坐标信息 
        
///   </summary>
         static   public   extern   bool  getmenuitemrect(intptr hwnd, intptr hmenu,  uint  item,  ref  rect rc);

        [dllimport(
" user32.dll " , exactspelling  =   true , charset  =  charset.auto)]
        
///   <summary>
        
///  该函数获得一个指定子窗口的父窗口句柄。
        
///   </summary>
         public   static   extern  intptr getparent(intptr hwnd);
        
///   <summary>
        
///  该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。 
        
///   </summary>
        
///   <param name="hwnd"> 其窗口程序将接收消息的窗口的句柄 </param>
        
///   <param name="msg"> 指定被发送的消息 </param>
        
///   <param name="wparam"> 指定附加的消息指定信息 </param>
        
///   <param name="lparam"> 指定附加的消息指定信息 </param>
        
///   <returns></returns>
         public   static   extern   int  sendmessage(intptr hwnd,  int  msg,  int  wparam,  int  lparam);
        
public   static   extern  intptr sendmessage(intptr hwnd,  int  msg,  int  wparam, intptr lparam);        
        
public   static   extern   void  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  rect lparam);
        
public   static   extern   int  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  point lparam);       
        
public   static   extern   void  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  tbbutton lparam);        
        
public   static   extern   void  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  tbbuttoninfo lparam);      
        
public   static   extern   int  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  rebarbandinfo lparam);      
        
public   static   extern   void  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  tvitem lparam);       
        
public   static   extern   void  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  lvitem lparam);    
        
public   static   extern   void  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  hditem lparam);   
        
public   static   extern   void  sendmessage(intptr hwnd,  int  msg,  int  wparam,  ref  hd_hittestinfo hti);  
        
///   <summary>
        
///  该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里
        
///   </summary>
         public   static   extern  intptr postmessage(intptr hwnd,  int  msg,  int  wparam,  int  lparam);
        
public   static   extern  intptr setwindowshookex( int  hookid, hookproc pfnhook, intptr hinst,  int  threadid);

        [dllimport(
" user32.dll " , charset  =  charset.auto, exactspelling  =   true )]
        
public   static   extern   bool  unhookwindowshookex(intptr hhook);

        [dllimport(
" user32.dll " , charset  =  charset.auto, exactspelling  =   true )]
        
public   static   extern  intptr callnexthookex(intptr hhook,  int  code, intptr wparam, intptr lparam);
        
///   <summary>
        
///  该函数对指定的窗口设置键盘焦点。
        
///   </summary>
         public   static   extern  intptr setfocus(intptr hwnd);
        
///   <summary>
        
///  该函数在指定的矩形里写入格式化文本,根据指定的方法对文本格式化(扩展的制表符,字符对齐、折行等)。
        
///   </summary>
         public   extern   static   int  drawtext(intptr hdc,  string  lpstring,  int  ncount,  ref  rect lprect,  int  uformat);
        
///   <summary>
        
///  该函数改变指定子窗口的父窗口。
        
///   </summary>
         public   extern   static  intptr setparent(intptr hchild, intptr hparent);
        
///   <summary>
        
///  获取对话框中子窗口控件的句柄
        
///   </summary>
         public   extern   static  intptr getdlgitem(intptr hdlg,  int  ncontrolid);
        
///   <summary>
        
///  该函数获取窗口客户区的坐标。
        
///   </summary>
         public   extern   static   int  getclientrect(intptr hwnd,  ref  rect rc);
        
///   <summary>
        
///  该函数向指定的窗体添加一个矩形,然后窗口客户区域的这一部分将被重新绘制。
        
///   </summary>
         public   extern   static   int  invalidaterect(intptr hwnd, intptr rect,  int  berase);
        
///   <summary>
        
///  该函数产生对其他线程的控制,假如一个线程没有其他消息在其消息队列里。
        
///   </summary>
         public   static   extern   bool  waitmessage();
        
///   <summary>
        
///  该函数为一个消息检查线程消息队列,并将该消息(假如存在)放于指定的结构。
        
///   </summary>
         public   static   extern   bool  peekmessage( ref  msg msg,  int  hwnd,  uint  wfiltermin,  uint  wfiltermax,  uint  wflag);
        
///   <summary>
        
///  该函数从调用线程的消息队列里取得一个消息并将其放于指定的结构。此函数可取得与指定窗口联系的消息和由postthreadmesssge寄送的线程消息。此函数接收一定范围的消息值。
        
///   </summary>
         public   static   extern   bool  getmessage( ref  msg msg,  int  hwnd,  uint  wfiltermin,  uint  wfiltermax);
        
///   <summary>
        
///  该函数将虚拟键消息转换为字符消息。
        
///   </summary>
         public   static   extern   bool  translatemessage( ref  msg msg);
        
///   <summary>
        
///  该函数调度一个消息给窗口程序。
        
///   </summary>
         public   static   extern   bool  dispatchmessage( ref  msg msg);
        
///   <summary>
        
///  该函数从一个与应用事例相关的可执行文件(exe文件)中载入指定的光标资源.
        
///   </summary>
         public   static   extern  intptr loadcursor(intptr hinstance,  uint  cursor);
        
///   <summary>
        
///  该函数确定光标的外形。
        
///   </summary>
         public   static   extern  intptr setcursor(intptr hcursor);
        
///   <summary>
        
///  确定当前焦点位于哪个控件上。
        
///   </summary>
         public   static   extern  intptr getfocus();
        
///   <summary>
        
///  该函数从当前线程中的窗口释放鼠标捕捉,并恢复通常的鼠标输入处理。捕捉鼠标的窗口接收所有的鼠标输入(无论光标的位置在哪里),除非点击鼠标键时,光标热点在另一个线程的窗口中。
        
///   </summary>
         public   static   extern   bool  releasecapture();
        
///   <summary>
        
///  预备指定的窗口来重绘并将绘画相关的信息放到一个paintstruct结构中。
        
///   </summary>
         public   static   extern  intptr beginpaint(intptr hwnd,  ref  paintstruct ps);
        
///   <summary>
        
///  标记指定窗口的绘画过程结束,每次调用beginpaint函数之后被请求
        
///   </summary>
         public   static   extern   bool  endpaint(intptr hwnd,  ref  paintstruct ps);
        
///   <summary>
        
///  半透明窗体
        
///   </summary>
         public   static   extern   bool  updatelayeredwindow(intptr hwnd, intptr hdcdst,  ref  point pptdst,  ref  size psize, intptr hdcsrc,  ref  point pprsrc, int32 crkey,  ref  blendfunction pblend, int32 dwflags);
        
///   <summary>
        
///  该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
        
///   </summary>
         public   static   extern   bool  getwindowrect(intptr hwnd,  ref  rect rect);
        
///   <summary>
        
///  该函数将指定点的用户坐标转换成屏幕坐标。
        
///   </summary>
         public   static   extern   bool  clienttoscreen(intptr hwnd,  ref  point pt);
        
///   <summary>
        
///  当在指定时间内鼠标指针离开或盘旋在一个窗口上时,此函数寄送消息。
        
///   </summary>
         public   static   extern   bool  trackmouseevent( ref  trackmouseevents tme);
        
///   <summary>
        
///  
        
///   </summary>
         public   static   extern   bool  setwindowrgn(intptr hwnd, intptr hrgn,  bool  redraw);
        
///   <summary>
        
///  该函数检取指定虚拟键的状态。
        
///   </summary>
         public   static   extern   ushort  getkeystate( int  virtkey);
        
///   <summary>
        
///  该函数改变指定窗口的位置和尺寸。对于顶层窗口,位置和尺寸是相对于屏幕的左上角的:对于子窗口,位置和尺寸是相对于父窗口客户区的左上角坐标的。
        
///   </summary>
         public   static   extern   bool  movewindow(intptr hwnd,  int  x,  int  y,  int  width,  int  height,  bool  repaint);
        
///   <summary>
        
///  该函数获得指定窗口所属的类的类名。
        
///   </summary>
         public   static   extern   int  getclassname(intptr hwnd,  out  stringbuffer classname,  int  nmaxcount);
        
///   <summary>
        
///  该函数改变指定窗口的属性
        
///   </summary>
         public   static   extern   int  setwindowlong(intptr hwnd,  int  nindex,  int  dwnewlong);
        
///   <summary>
        
///  该函数检索指定窗口客户区域或整个屏幕的显示设备上下文环境的句柄,在随后的gdi函数中可以使用该句柄在设备上下文环境中绘图。
        
///   </summary>
         public   static   extern  intptr getdcex(intptr hwnd, intptr hregion,  uint  flags);
        
///   <summary>
        
///  获取整个窗口(包括边框、滚动条、标题栏、菜单等)的设备场景 返回值 long。
        
///   </summary>
         public   static   extern  intptr getwindowdc(intptr hwnd);
        
///   <summary>
        
///  该函数用指定的画刷填充矩形,此函数包括矩形的左上边界,但不包括矩形的右下边界。
        
///   </summary>
         public   static   extern   int  fillrect(intptr hdc,  ref  rect rect, intptr hbrush);
        
///   <summary>
        
///  该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置。
        
///   </summary>
         public   static   extern   int  getwindowplacement(intptr hwnd,  ref  windowplacement wp);
        
///   <summary>
        
///  该函数改变指定窗口的标题栏的文本内容
        
///   </summary>
         public   static   extern   int  setwindowtext(intptr hwnd,  string  text);
        
///   <summary>
        
///  该函数将指定窗口的标题条文本(假如存在)拷贝到一个缓存区内。假如指定的窗口是一个控制,则拷贝控制的文本。
        
///   </summary>
         public   static   extern   int  getwindowtext(intptr hwnd,  out  stringbuffer text,  int  maxcount);
        
///   <summary>
        
///  用于得到被定义的系统数据或者系统配置信息.
        
///   </summary>
         static   public   extern   int  getsystemmetrics( int  nindex);
        
///   <summary>
        
///  该函数设置滚动条参数,包括滚动位置的最大值和最小值,页面大小,滚动按钮的位置。
        
///   </summary>
         static   public   extern   int  setscrollinfo(intptr hwnd,  int  bar,  ref  scrollinfo si,  int  fredraw);
        
///   <summary>
        
///  该函数显示或隐藏所指定的滚动条。
        
///   </summary>
         public   static   extern   int  showscrollbar(intptr hwnd,  int  bar,  int  show);
        
///   <summary>
        
///  该函数可以激活一个或两个滚动条箭头或是使其失效。
        
///   </summary>
         public   static   extern   int  enablescrollbar(intptr hwnd,  uint  flags,  uint  arrows);
        
///   <summary>
        
///  该函数将指定的窗口设置到z序的顶部。
        
///   </summary>
         public   static   extern   int  bringwindowtotop(intptr hwnd);
        
///   <summary>
        
///  该函数滚动指定窗体客户区域的目录。
        
///   </summary>
         static   public   extern   int  scrollwindowex(intptr hwnd,  int  dx,  int  dy, ref  rect rcscroll,  ref  rect rcclip, intptr updateregion,  ref  rect rcinvalidated,  uint  flags);
        
///   <summary>
        
///  该函数确定给定的窗口句柄是否识别一个已存在的窗口。
        
///   </summary>
         public   static   extern   int  iswindow(intptr hwnd);
        
///   <summary>
        
///  该函数将256个虚拟键的状态拷贝到指定的缓冲区中。
        
///   </summary>
         public   static   extern   int  getkeyboardstate( byte [] pbkeystate);
        
///   <summary>
        
///  该函数将指定的虚拟键码和键盘状态翻译为相应的字符或字符串。该函数使用由给定的键盘布局句柄标识的物理键盘布局和输入语言来翻译代码。
        
///   </summary>
         public   static   extern   int  toascii( int  uvirtkey, int  uscancode,  byte [] lpbkeystate,  byte [] lpwtranskey, int  fustate);
        
#endregion

    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值