C#窗口句柄

在Windows中,句柄是一个系统内部数据结构的引用。例如当你操作一个窗口,或说是一个Delphi窗体时,系统会给你一个该窗口的句柄,系统会通知你:你正在操作142号窗口,就此你的应用程序就能要求系统对142号窗口进行操作——移动窗口、改变窗口大小、把窗口极小化为图标等。实际上许多 Windows API函数把句柄作为它的第一个参数,如GDI(图形设备接口)句柄、菜单句柄、实例句柄、位图句柄等,不仅仅局限于窗口函数。换句话说,句柄是一种内部代码,通过它能引用受系统控制的特殊元素,如窗口、位图、图标、内存块、光标、字体、菜单等。

  1. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

  2. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

  3.   Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

  4.   Private Const WS_EX_LAYERED = &H80000

  5.   Private Const GWL_EXSTYLE = (-20)

  6.   Private Const LWA_ALPHA = &H2

  7.   Private Sub Form_Activate()

  8.   On Error Resume Next

  9.   For i = 0 To 150 Step 2.5

  10.   SetLayeredWindowAttributes Me.hwnd, 0, i, LWA_ALPHA

  11.   DoEvents

  12.   Next i

  13.   End Sub

  14.   Private Sub Form_load()

  15.   Dim rtn As Long

  16.   rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE)

  17.   rtn = rtn Or WS_EX_LAYERED

  18.   SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn

  19.   SetLayeredWindowAttributes Me.hwnd, 0, 0, LWA_ALPHA

  20.   End Sub

 

 
  1. //获取窗口标题

  2. [DllImport("user32", SetLastError = true)]

  3. public static extern int GetWindowText(

  4. IntPtr hWnd,//窗口句柄

  5. StringBuilder lpString,//标题

  6. int nMaxCount //最大值

  7. );

  8.  
  9. //获取类的名字

  10. [DllImport("user32.dll")]

  11. private static extern int GetClassName(

  12. IntPtr hWnd,//句柄

  13. StringBuilder lpString, //类名

  14. int nMaxCount //最大值

  15. );

  16.  
  17. //根据坐标获取窗口句柄

  18. [DllImport("user32")]

  19. private static extern IntPtr WindowFromPoint(

  20. Point Point //坐标

  21. );

  22.  
  23. private void timer1_Tick(object sender, EventArgs e)

  24. {

  25. int x = Cursor.Position.X;

  26. int y = Cursor.Position.Y;

  27. Point p = new Point(x, y);

  28. IntPtr formHandle = WindowFromPoint(p);//得到窗口句柄

  29. StringBuilder title = new StringBuilder(256);

  30. GetWindowText(formHandle, title, title.Capacity);//得到窗口的标题

  31. StringBuilder className = new StringBuilder(256);

  32. GetClassName(formHandle, className, className.Capacity);//得到窗口的句柄

  33. this.textBox1.Text = title.ToString();

  34. this.textBox2.Text = formHandle.ToString();

  35. this.textBox3.Text = className.ToString();

  36. }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值