INSTANCE/HWND/CWnd/HANDLE区别

本文详细介绍了Windows编程中常见的句柄类型,包括HINSTANCE、HWND、HANDLE等,并提供了这些句柄之间的转换方法。此外,还列举了如何获取不同类型的句柄,如窗口句柄、进程句柄等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HINSTANCE是应用程序实例句柄,

HWND是窗口对象句柄,

HANDLE是任意对象的句柄,

CWnd是MFC中的窗口类。

 

MSDN里面对于HINSTANCE的解释是"handle   to   an   instance"   就是说是一个instance的句柄。而对instance的解释是"An   object   for   which   memory   is   allocated   or   which   is   persistent."   占有内存的一个对象。

对于HWND的解释是“Handle   to   a   window.”而对window的解释是"In   a   graphical   Windows-based   application,   a   window   is   a   rectangular   area   of   the   screen   where   the   application   displays   output   and   receives   input   from   the   user.   Therefore,   one   of   the   first   tasks   of   a   graphical   Windows-based   application   is   to   create   a   window.   "  就是说是屏幕上的一块区域。

CWnd是MFC的一个类了,它有窗体,几乎所有有图形显示的类都是从它派生的,它自己是从CCmdTarget类派生的,所以它可以接受消息。CCmdTarget类的爸爸可就是CObject了。

msdn对于Handle的解释是"Handle   to   an   object."   ,简直是废话。自己怎么解释自己呢。可是好像也只能如此了。我感觉句柄就可以理解为控制对象的一个…………东西吧。   

 

 

 

转载:http://topic.youkuaiyun.com/t/20030331/14/1599133.html

------------------------------------------------------------------------------

ID--HANDLE--HWND三者之间的互相转换 

ID--HANDLE--HWND三者之间的互相转换
id->句柄-----------hWnd = ::GetDlgItem(hParentWnd,id);
id->指针-----------CWnd::GetDlgItem();
句柄->id-----------id = GetWindowLong(hWnd,GWL_ID);
句柄->指针--------CWnd *pWnd=CWnd::FromHandle(hWnd);
指针->ID----------id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID);
                                            GetDlgCtrlID();
指针->句柄--------hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;  

 

-------------------------------------------------------------------------------

应用程序的一些HANDLE 

// 得到窗口句柄 
HWDN parenthwnd = ::FindWindowEx(NULL, parenthwnd, "#32770", NULL); 
// 得到此窗口的主线程ID 
DWORD dwThreadId = ::GetWindowThreadProcessId(parenthwnd, 0); 
// 得到当前进程的句柄 
HANDLE hApp = GetModuleHandle(NULL); // 参数NULL表示获取当前调用函数的进程句柄,你也可以通过完整路径的可执行文件名/DLL,来获取当前加载在当前进程地址空间的其他模块句柄 

对补充问题回答:那是不可能的,目前你只能通过GetModuleHandle来获取进程句柄。 
补充回答:OpenProcess是新打开一个进程的时候获取其句柄,GetModuleHandle是用来获取已经加载调用进程地址空间的模块句柄。参数NULL表示当前进程句柄(也就是说即使你在DLL里面用了这个函数,若传入的参数为NULL,则返回的并不是DLL的句柄,而是调用DLL的进程句柄),如果要获取进程地址空间中的其他模块句柄,则需要传入模块的名字【此名字可以带全路径(注意路径要用反斜杠表示'//',当然只写文件名最好】如: 
#include<windows.h> 
#include<iostream> 
using namespace std; 
void main() 
{ 
HANDLE handle; 
char *pNotFound = {"the module not found!"}; 
cout << "In the calling process address sapce:/n"; // 显示在调用进程地址空间中加载的模块句柄 

cout << "the handle of the calling process is: "; 
if(handle = GetModuleHandle(NULL))cout << handle <<endl;// 调用进程句柄 
else cout << pNotFound << endl; 

cout << "the handle of the kernel32.dll is: ";// kernel32.dll模块 
if(handle = GetModuleHandle(L"kernel32"))cout << handle << endl; // 参数不带扩展名,会被默认为DLL
else cout << pNotFound << endl; 

cout << "the handle of the ntdll.dll is: "; // netdll.dll模块 
if(handle = GetModuleHandle(L"c://windows//system32//ntdll.dll"))cout << handle << endl; // 以带全路径文件名的方式传入参数 
else cout << pNotFound << endl; 

cout << "the handle of the xxx.dll is: "; // xxx.dll模块不存在 
if(handle = GetModuleHandle(L"xxx.dll"))cout << handle << endl; 
else cout << pNotFound << endl; 
} 
#include <windows.h> class CWnd { public: BOOL CreateEx(DWORD dwExStyle, // extended window style LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam); // window-creation data BOOL ShowWindow(int nCmdShow); BOOL UpdateWindow(); public: HWND m_hWnd; }; BOOL CWnd::CreateEx(DWORD dwExStyle, // extended window style LPCTSTR lpClassName, // registered class name LPCTSTR lpWindowName, // window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam) // window-creation data { m_hWnd=::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle, x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam); if(m_hWnd!=NULL) return TRUE; else return FALSE; } BOOL CWnd::ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd,nCmdShow); } BOOL CWnd::UpdateWindow() { return ::UpdateWindow(m_hWnd); } LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { WNDCLASS MyWnd; MyWnd.cbClsExtra = NULL; MyWnd.cbWndExtra = NULL; MyWnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); MyWnd.hCursor = LoadCursor(NULL, IDC_ARROW); MyWnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); MyWnd.hInstance = hInstance; MyWnd.lpfnWndProc = MyWndProc; MyWnd.lpszClassName = "Hello"; MyWnd.lpszMenuName = NULL; MyWnd.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&MyWnd); CWnd wnd; wnd.CreateEx(0,"Hello","CWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); wnd.ShowWindow(SW_SHOWNORMAL); wnd.UpdateWindow(); MSG msg; while (GetMessage(&msg,NULL,0,0)) //?????????? { TranslateMessage(&msg); //???? //4.???? DispatchMessage(&msg); //??????????? } return 0; } LRESULT CALLBACK MyWndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch (uMsg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); // 在窗口中心绘制"Hello, World!" RECT rect; GetClientRect(hwnd, &rect); const char* text = "Hello, World!"; DrawText( hdc,text, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER ); EndPaint(hwnd, &ps); break; } case WM_MOUSEMOVE: if (wParam & MK_LBUTTON) // 左键拖拽时绘制 { // 记录轨迹点... InvalidateRect(hwnd, NULL, FALSE); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,uMsg,wParam,lParam); } return 0; } 拓展各种功能,尽可能多
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值