出处: 方塘2003.9.22 整理,之所以前面引用了 贾旭滨 先生的这段描述,因为他写的是在精彩!
=== 重庆大学光电工程学院 贾旭滨 对句柄有下面的描述:
=== 有关 句柄或指针的 常用函数
1. 如何获取应用程序的 实例句柄? AfxGetInstanceHandle()
应用程序的 实例句柄保存在CWinAppIm_hInstance 中,可以这么调用 AfxGetInstancdHandle获得句柄.
Example: HANDLE hInstance=AfxGetInstanceHandle();
2. 如何通过代码获得应用程序主窗口的 指针? AfxGetMainWnd GetSafeHwnd() AfxGetAppName() AfxGetThread
主窗口的 指针保存在CWinThread::m_pMainWnd中,调用 AfxGetMainWnd实现。
【例】 AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED); //使程序最大化.
【例】此例的主窗口是对话框,下面的代码是在另外一个CFileTreeCtrl 类(子窗)中相关函数实现在主对话框(主窗)中的几个 静态文本框(子窗)中显示路径:
CWnd* m_pCWnd= AfxGetMainWnd(); //得到主窗口指针,通过主窗指针访问其他子窗资源
//方法一
m_pCWnd->SetDlgItemText(IDC_STATIC_path,"CWnd* "+m_sCurPath); //在主窗中的子窗(ID:IDC_STATIC_path)中显示字符串
m_pCWnd->SetDlgItemText(IDC_STATIC_who,"路径显示由FileTreeCtrl类完成:");
//方法二
m_pCWnd->SendMessage(STN_CLICKED); //向主窗口发送一个消息,显示任务由主窗完成。
//在主窗的.cpp中有:ON_MESSAGE(STN_CLICKED, OnSTATICpath3)相关描述
//有的函数必须通过窗口的 句柄 来访问,我们可以使用下面的方法三
//CWnd::GetSafeHwnd
//Returns the window handle for a window
//HWND GetSafeHwnd( ) const;
HWND m_hWnd_tree =GetSafeHwnd();//【注】此处得到的只是当前窗口(FileTree类)的句柄
HWND m_hWnd = m_pCWnd->GetSafeHwnd();// 这里才是主窗口的句柄(由主窗指针得到主窗的句柄)
//BOOL SetWindowText( HWND hWnd, LPCTSTR lpString )
::SetWindowText(m_hWnd,"ok2222");//修改主窗口标题
::SetDlgItemText(m_hWnd,IDC_STATIC_path2,"HWND: "+m_sCurPath);
【另】AfxGetThread
CWinThread* AfxGetThread( );
Return Value:Pointer to the currently executing thread.
3. 如何在程序中获得其他程序的 图标? AfxGetInstanceHandle()
HINSTANCE AfxGetInstanceHandle( );
Return Value
An HINSTANCE to the current instance of the application. If called from within a DLL linked with the USRDLL version of MFC, an HINSTANCE to the DLL is returned.
Remarks
This function allows you to retrieve the instance handle of the current application. AfxGetInstanceHandle always returns the HINSTANCE of your executable file (.EXE) unless it is called from within a DLL linked with the USRDLL version of MFC. In this case, it returns an HINSTANCE to the DLL.
两种方法:
(1) SDK函数 SHGetFileInfo 或使用 ExtractIcon获得图标资源的 handle(句柄),
(2) SDK函数 SHGetFileInfo获得有关文件的 很多信息,如大小图标,属性,类型等.
Example(1): 在程序窗口左上角显示 NotePad图标.
void CSampleView: OnDraw(CDC * pDC)
{
if( :: SHGetFileInfo(_T("c:\\pwin95\\notepad.exe"),0,
&stFileInfo,sizeof(stFileInfo),SHGFI_ICON))
{
pDC ->DrawIcon(10,10,stFileInfo.hIcon);
}
}
Example(2):同样功能,Use ExtractIcon Function
void CSampleView:: OnDraw(CDC *pDC)
{
HICON hIcon=:: ExtractIcon(AfxGetInstanceHandle(),_T
("NotePad.exe"),0);
if (hIcon &&hIcon!=(HICON)-1)
pDC->DrawIcon(10,10,hIcon);
}
【说明】关于如何得到系统文件的正确路径,象win.ini system32.ini等的路径,各种系统中具体的路径是不一样的。如:
获得notepad.exe的路径正规上来说用GetWindowsDirectory 函数得到;
如果是调用 win95下的画笔,应该用访问注册表的方法获得其路径;
要作成一个比较考究的程序,考虑应该全面点.
【另】
HINSTANCE AfxGetResourceHandle( );
Return Value:An HINSTANCE handle where the default resources of the application are loaded.
4. 有关取得桌面句柄 GetDesktopWindow()
MSDN中的例子:
// 静态函数CWnd:: GetDesktopWindow 返回桌面窗口的指针。下例说明了MFC
void CFrameWnd::BeginModalState ()
{
//first count all windows that need to be disabled
UINT nCount=0;
HWND hWnd=:: GetWindow (:: GetDesktopWindow (), GW_CHILD);
while (hWnd!=NULL)
{
if (:: IsWindowEnabled (hwnd) &&
CWnd::FromHandlePermanent (hWnd)!=NULL &&
AfxIsDescendant (pParent->m_hWnd, hWnd) &&
:: SendMessage (hWnd, WM_DISABLEMODAL, 0, 0) == 0)
{
++nCount;
}
hWnd=:: GetWindow (hWnd, GW_HWNDNEXT);
}
}
//用户的问题:下面程序取的不是同一程序的句柄,但是GetModuleFileName返回的结果一样请问为什莫
HWND ChWnd;//子窗口句柄
HWND hwDesktop=::GetDesktopWindow();//取得桌面句柄
ChWnd=::GetWindow(hwDesktop,GW_CHILD);//取得桌面子句柄
CString csTitle,csClass,csTm,mLookstring;
char szBuffer[255];
while(ChWnd!=NULL)//循环取子句柄的同级句柄
{
if(::IsWindowVisible(ChWnd))//判断是否为可显示窗口
{
::GetWindowText(ChWnd,csTitle.GetBuffer(255),254);
::GetClassName(ChWnd,csClass.GetBuffer(255),254);
csTitle.ReleaseBuffer();//标题
csClass.ReleaseBuffer();//类名
csTm.Format("%08X:",ChWnd);
if(csTitle=="")
{
mLookstring=csTm+csClass;
}else
{
mLookstring=csTm+csTitle;
}
//这里的窗口句柄不是同一个程序吧?(问题所在!)可是为什莫结果一样
HINSTANCE hInstance = (HINSTANCE)::GetWindowLong(ChWnd,DWL_USER);
::GetModuleFileName(hInstance, szBuffer, sizeof(szBuffer));
MessageBox(szBuffer,mLookstring);
}
ChWnd=::GetWindow(ChWnd,GW_HWNDNEXT);
}
回答:
问题在于Win32下GetWindowLong(ChWnd,DWL_USER)总是返回当前程序运行的hInstance,所以你得到的文件名总是一个。所以你要用枚举所有"进程的程序名"来获得程序名。
=== 再谈句柄于指针的区别
因篇幅问题不能全部显示,请点此查看更多更全内容