溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#調用Win32的API函數(shù)的方法

發(fā)布時間:2020-06-17 14:39:00 來源:億速云 閱讀:289 作者:元一 欄目:編程語言

Win32的API函數(shù)是微軟自己的東西,可以直接在C#中直接調用,在做WinForm時還是很有幫助的。有時候我們之直接調用Win32 的API,可以很高效的實現(xiàn)想要的效果。

代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsAPI
{
class CSharp_Win32Api
{
#region User32.dll 函數(shù)
/// <summary>
/// 該函數(shù)檢索一指定窗口的客戶區(qū)域或整個屏幕的顯示設備上下文環(huán)境的句柄,以后可以在GDI函數(shù)中使用該句柄來在設備上下文環(huán)境中繪圖。hWnd:設備上

下文環(huán)境被檢索的窗口的句柄
/// </summary>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDC(IntPtr hWnd);
/// <summary>
/// 函數(shù)釋放設備上下文環(huán)境(DC)供其他應用程序使用。
/// </summary>
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
/// <summary>
/// 該函數(shù)返回桌面窗口的句柄。桌面窗口覆蓋整個屏幕。
/// </summary>
static public extern IntPtr GetDesktopWindow();
/// <summary>
/// 該函數(shù)設置指定窗口的顯示狀態(tài)。
/// </summary>
static public extern bool ShowWindow(IntPtr hWnd, short State);
/// <summary>
/// 通過發(fā)送重繪消息 WM_PAINT 給目標窗體來更新目標窗體客戶區(qū)的無效區(qū)域。
/// </summary>
static public extern bool UpdateWindow(IntPtr hWnd);
/// <summary>
/// 該函數(shù)將創(chuàng)建指定窗口的線程設置到前臺,并且激活該窗口。鍵盤輸入轉向該窗口,并為用戶改各種可視的記號。系統(tǒng)給創(chuàng)建前臺窗口的線程分配的權限稍

高于其他線程。
/// </summary>
static public extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 該函數(shù)改變一個子窗口,彈出式窗口式頂層窗口的尺寸,位置和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>
/// 將存放有數(shù)據(jù)的內存塊放入剪切板的資源管理中
/// </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>
    /// 該函數(shù)獲得一個指定子窗口的父窗口句柄。
    /// </summary>
    public static extern IntPtr GetParent(IntPtr hWnd);
    /// <summary>
    /// 該函數(shù)將指定的消息發(fā)送到一個或多個窗口。此函數(shù)為指定的窗口調用窗口程序,直到窗口程序處理完消息再返回?!?
    /// </summary>
    /// <param name="hWnd">其窗口程序將接收消息的窗口的句柄</param>
    /// <param name="msg">指定被發(fā)送的消息</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>
    /// 該函數(shù)將一個消息放入(寄送)到與指定窗口創(chuàng)建的線程相聯(lián)系消息隊列里
    /// </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>
    /// 該函數(shù)對指定的窗口設置鍵盤焦點。
    /// </summary>
    public static extern IntPtr SetFocus(IntPtr hWnd);
    /// <summary>
    /// 該函數(shù)在指定的矩形里寫入格式化文本,根據(jù)指定的方法對文本格式化(擴展的制表符,字符對齊、折行等)。
    /// </summary>
    public extern static int DrawText(IntPtr hdc, string lpString, int nCount, ref RECT lpRect, int uFormat);
    /// <summary>
    /// 該函數(shù)改變指定子窗口的父窗口。
    /// </summary>
    public extern static IntPtr SetParent(IntPtr hChild, IntPtr hParent);
    /// <summary>
    /// 獲取對話框中子窗口控件的句柄
    /// </summary>
    public extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);
    /// <summary>
    /// 該函數(shù)獲取窗口客戶區(qū)的坐標。
    /// </summary>
    public extern static int GetClientRect(IntPtr hWnd, ref RECT rc);
    /// <summary>
    /// 該函數(shù)向指定的窗體添加一個矩形,然后窗口客戶區(qū)域的這一部分將被重新繪制。
    /// </summary>
    public extern static int InvalidateRect(IntPtr hWnd, IntPtr rect, int bErase);
    /// <summary>
    /// 該函數(shù)產生對其他線程的控制,如果一個線程沒有其他消息在其消息隊列里。
    /// </summary>
    public static extern bool WaitMessage();
    /// <summary>
    /// 該函數(shù)為一個消息檢查線程消息隊列,并將該消息(如果存在)放于指定的結構。
    /// </summary>
    public static extern bool PeekMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax, uint wFlag);
    /// <summary>
    /// 該函數(shù)從調用線程的消息隊列里取得一個消息并將其放于指定的結構。此函數(shù)可取得與指定窗口聯(lián)系的消息和由PostThreadMesssge寄送的線程消息。此函

數(shù)接收一定范圍的消息值。
/// </summary>
public static extern bool GetMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax);
/// <summary>
/// 該函數(shù)將虛擬鍵消息轉換為字符消息。
/// </summary>
public static extern bool TranslateMessage(ref MSG msg);
/// <summary>
/// 該函數(shù)調度一個消息給窗口程序。
/// </summary>
public static extern bool DispatchMessage(ref MSG msg);
/// <summary>
/// 該函數(shù)從一個與應用事例相關的可執(zhí)行文件(EXE文件)中載入指定的光標資源.
/// </summary>
public static extern IntPtr LoadCursor(IntPtr hInstance, uint cursor);
/// <summary>
/// 該函數(shù)確定光標的形狀。
/// </summary>
public static extern IntPtr SetCursor(IntPtr hCursor);
/// <summary>
/// 確定當前焦點位于哪個控件上。
/// </summary>
public static extern IntPtr GetFocus();
/// <summary>
/// 該函數(shù)從當前線程中的窗口釋放鼠標捕獲,并恢復通常的鼠標輸入處理。捕獲鼠標的窗口接收所有的鼠標輸入(無論光標的位置在哪里),除非點擊鼠標鍵

時,光標熱點在另一個線程的窗口中。
/// </summary>
public static extern bool ReleaseCapture();
/// <summary>
/// 準備指定的窗口來重繪并將繪畫相關的信息放到一個PAINTSTRUCT結構中。
/// </summary>
public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
/// <summary>
/// 標記指定窗口的繪畫過程結束,每次調用BeginPaint函數(shù)之后被請求
/// </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>
/// 該函數(shù)返回指定窗口的邊框矩形的尺寸。該尺寸以相對于屏幕坐標左上角的屏幕坐標給出。
/// </summary>
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
/// <summary>
/// 該函數(shù)將指定點的用戶坐標轉換成屏幕坐標。
/// </summary>
public static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);
/// <summary>
/// 當在指定時間內鼠標指針離開或盤旋在一個窗口上時,此函數(shù)寄送消息。
/// </summary>
public static extern bool TrackMouseEvent(ref TRACKMOUSEEVENTS tme);
/// <summary>
///
/// </summary>
public static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
/// <summary>
/// 該函數(shù)檢取指定虛擬鍵的狀態(tài)。
/// </summary>
public static extern ushort GetKeyState(int virtKey);
/// <summary>
/// 該函數(shù)改變指定窗口的位置和尺寸。對于頂層窗口,位置和尺寸是相對于屏幕的左上角的:對于子窗口,位置和尺寸是相對于父窗口客戶區(qū)的左上角坐標的


/// </summary>
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
/// <summary>
/// 該函數(shù)獲得指定窗口所屬的類的類名。
/// </summary>
public static extern int GetClassName(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);
/// <summary>
/// 該函數(shù)改變指定窗口的屬性
/// </summary>
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
/// <summary>
/// 該函數(shù)檢索指定窗口客戶區(qū)域或整個屏幕的顯示設備上下文環(huán)境的句柄,在隨后的GDI函數(shù)中可以使用該句柄在設備上下文環(huán)境中繪圖。
/// </summary>
public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hRegion, uint flags);
/// <summary>
/// 獲取整個窗口(包括邊框、滾動條、標題欄、菜單等)的設備場景 返回值 Long。
/// </summary>
public static extern IntPtr GetWindowDC(IntPtr hWnd);
/// <summary>
/// 該函數(shù)用指定的畫刷填充矩形,此函數(shù)包括矩形的左上邊界,但不包括矩形的右下邊界。
/// </summary>
public static extern int FillRect(IntPtr hDC, ref RECT rect, IntPtr hBrush);
/// <summary>
/// 該函數(shù)返回指定窗口的顯示狀態(tài)以及被恢復的、最大化的和最小化的窗口位置。
/// </summary>
public static extern int GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT wp);
/// <summary>
/// 該函數(shù)改變指定窗口的標題欄的文本內容
/// </summary>
public static extern int SetWindowText(IntPtr hWnd, string text);
/// <summary>
/// 該函數(shù)將指定窗口的標題條文本(如果存在)拷貝到一個緩存區(qū)內。如果指定的窗口是一個控制,則拷貝控制的文本。
/// </summary>
public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER text, int maxCount);
/// <summary>
/// 用于得到被定義的系統(tǒng)數(shù)據(jù)或者系統(tǒng)配置信息.
/// </summary>
static public extern int GetSystemMetrics(int nIndex);
/// <summary>
/// 該函數(shù)設置滾動條參數(shù),包括滾動位置的最大值和最小值,頁面大小,滾動按鈕的位置。
/// </summary>
static public extern int SetScrollInfo(IntPtr hwnd, int bar, ref SCROLLINFO si, int fRedraw);
/// <summary>
/// 該函數(shù)顯示或隱藏所指定的滾動條。
/// </summary>
public static extern int ShowScrollBar(IntPtr hWnd, int bar, int show);
/// <summary>
/// 該函數(shù)可以激活一個或兩個滾動條箭頭或是使其失效。
/// </summary>
public static extern int EnableScrollBar(IntPtr hWnd, uint flags, uint arrows);
/// <summary>
/// 該函數(shù)將指定的窗口設置到Z序的頂部。
/// </summary>
public static extern int BringWindowToTop(IntPtr hWnd);
/// <summary>
/// 該函數(shù)滾動指定窗體客戶區(qū)域的目錄。
/// </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>
/// 該函數(shù)確定給定的窗口句柄是否識別一個已存在的窗口。
/// </summary>
public static extern int IsWindow(IntPtr hWnd);
/// <summary>
/// 該函數(shù)將256個虛擬鍵的狀態(tài)拷貝到指定的緩沖區(qū)中。
/// </summary>
public static extern int GetKeyboardState(byte[] pbKeyState);
/// <summary>
/// 該函數(shù)將指定的虛擬鍵碼和鍵盤狀態(tài)翻譯為相應的字符或字符串。該函數(shù)使用由給定的鍵盤布局句柄標識的物理鍵盤布局和輸入語言來翻譯代碼。
/// </summary>
public static extern int ToAscii(int uVirtKey,int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey,int fuState);
#endregion

}

擴展知識:

C#是微軟公司發(fā)布的一種面向對象的、運行于.NET Framework和.NET Core(完全開源,跨平臺)之上的高級程序設計語言。

C#是微軟公司研究員Anders Hejlsberg的最新成果,是由C和C++衍生出來的面向對象的編程語言,在繼承C和C++強大功能的同時去掉了一些它們的復雜特性(例如沒有宏以及不允許多重繼承)。C#與Java有相似又有不同,它借鑒了Delphi的一個特點,與COM(組件對象模型)是直接集成的,而且它是微軟公司 .NET windows網絡框架的主角。

C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優(yōu)雅的語法風格、創(chuàng)新的語言特性和便捷的面向組件編程的支持成為.NET開發(fā)的首選語言。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI