CWnd* pWnd=FromeHandle(hMyHandle); pWnd->SetWindowText("This is just an example!");orCWnd* pWnd; pWnd->Attach(hMyHandle);
There are different types of handles in Win32 API, the three mains types are GDI handles, USEER handles and KERNEL handles.GDI handles are DDB handles, DIB section handles, region handles, font handles, brush handles, pen handles, ..USER handles are icon handles, cursor handles, windows handles, menu handles,KERNEL handles are file handles, device handels, thread handles, process handles, ...The basic information within a handle is normall an index into an hidden table kept by the system. For example, the lower 12 or 16-bit of a GDI handle is an index into GDI handle table kept by kernel mode Win32k.sys. On Win9X, GDI handle is just an index. On NT-based systesm, GDI handle has extra information.Handles are normally not pointers, although Win32 SDK header files define them as pointers.If you want to know everything you ever wanted to know about GDI handles, read the chapter 3 of my book.
句柄是指针的“指针”,使用句柄主要是为了利于windows在进程内存地址空间移动分配的内存块,以防止进程的内存空间被撕的四分五裂而存在过多的碎片
句柄是一些表的索引也就是指向指针的指针。间接的引用对象,windows可以修改对象的"物理"地址和 描述器的值,但是句柄的值是不变的。
句柄和指针都是地址,不同在于:
1,句柄所指的可以是一个很复杂的结构,并且很有可以是与系统有关的,比如说上面所说的线程的句柄,它指向的就是一个很类或者结构,他和系统有很密切的关系,当一个线程由于不可预料的原因,而终止时在系统就可以回它所占用的资料,如CPU,内存等等,反过来想可以知道,这个句柄中的某一些项,是与系统进行交互的。由于Windows系统,是一个多任务的系统,它随时都可能要分配内存,回收内存,重组内存。
2,指针它也可以指向一个复杂的结构,但是通常是用户定义的,所以的必需的工作都要用户完成,特别是在删除的时候。
但在VC++6.0中也有一些指针,它们都是处理一些小问题才用的,如最常见的字符的指针,它也是要用户处理的如果你动态分配了内存;但是Cstring就不要用户处理了,它其实是VC++中的一个类,所以的操作都由成员函数完成,产生(分配)由构造函数,删除(回收)由析构函数完成
pWnd->SetWindowText("This is just an example!");orCWnd* pWnd;
pWnd->Attach(hMyHandle);
比如对于Window句柄:
static CWnd* PASCAL FromHandle(
HWND hWnd
);
HWND GetSafeHwnd( ) const;对于位图:
static CBitmap* PASCAL FromHandle(
HBITMAP hBitmap
);
static CGdiObject* PASCAL FromHandle(
HGDIOBJ hObject
);
HGDIOBJ GetSafeHandle( ) const;这些东西自己查一下MSDN就都有了。