前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#窗口句柄

C#窗口句柄

作者头像
全栈程序员站长
发布2022-09-14 11:14:54
5580
发布2022-09-14 11:14:54
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

在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
代码语言:javascript
复制
  1. //获取窗口标题
  2. [DllImport("user32", SetLastError = true)]
  3. public static extern int GetWindowText(
  4. IntPtr hWnd,//窗口句柄
  5. StringBuilder lpString,//标题
  6. int nMaxCount //最大值
  7. );
  8. //获取类的名字
  9. [DllImport("user32.dll")]
  10. private static extern int GetClassName(
  11. IntPtr hWnd,//句柄
  12. StringBuilder lpString, //类名
  13. int nMaxCount //最大值
  14. );
  15. //根据坐标获取窗口句柄
  16. [DllImport("user32")]
  17. private static extern IntPtr WindowFromPoint(
  18. Point Point //坐标
  19. );
  20. private void timer1_Tick(object sender, EventArgs e)
  21. {
  22. int x = Cursor.Position.X;
  23. int y = Cursor.Position.Y;
  24. Point p = new Point(x, y);
  25. IntPtr formHandle = WindowFromPoint(p);//得到窗口句柄
  26. StringBuilder title = new StringBuilder(256);
  27. GetWindowText(formHandle, title, title.Capacity);//得到窗口的标题
  28. StringBuilder className = new StringBuilder(256);
  29. GetClassName(formHandle, className, className.Capacity);//得到窗口的句柄
  30. this.textBox1.Text = title.ToString();
  31. this.textBox2.Text = formHandle.ToString();
  32. this.textBox3.Text = className.ToString();
  33. }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158985.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档