Windows的所有句柄类型定义

Windows的所有句柄类型定义

编者:李国帅

qq:9611153 微信lgs9611153

时间:2012-11-21

问题相关:

包含4个内容:常见句柄类型,全部句柄及描述,句柄函数宏定义,MFC中GDI类使用

常见句柄类型:

文件:ntdll.h

类型:OB_TYPE_TYPE

 

   句柄类型 说明

   HBITMP:标示位图句柄

   HBRUSH:标示画刷句柄

   HCOURSOR:标示鼠标光标句柄

   HDC:标示设备环境句柄

   HFONT:标示字体句柄

   HICON:标示图标句柄

   HINSTANCE:标示当前实例句柄

   HMENU:标示菜单句柄

   HPALETTE:颜色调色板句柄

   HPEN:标示画笔句柄

   HWND:标示窗口句柄

   HFILE:标示文件句柄

 

全部句柄及描述:

Object      Description

Access token

         The handle is returned by the CreateRestrictedToken, DuplicateToken, DuplicateTokenEx, OpenProcessToken, or OpenThreadToken function.

Change notification

         The handle is returned by the FindFirstChangeNotification function.

Communications device

         The handle is returned by the CreateFile function.

Console input

         The handle is returned by the CreateFile function when CONIN$ is specified, or by the GetStdHandle function when STD_INPUT_HANDLE is specified. Console handles can be duplicated for use only in the same process.

Console screen buffer

         The handle is returned by the CreateFile function when CONOUT$ is specified, or by the GetStdHandle function when STD_OUTPUT_HANDLE is specified. Console handles can be duplicated for use only in the same process.

Desktop

         The handle is returned by the GetThreadDesktop function.

Event

         The handle is returned by the CreateEvent or OpenEvent function.

File

         The handle is returned by the CreateFile function.

File mapping

         The handle is returned by the CreateFileMapping function.

Job

         The handle is returned by the CreateJobObject function.

Mailslot

         The handle is returned by the CreateMailslot function.

Mutex

         The handle is returned by the CreateMutex or OpenMutex function.

Pipe

         A named pipe handle is returned by the CreateNamedPipe or CreateFile function. An anonymous pipe handle is returned by the CreatePipe function.

Process

         The handle is returned by the CreateProcess, GetCurrentProcess, or OpenProcess function.

Registry key

         The handle is returned by the RegCreateKey, RegCreateKeyEx, RegOpenKey, or RegOpenKeyEx function. Note that registry key handles returned by the RegConnectRegistry function cannot be used in a call to DuplicateHandle.

Windows Me/98/95:  You cannot use DuplicateHandle to duplicate registry key handles.

Semaphore

         The handle is returned by the CreateSemaphore or OpenSemaphore function.

Socket

         The handle is returned by the socket or accept function.

Thread

         The handle is returned by the CreateProcess, CreateThread, CreateRemoteThread, or GetCurrentThread function

Timer

         The handle is returned by the CreateWaitableTimer or OpenWaitableTimer function.

Window station

         The handle is returned by the GetProcessWindowStation function.

 

句柄函数宏定义:

很多宏看上去很形象,但实际上隐藏了细节

文件:windowsx.h

 

/****** KERNEL Macro APIs ****************************************************/

#define     GetInstanceModule(hInstance) (HMODULE)(hInstance)

 

#define     GlobalPtrHandle(lp)    ((HGLOBAL)GlobalHandle(lp))

 

#define     GlobalLockPtr(lp)    ((BOOL)GlobalLock(GlobalPtrHandle(lp)))

#define     GlobalUnlockPtr(lp)    GlobalUnlock(GlobalPtrHandle(lp))

 

#define     GlobalAllocPtr(flags, cb)   (GlobalLock(GlobalAlloc((flags), (cb))))

#define     GlobalReAllocPtr(lp, cbNew, flags)  (GlobalUnlockPtr(lp), GlobalLock(GlobalReAlloc(GlobalPtrHandle(lp) , (cbNew), (flags))))

#define     GlobalFreePtr(lp)  (GlobalUnlockPtr(lp), (BOOL)(ULONG_PTR)GlobalFree(GlobalPtrHandle(lp)))

 

/****** GDI Macro APIs *******************************************************/

 

#define     DeletePen(hpen)      DeleteObject((HGDIOBJ)(HPEN)(hpen))

#define     SelectPen(hdc, hpen)    ((HPEN)SelectObject((hdc), (HGDIOBJ)(HPEN)(hpen)))

#define     GetStockPen(i)       ((HPEN)GetStockObject(i))

 

#define     DeleteBrush(hbr)     DeleteObject((HGDIOBJ)(HBRUSH)(hbr))

#define     SelectBrush(hdc, hbr)   ((HBRUSH)SelectObject((hdc), (HGDIOBJ)(HBRUSH)(hbr)))

#define     GetStockBrush(i)     ((HBRUSH)GetStockObject(i))

 

#define     DeleteRgn(hrgn)      DeleteObject((HGDIOBJ)(HRGN)(hrgn))

 

#define     CopyRgn(hrgnDst, hrgnSrc)               CombineRgn(hrgnDst, hrgnSrc, 0, RGN_COPY)

#define     IntersectRgn(hrgnResult, hrgnA, hrgnB)  CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_AND)

#define     SubtractRgn(hrgnResult, hrgnA, hrgnB)   CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_DIFF)

#define     UnionRgn(hrgnResult, hrgnA, hrgnB)      CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_OR)

#define     XorRgn(hrgnResult, hrgnA, hrgnB)        CombineRgn(hrgnResult, hrgnA, hrgnB, RGN_XOR)

 

#define     DeletePalette(hpal)     DeleteObject((HGDIOBJ)(HPALETTE)(hpal))

 

#define     DeleteFont(hfont)            DeleteObject((HGDIOBJ)(HFONT)(hfont))

#define     SelectFont(hdc, hfont)  ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))

#define     GetStockFont(i)      ((HFONT)GetStockObject(i))

 

#define     DeleteBitmap(hbm)       DeleteObject((HGDIOBJ)(HBITMAP)(hbm))

#define     SelectBitmap(hdc, hbm)  ((HBITMAP)SelectObject((hdc), (HGDIOBJ)(HBITMAP)(hbm)))

 

#define     InsetRect(lprc, dx, dy) InflateRect((lprc), -(dx), -(dy))

...

 

MFC的CGdiObject对象不需要手动释放句柄:

对于MFC的CGdiObject对象,并不需要手动释放,离栈自动释放句柄。例如

         CBitmap BmpSlider;

         BmpSlider.LoadBitmap(IDB_TOOLBK);

         //BmpSlider.DeleteObject();//不需要手动删除对象,CBitmap离栈自动释放句柄

 

代码跟踪:

 

	class CBitmap : public CGdiObject
	class CGdiObject : public CObject

	_AFXWIN_INLINE CGdiObject::~CGdiObject()
	{
	    AFX_BEGIN_DESTRUCTOR
	        DeleteObject();
	    AFX_END_DESTRUCTOR
	}

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在VB中遍历窗口所有窗体句柄可以使用API函数EnumWindows。EnumWindowsWindows操作系统提供的枚举窗口的函数,在VB中可以通过声明API函数的方式使用它。 首先,在Module中声明EnumWindows需要的API函数,如下: Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long 然后,在Form中编写一个回调函数,用于遍历窗口: Private Function EnumWindowProc(ByVal hwnd As Long, ByVal lParam As Long) As Long Dim text As String Dim len As Long len = GetWindowTextLength(hwnd) + 1 text = Space(len) GetWindowText hwnd, text, len '将窗口句柄和窗口标题输出 Debug.Print "HWND: " & hwnd & ", Title: " & Left(text, len - 1) EnumWindowProc = 1 End Function 最后,在需要进行遍历的地方调用EnumWindows函数: EnumWindows AddressOf EnumWindowProc, 0 这样就可以遍历当前所有窗口的句柄,并且输出它们的标题。可以根据实际需求,将窗口句柄和标题进行处理和使用。 ### 回答2: 在VB中,可以使用API函数来遍历窗口所有窗体句柄。下面是一种实现方法: 首先,在模块中声明API函数GetWindow和GetWindowLong,用于获取窗体句柄和窗体样式: Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long 然后,在表单的代码中,定义一个遍历窗体句柄的函数: Private Sub TraverseWindowsHandles() Dim hWndParent As Long Dim hWndChild As Long ' 获取桌面窗口的句柄 hWndParent = GetDesktopWindow() ' 遍历所有窗口 hWndChild = GetWindow(hWndParent, GW_CHILD) Do While hWndChild <> 0 ' 对每个窗口进行操作,例如打印窗体标题 Debug.Print GetWindowText(hWndChild) ' 继续遍历下一个窗口 hWndChild = GetWindow(hWndChild, GW_HWNDNEXT) Loop End Sub 在以上代码中,先获取桌面窗口的句柄,然后通过GetWindow函数遍历所有子窗口的句柄,通过GetWindowText函数获取窗口的标题,然后可以对每个窗口进行进一步的处理。 注意,以上代码仅是演示如何遍历窗体句柄,并打印窗体标题。实际应用中,可以根据需求对窗口进行其他操作,如查找特定类型的窗口、获取窗口信息等。 希望以上解答能对您有所帮助! ### 回答3: 在VB中,要遍历窗口的所有窗体句柄,可以使用Windows API函数来实现。下面是一个示例代码: ```vb Option Explicit Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Sub Form_Load() EnumWindows AddressOf EnumWindowsCallback, 0 End Sub Private Function EnumWindowsCallback(ByVal hWnd As Long, ByVal lParam As Long) As Long Dim title As String Dim length As Long ' 获取窗口标题的长度 length = GetWindowTextLength(hWnd) + 1 If length > 1 Then ' 获取窗口标题 title = String(length, Chr$(0)) GetWindowText(hWnd, title, length) ' 显示窗口句柄和标题 Debug.Print "窗口句柄: " & hWnd & " 窗口标题: " & title End If EnumWindowsCallback = 1 End Function ``` 这段代码使用了`EnumWindows`函数来枚举所有顶级窗口,然后通过`GetWindowTextLength`和`GetWindowText`函数获取窗口的标题信息。在每次枚举到窗口时,会将窗口句柄和标题打印输出。 注意,这段代码需要在Windows系统上运行,因为它使用了Windows API函数。在VB的窗体加载时,调用`EnumWindows`函数开始遍历窗口,并将窗口句柄和标题打印输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微澜-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值