C#窗口句柄
在Windows中,句柄是一個系統內部數據結構的引用。例如當你操作一個窗口,或說是一個Delphi窗體時,系統會給你一個該窗口的句柄,系統會通知你:你正在操作142號窗口,就此你的應用程序就能要求系統對142號窗口進行操作——移動窗口、改變窗口大小、把窗口極小化為圖標等。實際上許多 Windows API函數把句柄作為它的第一個參數,如GDI(圖形設備接口)句柄、菜單句柄、實例句柄、位圖句柄等,不僅僅局限于窗口函數。換句話說,句柄是一種內部代碼,通過它能引用受系統控制的特殊元素,如窗口、位圖、圖標、內存塊、光標、字體、菜單等。
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2
Private Sub Form_Activate()
On Error Resume Next
For i = 0 To 150 Step 2.5
SetLayeredWindowAttributes Me.hwnd, 0, i, LWA_ALPHA
DoEvents
Next i
End Sub
Private Sub Form_load()
Dim rtn As Long
rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn
SetLayeredWindowAttributes Me.hwnd, 0, 0, LWA_ALPHA
End Sub
?
//獲取窗口標題
[DllImport("user32", SetLastError = true)]
public static extern int GetWindowText(
IntPtr hWnd,//窗口句柄
StringBuilder lpString,//標題
int nMaxCount //最大值
);
//獲取類的名字
[DllImport("user32.dll")]
private static extern int GetClassName(
IntPtr hWnd,//句柄
StringBuilder lpString, //類名
int nMaxCount //最大值
);
//根據坐標獲取窗口句柄
[DllImport("user32")]
private static extern IntPtr WindowFromPoint(
Point Point //坐標
);
private void timer1_Tick(object sender, EventArgs e)
{
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
Point p = new Point(x, y);
IntPtr formHandle = WindowFromPoint(p);//得到窗口句柄
StringBuilder title = new StringBuilder(256);
GetWindowText(formHandle, title, title.Capacity);//得到窗口的標題
StringBuilder className = new StringBuilder(256);
GetClassName(formHandle, className, className.Capacity);//得到窗口的句柄
this.textBox1.Text = title.ToString();
this.textBox2.Text = formHandle.ToString();
this.textBox3.Text = className.ToString();
}
總結
- 上一篇: 【合集】高等数学随堂笔记-乐经良老师-全
- 下一篇: macOS 开发 - Command L