C 中调用WIN32API函数

本文介绍了如何在 C# 中调用 Windows API 函数 `GetWindowRect` 以获取窗口边界尺寸,并提供了导入命名空间、函数声明及示例代码。同时,还列举了其他相关API如`SetCursorPos`、`ClientToScreen`等的使用方法,以及Windows API函数的基本调用技巧。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

http://www.pinvoke.net/

 

磐实文章站(首页)首页 >Visual Basic软件开发资料 > API 函数

http://www.panshsoft.com/Sort_VB/API_fun/

 

GetWindowRect用法

http://blog.csdn.net/coolszy/article/details/5601455

函数功能:该函数返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。

函数原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect);

参数:

hWnd:窗口句柄。

lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标。

返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。

 

C#中使用该函数首先导入命名空间:

using System.Runtime.InteropServices;  

然后写API引用部分的代码,放入 class 内部

[DllImport( "user32.dll")] 
private static extern int GetWindowRect(IntPtr hwnd, out  Rect lpRect); 
[c-sharp] view plain copy print ?
  1. [DllImport("user32.dll")]  
  2. private static extern int GetWindowRect(IntPtr hwnd,out  Rect lpRect);  
[DllImport("user32.dll")]private static extern int GetWindowRect(IntPtr hwnd,out  Rect lpRect);

这个函数有两个个参数,第一个参数是指定窗口句柄;第二个参数接收窗口的左上角和右下角的屏幕坐标,它是Rect结构。Rect结构定义如下:

public struct Rect 

{     

 public int Left;    

 public int Top;    

 public int Right;    

 public int Bottom; 

}  

演示代码:

  1. IntPtr hwnd = FindWindow("","计算器"); 
  2. Rect rect = new Rect(); 
  3. GetWindowRect(hwnd, out lpRect); 

 

SetCursorPos用法

http://blog.csdn.net/dangdaa/article/details/7001814

http://blog.csdn.net/coolszy/article/details/5608745

函数功能:该函数把光标移到屏幕的指定位置。如果新位置不在由 ClipCursor函数设置的屏幕矩形区域之内,则系统自动调整坐标,使得光标在矩形之内。

函数原型:BOOL SetCursorPOS(int X,int Y);
参数:
X:指定光标的新的X坐标,以屏幕坐标表示。
Y:指定光标的新的Y坐标,以屏幕坐标表示。
返回值:如果成功,返回非零值;如果失败,返回值是零,若想获得更多错误信息,请调用GetLastError函数。
备注:该光标是共享资源,仅当该光标在一个窗口的客户区域内时它才能移动该光标。

C#中使用该函数首先导入命名空间:

  1. using System.Runtime.InteropServices;
    [c-sharp] view plain copy print ?
    1. using System.Runtime.InteropServices;  
    using System.Runtime.InteropServices;

然后写API引用部分的代码,放入 class 内部

  1. [DllImport("user32.dll", EntryPoint ="SetCursorPos")]
  2. private staticexternint SetCursorPos(int x,int y);

这个函数有两个个参数,第一个参数是指定光标的新的X坐标;第二个参数是指定光标的新的Y坐标。例如:

  1. SetCursorPos(100, 100);

 

 

【声明】

vb

Public Declare Function OffsetRect Lib "user32" Alias "OffsetRect" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long

vb.net

Public Declare Function OffsetRect Lib "user32" Alias "OffsetRect" (lpRect As RECT, ByVal x As Integer, ByVal y As Integer) As Integer

c#

[DllImport("user32", EntryPoint="OffsetRect")]
public static extern int OffsetRect (
        ref RECT lpRect,
        int x,
        int y
);


【说明】
该函数通过应用一个指定的偏移,从而让矩形移动起来。x会添加到右侧和左侧区域。y添加到顶部和底部区域。偏移方向则取决于参数是正数还是负数,以及采用的是什么坐标系统

【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError

【参数表】
  lpRect ---------  RECT,欲移动的矩形

  x --------------  Long,水平偏移量

  y --------------  Long,垂直偏移量

 

C#调用WindowsAPI之GetClientRect用法

http://www.cnblogs.com/zhuiyi/archive/2012/07/17/2595309.html

    函数功能:该函数获取窗口客户区的坐标。客户区坐标指定客户区的左上角和右下角。由于客户区坐标是相对子窗口客户区的左上角而言的,因此左上角坐标为(0,0)

    函数原型:BOOL GetClientRect(HWND hWnd,LPRECT lpRect);

    参数:

    GetLastError 函数。

    备注:Windows CE:命令条包含在客户区中。

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

第一步,声明结构


public struct RECT
{
    public uint Left;
    public uint Top;
    public uint Right;
    public uint Bottom;
}

第二步,导入user32.dll

[DllImport( " user32 ")]
public static extern bool GetClientRect(
    IntPtr hwnd,
    out RECT lpRect
    );

第三步,设定一个RECT

RECT rect = new RECT();

第四步,应用

bool result = GetClientRect( this.Handle, out RECT rect);

注:GetClientRect 函数所取得的LeftTop 值是0RightBottom 值是width 和height。也就是说,Right的就是宽度,Bottom的值就是高度了

 

ClientToScreen

http://www.panshsoft.com/Sort_VB/API_fun/Control_msg/2012-04-26/923.php

vb

Public Declare Function ClientToScreen Lib "user32" Alias "ClientToScreen" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

vb.net

Public Declare Function ClientToScreen Lib "user32" Alias "ClientToScreen" (ByVal hwnd As Integer, lpPoint As POINTAPI) As Integer

c#

[DllImport("user32", EntryPoint="ClientToScreen")]
public static extern int ClientToScreen (
        int hwnd,
        ref POINTAPI lpPoint
);

ClientToScreen是把坐标从当前窗体转化成全屏幕的!!!
ScreenToClient是把屏幕坐标转化成相对当前窗体的坐标!!!!


【说明】
判断窗口内以客户区坐标表示的一个点的屏幕坐标

【返回值】
Long,非零表示成功,零表示失败

【参数表】
  hwnd -----------  Long,判断客户区坐标时那个窗口的句柄

  lpPoint --------  POINTAPI,用hwnd窗口的客户区坐标表示的点,这个参数会包含屏幕坐标系统中相同的点


 

[DllImport("user32", EntryPoint="ScreenToClient")]
public static extern int ScreenToClient (
        int hwnd,
        ref POINTAPI lpPoint
);


【说明】
判断屏幕上一个指定点的客户区坐标

【返回值】
Long,非零表示成功,零表示失败

【参数表】
  hwnd -----------  Long,一个窗口的句柄,该窗口定义了要使用的客户区坐标系统

  lpPoint --------  POINTAPI,屏幕坐标系统中包含了屏幕点的结构。这个函数会随同相应的客户区坐标(由hwnd决定)载入结构

 

http://www.byywee.com/page/M0/S443/443605.html

/// <summary>
        /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备上下文环境中绘图。hWnd:设备上下文环境被检索的窗口的句柄
        /// </summary>
        [DllImport("user32.dll", CharSet= CharSet.Auto)]
        publicstaticextern IntPtr GetDC(IntPtr hWnd);
 
        [DllImport("user32.dll", ExactSpelling=true, CharSet= CharSet.Auto)]
        /// <summary>
        /// 该函数获得一个指定子窗口的父窗口句柄。
        /// </summary>
        publicstaticextern IntPtr GetParent(IntPtr hWnd);

 

        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);
        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);
       
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public static extern int GetSystemMetrics(int which);

        [DllImport("user32.dll")]
        public static extern void SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                         int X, int Y, int width, int height, uint flags);

 
//这个API作用是获取窗口的属性
     [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
      static extern int GetWindowLong(HandleRef hWnd, int nIndex);

      //这个API是专门设置窗口的属性的   
     [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
      static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

//获取窗体属性句柄
             int windowLong = (GetWindowLong(new HandleRef(form, form.Handle), -16));

             //设置窗体属性句柄
             SetWindowLong(new HandleRef(form, form.Handle), -16, windowLong | WndMessage.WS_SYSMENU | WndMessage.WS_MINIMIZEBOX);
          //注意,这里是重点:WS_SYSMENU允许有系统菜单 WS_MINIMIZEBOX:可以最大最小化
          //这两个参数是上面那个类里提供的

 [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
              CharSet = CharSet.Unicode, ExactSpelling = true,
              CallingConvention = CallingConvention.StdCall)]
         private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);

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

         [DllImport("user32.dll", SetLastError = true)]
         private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

         [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
         private static extern long GetWindowLong(IntPtr hwnd, int nIndex);

         [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
         private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
         //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);


         [DllImport("user32.dll", SetLastError = true)]
         private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

         [DllImport("user32.dll", SetLastError = true)]
         private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

         [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
         private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);


        /// <summary>
        /// MulDiv(a, b, c) 就是计算 a * b / c ,不过在 a * b > 2^32 仍保证结果正确
        /// </summary>
        /// <param name="nNumber"></param>
        /// <param name="nNumerator"></param>
        /// <param name="nDenominator"></param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern int MulDiv(int nNumber, int nNumerator, int nDenominator); 

[DllImport("gdi32", EntryPoint="CreateCompatibleDC")]
public static extern int CreateCompatibleDC (
        int hdc
);


【说明】
创建一个与特定设备场景一致的内存设备场景

【返回值】
Long,新设备场景句柄,若出错则为零

【参数表】
  hdc ------------  Long,设备场景句柄。新的设备场景将与它一致。也可能为0以创建一个与屏幕一致的设备场景

【其它说明】
在绘制之前,先要为该设备场景选定一个位图。不再需要时,该设备场景可用DeleteDC函数删除。删除前,其所有对象应回复初始状态

 

 [DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateSolidBrush(int crColor);
[DllImport("gdi32.dll")]
public static externbool ExtFloodFill(IntPtr hdc,int nXStart, int nYStart,
    int crColor,uint fuFillType);
[DllImport("gdi32.dll")]
public static externbool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static externint GetPixel(IntPtr hdc,int x, int y);


        //函数功能:该函数装载图标,光标,或位图。  
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr LoadImage(IntPtr hinst,[MarshalAs(UnmanagedType.LPTStr)]string lpszName, uint uType,int cxDesired, int cyDesired, uint fuLoad);
       

LPTSTR对应StringBuilder,String,Byte[],char[]都行,后两者在知道长度的情况下是没问题的,你这没有,但应该也行,写法如下:

StringBuilder:
[DllImport("<Unknown>", EntryPoint="SCardListReadersW")]
public static extern int SCardListReadersW(uint hContext, [In,MarshalAs(UnmanagedType.LPWStr)] string mszGroups, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder mszReaders, ref uint pcchReaders) ;

string:
[DllImport("

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值