📄 atlwin.h
字号:
BOOL IsDialogMessage(LPMSG lpMsg)
{
ATLASSERT(::IsWindow(m_hWnd));
return ::IsDialogMessage(m_hWnd, lpMsg);
}
void NextDlgCtrl() const
{
ATLASSERT(::IsWindow(m_hWnd));
::SendMessage(m_hWnd, WM_NEXTDLGCTL, 0, 0L);
}
void PrevDlgCtrl() const
{
ATLASSERT(::IsWindow(m_hWnd));
::SendMessage(m_hWnd, WM_NEXTDLGCTL, 1, 0L);
}
void GotoDlgCtrl(HWND hWndCtrl) const
{
ATLASSERT(::IsWindow(m_hWnd));
::SendMessage(m_hWnd, WM_NEXTDLGCTL, (WPARAM)hWndCtrl, 1L);
}
BOOL ResizeClient(int nWidth, int nHeight, BOOL bRedraw = TRUE)
{
ATLASSERT(::IsWindow(m_hWnd));
RECT rcWnd;
if(!GetClientRect(&rcWnd))
return FALSE;
if(nWidth != -1)
rcWnd.right = nWidth;
if(nHeight != -1)
rcWnd.bottom = nHeight;
if(!::AdjustWindowRectEx(&rcWnd, GetStyle(), (!(GetStyle() & WS_CHILD) && (GetMenu() != NULL)), GetExStyle()))
return FALSE;
UINT uFlags = SWP_NOZORDER | SWP_NOMOVE;
if(!bRedraw)
uFlags |= SWP_NOREDRAW;
return SetWindowPos(NULL, 0, 0, rcWnd.right - rcWnd.left, rcWnd.bottom - rcWnd.top, uFlags);
}
int GetWindowRgn(HRGN hRgn)
{
ATLASSERT(::IsWindow(m_hWnd));
return ::GetWindowRgn(m_hWnd, hRgn);
}
int SetWindowRgn(HRGN hRgn, BOOL bRedraw = FALSE)
{
ATLASSERT(::IsWindow(m_hWnd));
return ::SetWindowRgn(m_hWnd, hRgn, bRedraw);
}
HDWP DeferWindowPos(HDWP hWinPosInfo, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags)
{
ATLASSERT(::IsWindow(m_hWnd));
return ::DeferWindowPos(hWinPosInfo, m_hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
}
DWORD GetWindowThreadID()
{
ATLASSERT(::IsWindow(m_hWnd));
return ::GetWindowThreadProcessId(m_hWnd, NULL);
}
DWORD GetWindowProcessID()
{
ATLASSERT(::IsWindow(m_hWnd));
DWORD dwProcessID;
::GetWindowThreadProcessId(m_hWnd, &dwProcessID);
return dwProcessID;
}
BOOL IsWindow()
{
return ::IsWindow(m_hWnd);
}
BOOL IsWindowUnicode()
{
ATLASSERT(::IsWindow(m_hWnd));
return ::IsWindowUnicode(m_hWnd);
}
BOOL IsParentDialog()
{
ATLASSERT(::IsWindow(m_hWnd));
TCHAR szBuf[8]; // "#32770" + NUL character
GetClassName(GetParent(), szBuf, sizeof(szBuf)/sizeof(TCHAR));
return lstrcmp(szBuf, _T("#32770")) == 0;
}
BOOL ShowWindowAsync(int nCmdShow)
{
ATLASSERT(::IsWindow(m_hWnd));
return ::ShowWindowAsync(m_hWnd, nCmdShow);
}
HWND GetDescendantWindow(int nID) const
{
ATLASSERT(::IsWindow(m_hWnd));
// GetDlgItem recursive (return first found)
// breadth-first for 1 level, then depth-first for next level
// use GetDlgItem since it is a fast USER function
HWND hWndChild, hWndTmp;
CWindow wnd;
if((hWndChild = ::GetDlgItem(m_hWnd, nID)) != NULL)
{
if(::GetTopWindow(hWndChild) != NULL)
{
// children with the same ID as their parent have priority
wnd.Attach(hWndChild);
hWndTmp = wnd.GetDescendantWindow(nID);
if(hWndTmp != NULL)
return hWndTmp;
}
return hWndChild;
}
// walk each child
for(hWndChild = ::GetTopWindow(m_hWnd); hWndChild != NULL;
hWndChild = ::GetNextWindow(hWndChild, GW_HWNDNEXT))
{
wnd.Attach(hWndChild);
hWndTmp = wnd.GetDescendantWindow(nID);
if(hWndTmp != NULL)
return hWndTmp;
}
return NULL; // not found
}
void SendMessageToDescendants(UINT message, WPARAM wParam = 0, LPARAM lParam = 0, BOOL bDeep = TRUE)
{
CWindow wnd;
for(HWND hWndChild = ::GetTopWindow(m_hWnd); hWndChild != NULL;
hWndChild = ::GetNextWindow(hWndChild, GW_HWNDNEXT))
{
::SendMessage(hWndChild, message, wParam, lParam);
if(bDeep && ::GetTopWindow(hWndChild) != NULL)
{
// send to child windows after parent
wnd.Attach(hWndChild);
wnd.SendMessageToDescendants(message, wParam, lParam, bDeep);
}
}
}
BOOL CenterWindow(HWND hWndCenter = NULL)
{
ATLASSERT(::IsWindow(m_hWnd));
// determine owner window to center against
DWORD dwStyle = GetStyle();
if(hWndCenter == NULL)
{
if(dwStyle & WS_CHILD)
hWndCenter = ::GetParent(m_hWnd);
else
hWndCenter = ::GetWindow(m_hWnd, GW_OWNER);
}
// get coordinates of the window relative to its parent
RECT rcDlg;
::GetWindowRect(m_hWnd, &rcDlg);
RECT rcArea;
RECT rcCenter;
HWND hWndParent;
if(!(dwStyle & WS_CHILD))
{
// don't center against invisible or minimized windows
if(hWndCenter != NULL)
{
DWORD dwStyle = ::GetWindowLong(hWndCenter, GWL_STYLE);
if(!(dwStyle & WS_VISIBLE) || (dwStyle & WS_MINIMIZE))
hWndCenter = NULL;
}
// center within screen coordinates
::SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcArea, NULL);
if(hWndCenter == NULL)
rcCenter = rcArea;
else
::GetWindowRect(hWndCenter, &rcCenter);
}
else
{
// center within parent client coordinates
hWndParent = ::GetParent(m_hWnd);
ATLASSERT(::IsWindow(hWndParent));
::GetClientRect(hWndParent, &rcArea);
ATLASSERT(::IsWindow(hWndCenter));
::GetClientRect(hWndCenter, &rcCenter);
::MapWindowPoints(hWndCenter, hWndParent, (POINT*)&rcCenter, 2);
}
int DlgWidth = rcDlg.right - rcDlg.left;
int DlgHeight = rcDlg.bottom - rcDlg.top;
// find dialog's upper left based on rcCenter
int xLeft = (rcCenter.left + rcCenter.right) / 2 - DlgWidth / 2;
int yTop = (rcCenter.top + rcCenter.bottom) / 2 - DlgHeight / 2;
// if the dialog is outside the screen, move it inside
if(xLeft < rcArea.left)
xLeft = rcArea.left;
else if(xLeft + DlgWidth > rcArea.right)
xLeft = rcArea.right - DlgWidth;
if(yTop < rcArea.top)
yTop = rcArea.top;
else if(yTop + DlgHeight > rcArea.bottom)
yTop = rcArea.bottom - DlgHeight;
// map screen coordinates to child coordinates
return ::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
BOOL ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0)
{
ATLASSERT(::IsWindow(m_hWnd));
DWORD dwStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
if(dwStyle == dwNewStyle)
return FALSE;
::SetWindowLong(m_hWnd, GWL_STYLE, dwNewStyle);
if(nFlags != 0)
{
::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | nFlags);
}
return TRUE;
}
BOOL ModifyStyleEx(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0)
{
ATLASSERT(::IsWindow(m_hWnd));
DWORD dwStyle = ::GetWindowLong(m_hWnd, GWL_EXSTYLE);
DWORD dwNewStyle = (dwStyle & ~dwRemove) | dwAdd;
if(dwStyle == dwNewStyle)
return FALSE;
::SetWindowLong(m_hWnd, GWL_EXSTYLE, dwNewStyle);
if(nFlags != 0)
{
::SetWindowPos(m_hWnd, NULL, 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | nFlags);
}
return TRUE;
}
BOOL GetWindowText(BSTR* pbstrText)
{
return GetWindowText(*pbstrText);
}
BOOL GetWindowText(BSTR& bstrText)
{
USES_CONVERSION;
ATLASSERT(::IsWindow(m_hWnd));
if (bstrText != NULL)
{
SysFreeString(bstrText);
bstrText = NULL;
}
int nLen = ::GetWindowTextLength(m_hWnd);
if(nLen == 0)
{
bstrText = ::SysAllocString(OLESTR(""));
return (bstrText != NULL) ? TRUE : FALSE;
}
LPTSTR lpszText = (LPTSTR)_alloca((nLen+1)*sizeof(TCHAR));
if(!::GetWindowText(m_hWnd, lpszText, nLen+1))
return FALSE;
bstrText = ::SysAllocString(T2OLE(lpszText));
return (bstrText != NULL) ? TRUE : FALSE;
}
HWND GetTopLevelParent() const
{
ATLASSERT(::IsWindow(m_hWnd));
HWND hWndParent = m_hWnd;
HWND hWndTmp;
while((hWndTmp = ::GetParent(hWndParent)) != NULL)
hWndParent = hWndTmp;
return hWndParent;
}
HWND GetTopLevelWindow() const
{
ATLASSERT(::IsWindow(m_hWnd));
HWND hWndParent;
HWND hWndTmp = m_hWnd;
do
{
hWndParent = hWndTmp;
hWndTmp = (::GetWindowLong(hWndParent, GWL_STYLE) & WS_CHILD) ? ::GetParent(hWndParent) : ::GetWindow(hWndParent, GW_OWNER);
}
while(hWndTmp != NULL);
return hWndParent;
}
};
_declspec(selectany) RECT CWindow::rcDefault = { CW_USEDEFAULT, CW_USEDEFAULT, 0, 0 };
/////////////////////////////////////////////////////////////////////////////
// CAxWindow - client side for an ActiveX host window
#ifndef _ATL_NO_HOSTING
template <class TBase = CWindow>
class CAxWindowT : public TBase
{
public:
// Constructors
CAxWindowT(HWND hWnd = NULL) : TBase(hWnd)
{ }
CAxWindowT< TBase >& operator=(HWND hWnd)
{
m_hWnd = hWnd;
return *this;
}
// Attributes
static LPCTSTR GetWndClassName()
{
return _T("AtlAxWin");
}
// Operations
HWND Create(HWND hWndParent, RECT& rcPos, LPCTSTR szWindowName = NULL,
DWORD dwStyle = 0, DWORD dwExStyle = 0,
UINT nID = 0, LPVOID lpCreateParam = NULL)
{
return CWindow::Create(GetWndClassName(), hWndParent, rcPos, szWindowName, dwStyle, dwExStyle, nID, lpCreateParam);
}
HWND Create(HWND hWndParent, LPRECT lpRect = NULL, LPCTSTR szWindowName = NULL,
DWORD dwStyle = 0, DWORD dwExStyle = 0,
HMENU hMenu = NULL, LPVOID lpCreateParam = NULL)
{
return CWindow::Create(GetWndClassName(), hWndParent, lpRect, szWindowName, dwStyle, dwExStyle, hMenu, lpCreateParam);
}
HRESULT CreateControl(LPCOLESTR lpszName, IStream* pStream = NULL, IUnknown** ppUnkContainer = NULL)
{
ATLASSERT(::IsWindow(m_hWnd));
return AtlAxCreateControl(lpszName, m_hWnd, pStream, ppUnkContainer);
}
HRESULT CreateControl(DWORD dwResID, IStream* pStream = NULL, IUnknown** ppUnkContainer = NULL)
{
TCHAR szModule[_MAX_PATH];
GetModuleFileName(_Module.GetModuleInstance(), szModule, _MAX_PATH);
CComBSTR bstrURL(OLESTR("res://"));
bstrURL.Append(szModule);
bstrURL.Append(OLESTR("/"));
TCHAR szResID[11];
wsprintf(szResID, _T("%0d"), dwResID);
bstrURL.Append(szResID);
ATLASSERT(::IsWindow(m_hWnd));
return AtlAxCreateControl(bstrURL, m_hWnd, pStream, ppUnkContainer);
}
HRESULT CreateControlEx(LPCOLESTR lpszName, IStream* pStream = NULL,
IUnknown** ppUnkContainer = NULL, IUnknown** ppUnkControl = NULL,
REFIID iidSink = IID_NULL, IUnknown* punkSink = NULL)
{
ATLASSERT(::IsWindow(m_hWnd));
return AtlAxCreateControlEx(lpszName, m_hWnd, pStream, ppUnkContainer, ppUnkControl, iidSink, punkSink);
}
HRESULT CreateControlEx(DWORD dwResID, IStream* pStream = NULL,
IUnknown** ppUnkContainer = NULL, IUnknown** ppUnkControl = NULL,
REFIID iidSink = IID_NULL, IUnknown* punkSink = NULL)
{
TCHAR szModule[_MAX_PATH];
GetModuleFileName(_Module.GetModuleInstance(), szModule, _MAX_PATH);
CComBSTR bstrURL(OLESTR("res://"));
bstrURL.Append(szModule);
bstrURL.Append(OLESTR("/"));
TCHAR szResID[11];
wsprintf(szResID, _T("%0d"), dwResID);
bstrURL.Append(szResID);
ATLASSERT(::IsWindow(m_hWnd));
return AtlAxCreateControlEx(bstrURL, m_hWnd, pStream, ppUnkContainer, ppUnkControl, iidSink, punkSink);
}
HRESULT AttachControl(IUnknown* pControl, IUnknown** ppUnkContainer)
{
ATLASSERT(::IsWindow(m_hWnd));
return AtlAxAttachControl(pControl, m_hWnd, ppUnkContainer);
}
HRESULT QueryHost(REFIID iid, void** ppUnk)
{
ATLASSERT(ppUnk != NULL);
HRESULT hr;
*ppUnk = NULL;
CComPtr<IUnknown> spUnk;
hr = AtlAxGetHost(m_hWnd, &spUnk);
if (SUCCEEDED(hr))
hr = spUnk->QueryInterface(iid, ppUnk);
return hr;
}
template <class Q>
HRESULT QueryHost(Q** ppUnk)
{
return QueryHost(__uuidof(Q), (void**)ppUnk);
}
HRESULT QueryControl(REFIID iid, void** ppUnk)
{
ATLASSERT(ppUnk != NULL);
HRESULT hr;
*ppUnk = NULL;
CComPtr<IUnknown> spUnk;
hr = AtlAxGetControl(m_hWnd, &spUnk);
if (SUCCEEDED(hr))
hr = spUnk->QueryInterface(iid, ppUnk);
return hr;
}
template <class Q>
HRESULT QueryControl(Q** ppUnk)
{
return QueryControl(__uuidof(Q), (void**)ppUnk);
}
HRESULT SetExternalDispatch(IDispatch* pDisp)
{
HRESULT hr;
CComPtr<IAxWinHostWindow> spHost;
hr = QueryHost(IID_IAxWinHostWindow, (void**)&spHost);
if (SUCCEEDED(hr))
hr = spHost->SetExternalDispatch(pDisp);
return hr;
}
HRESULT SetExternalUIHandler(IDocHostUIHandlerDispatch* pUIHandler)
{
HRESULT hr;
CComPtr<IAxWinHostWindow> spHost;
hr = QueryHost(IID_IAxWinHostWindow, (void**)&spHost);
if (SUCCEEDED(hr))
hr = spHost->SetExternalUIHandler(pUIHandler);
return hr;
}
};
typedef CAxWindowT<CWindow> CAxWindow;
#endif //_ATL_NO_HOSTING
/////////////////////////////////////////////////////////////////////////////
// WindowProc thunks
#if defined(_M_IX86)
#pragma pack(push,1)
struct _WndProcThunk
{
DWORD m_mov; // mov dword ptr [esp+0x4], pThis (esp+0x4 is hWnd)
DWORD m_this; //
BYTE m_jmp; // jmp WndProc
DWORD m_relproc; // relative jmp
};
#pragma pack(pop)
#elif defined (_M_ALPHA)
// For ALPHA we will stick the this pointer into a0, which is where
// the HWND is. However, we don't actually need the HWND so this is OK.
#pragma pack(push,4)
struct _WndProcThunk //this should come out to 20 bytes
{
DWORD ldah_at; // ldah at, HIWORD(func)
DWORD ldah_a0; // ldah a0, HIWORD(this)
DWORD lda_at; // lda at, LOWORD(func)(at)
DWORD lda_a0; // lda a0, LOWORD(this)(a0)
DWORD jmp; // jmp zero,(at),0
};
#pragma pack(pop)
#else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -