GetSafeHwnd()

本文详细介绍了 CWnd 类中的 GetSafeHwnd 成员函数,该函数能够安全地获取窗口句柄(HWND)。文章通过示例代码解释了在何种情况下使用此函数可以避免潜在的错误,并说明了当 CWnd 对象未与窗口关联时函数将返回 NULL 的情况。

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

CWnd::GetSafeHwnd  
(这是CWnd的一个成员函数)
HWND GetSafeHwnd()const;
(这是个const 函数)
Return Value
Returns the window handle for a window. Returns NULL if the CWnd is not attached to a window or if it is used with a NULL CWnd pointer. 
(调用它返回窗口的句柄,如果CWnd没有与任何窗口联系或者用于一直空的CWnd指针,它返回NULL)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数,通过下面的例子来看其理由:

  HWND hwnd;
  CWnd* pWnd;
  pWnd=((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
  hwnd=pWnd->GetDlgItem(IDC_EDIT2)->GetSafeHwnd();

  ::SetWindowText(hwnd,m_strResult);

//m_strResult 是变量 IDC_EDIT2是一个编辑框的id

 

原文: http://blog.youkuaiyun.com/wuyuan2011woaini/article/details/7558788

``` m_idTopLeftCell.row = -1; if (!m_bAllowDraw || !::IsWindow(GetSafeHwnd())) return; CRect rect; // This would have caused OnSize event - Brian //EnableScrollBars(SB_BOTH, FALSE); GetClientRect(rect); if (rect.left == rect.right || rect.top == rect.bottom) return; if (IsVisibleVScroll()) rect.right += GetSystemMetrics(SM_CXVSCROLL) + GetSystemMetrics(SM_CXBORDER); if (IsVisibleHScroll()) rect.bottom += GetSystemMetrics(SM_CYHSCROLL) + GetSystemMetrics(SM_CYBORDER); rect.left += GetFixedColumnWidth(); rect.top += GetFixedRowHeight(); if (rect.left >= rect.right || rect.top >= rect.bottom) { EnableScrollBarCtrl(SB_BOTH, FALSE); return; } CRect VisibleRect(GetFixedColumnWidth(), GetFixedRowHeight(), rect.right, rect.bottom); CRect VirtualRect(GetFixedColumnWidth(), GetFixedRowHeight(), GetVirtualWidth(), GetVirtualHeight()); // Removed to fix single row scrollbar problem (Pontus Goffe) // CCellRange visibleCells = GetUnobstructedNonFixedCellRange(); // if (!IsValid(visibleCells)) return; //TRACE(_T("Visible: %d x %d, Virtual %d x %d. H %d, V %d\n"), // VisibleRect.Width(), VisibleRect.Height(), // VirtualRect.Width(), VirtualRect.Height(), // IsVisibleHScroll(), IsVisibleVScroll()); // If vertical scroll bar, horizontal space is reduced if (VisibleRect.Height() < VirtualRect.Height()) VisibleRect.right -= ::GetSystemMetrics(SM_CXVSCROLL); // If horz scroll bar, vert space is reduced if (VisibleRect.Width() < VirtualRect.Width()) VisibleRect.bottom -= ::GetSystemMetrics(SM_CYHSCROLL); // Recheck vertical scroll bar //if (VisibleRect.Height() < VirtualRect.Height()) // VisibleRect.right -= ::GetSystemMetrics(SM_CXVSCROLL); if (VisibleRect.Height() < VirtualRect.Height()) { EnableScrollBars(SB_VERT, TRUE); m_nVScrollMax = VirtualRect.Height() - 1; } else { EnableScrollBars(SB_VERT, FALSE); m_nVScrollMax = 0; } if (VisibleRect.Width() < VirtualRect.Width()) { EnableScrollBars(SB_HORZ, TRUE); m_nHScrollMax = VirtualRect.Width() - 1; } else { EnableScrollBars(SB_HORZ, FALSE); m_nHScrollMax = 0; } ASSERT(m_nVScrollMax < INT_MAX && m_nHScrollMax < INT_MAX); // This should be fine /* Old code - CJM SCROLLINFO si; si.cbSize = sizeof(SCROLLINFO); si.fMask = SIF_PAGE; si.nPage = (m_nHScrollMax>0)? VisibleRect.Width() : 0; SetScrollInfo(SB_HORZ, &si, FALSE); si.nPage = (m_nVScrollMax>0)? VisibleRect.Height() : 0; SetScrollInfo(SB_VERT, &si, FALSE); SetScrollRange(SB_VERT, 0, m_nVScrollMax, TRUE); SetScrollRange(SB_HORZ, 0, m_nHScrollMax, TRUE); */ // New code - Paul Runstedler SCROLLINFO si; si.cbSize = sizeof(SCROLLINFO); si.fMask = SIF_PAGE | SIF_RANGE; si.nPage = (m_nHScrollMax>0)? VisibleRect.Width() : 0; si.nMin = 0; si.nMax = m_nHScrollMax; SetScrollInfo(SB_HORZ, &si, TRUE); si.fMask |= SIF_DISABLENOSCROLL; si.nPage = (m_nVScrollMax>0)? VisibleRect.Height() : 0; si.nMin = 0; si.nMax = m_nVScrollMax; SetScrollInfo(SB_VERT, &si, TRUE);```分析这段代码
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值