📄 nwnd.cpp
字号:
if (hWndNew == NULL)
return FALSE;
m_hWnd = hWndNew;
return TRUE;
}
// Detach the HWND from the CNWnd object
HWND CNWnd::Detach()
{
HWND hWnd = m_hWnd;
// set the related member variables to null.
m_hWnd = NULL;
m_pfnSuper = NULL;
m_pParentWnd = NULL;
return hWnd;
}
/////////////////////////////////////////////////////////////////////////////
// CNWnd will delegate owner draw messages to self drawing controls
void CNWnd::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// reflect notification to child window control
if( ::SendMessage(lpDrawItemStruct->hwndItem, WM_DRAWITEM, (WPARAM)nIDCtl, (LPARAM)lpDrawItemStruct) )
return; // eat it
// not handled - do default
Default(WM_DRAWITEM, (WPARAM)nIDCtl, (LPARAM)lpDrawItemStruct);
}
LRESULT CNWnd::Default(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult;
if( m_pfnSuper == NULL )
lResult = DefWindowProc(this->m_hWnd, uMsg, wParam, lParam);
else
lResult = ::CallWindowProc(m_pfnSuper, this->m_hWnd, uMsg, wParam, lParam);
return lResult;
}
void CNWnd::OnPaint(void)
{
// not handled - do default
Default(WM_PAINT, 0, 0);
}
void CNWnd::OnActivate(UINT nState, HWND hWndOther, BOOL bMinimized )
{
Default(WM_ACTIVATE,MAKELONG(nState,bMinimized), (LPARAM)hWndOther);
}
void CNWnd::OnKeyDown(WPARAM wParam, LPARAM lParam)
{
}
void CNWnd::OnClose()
{
}
void CNWnd::OnDestroy()
{
Detach();
}
BOOL CNWnd::DestroyWindow()
{
ASSERT(m_hWnd != NULL);
HWND hWnd = Detach();
return ::DestroyWindow(hWnd);
}
BOOL CNWnd::OnEraseBkgnd(HDC hdc)
{
CNWnd *pParent = GetParent();
HBRUSH hbr;
TCHAR szClassName[128];
UINT msg;
RECT rect;
// for button and static control used in this app, we
// fill the background with the brush returned by calling
// its parent's OnCtlColor() member function.(don't send WM_CTLCOLORBTN
// and WM_CTLCOLORSTATIC, but call the function directly for better
// performance.)
if ( pParent )
{
GetClassName(GetSafeHwnd(),szClassName,128-1);
_tcsupr(szClassName);
if( _tcscmp(szClassName,_T("BUTTON")) == 0 )
msg = WM_CTLCOLORBTN;
else if( _tcscmp(szClassName,_T("STATIC")) == 0 )
msg = WM_CTLCOLORSTATIC;
else
goto LDefault;
GetClientRect(&rect);
hbr = (HBRUSH)pParent->OnCtlColor(hdc,GetSafeHwnd(),msg);
FillRect(hdc, &rect, hbr);
return TRUE;
}
LDefault:
return Default(WM_ERASEBKGND, (WPARAM)hdc, 0);
}
BOOL CNWnd::OnInitDialog()
{
return TRUE;
}
void CNWnd::OnMouseMove(UINT nFlags, POINT point)
{
Default(WM_MOUSEMOVE, nFlags, MAKELONG(point.x, point.y));
}
void CNWnd::OnLButtonDown(UINT nFlags, POINT point)
{
Default(WM_LBUTTONDOWN, nFlags, MAKELONG(point.x, point.y));
}
void CNWnd::OnLButtonUp(UINT nFlags, POINT point)
{
Default(WM_LBUTTONUP, nFlags, MAKELONG(point.x, point.y));
}
void CNWnd::OnLButtonDblClk(UINT nFlags, POINT point)
{
Default(WM_LBUTTONDBLCLK, nFlags, MAKELONG(point.x, point.y));
}
void CNWnd::OnRButtonDown(UINT nFlags, POINT point)
{
Default(WM_RBUTTONDOWN, nFlags, MAKELONG(point.x, point.y));
}
void CNWnd::OnRButtonUp(UINT nFlags, POINT point)
{
Default(WM_RBUTTONUP, nFlags, MAKELONG(point.x, point.y));
}
HBRUSH CNWnd::OnCtlColor(HDC hdc,HWND hwnd, UINT nCtlColor)
{
return (HBRUSH)Default(nCtlColor, (WPARAM)hdc, (LPARAM)hwnd);
}
HWND CNWnd::GetSafeHwnd()
{
ASSERT(::IsWindow(m_hWnd));
return m_hWnd;
}
BOOL CNWnd::UpdateWindow(void) const
{
ASSERT(::IsWindow(m_hWnd));
BOOL bRet = ::UpdateWindow(m_hWnd);
CNDialog *pDlg = (CNDialog*)NGetActiveWindow();
if(pDlg && bRet)
pDlg->OnIdle();
return bRet;//::UpdateWindow(m_hWnd);
}
void CNWnd::InvalidateRect( LPCRECT lpRect, BOOL bErase /* = TRUE*/)
{
ASSERT(::IsWindow(m_hWnd));
::InvalidateRect(m_hWnd, lpRect, bErase);
}
HDC CNWnd::GetWindowDC() const
{
ASSERT(::IsWindow(m_hWnd));
return ::GetWindowDC(m_hWnd);
}
int CNWnd::ReleaseDC(HDC hdc) const
{
ASSERT(::IsWindow(m_hWnd));
return ::ReleaseDC(m_hWnd, hdc);
}
HDC CNWnd::GetDC() const
{
ASSERT(::IsWindow(m_hWnd));
return ::GetDC(m_hWnd);
}
LRESULT CNWnd::SendMessage(UINT Msg,WPARAM wParam,LPARAM lParam)
{
ASSERT(::IsWindow(m_hWnd));
return ::SendMessage(m_hWnd, Msg, wParam, lParam);
}
LRESULT CNWnd::SendMessage(UINT Msg,WPARAM wParam,LPARAM lParam) const
{
ASSERT(::IsWindow(m_hWnd));
return ::SendMessage(m_hWnd, Msg, wParam, lParam);
}
LRESULT CNWnd::PostMessage(UINT Msg,WPARAM wParam,LPARAM lParam)
{
ASSERT(::IsWindow(m_hWnd));
return ::PostMessage(m_hWnd, Msg, wParam, lParam);
}
LRESULT CNWnd::PostMessage(UINT Msg,WPARAM wParam,LPARAM lParam) const
{
ASSERT(::IsWindow(m_hWnd));
return ::PostMessage(m_hWnd, Msg, wParam, lParam);
}
BOOL CNWnd::GetClientRect(RECT *pRect) const
{
ASSERT(::IsWindow(m_hWnd));
::GetClientRect(m_hWnd, pRect);
DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE);
// if it's a child window, convert it on parent window's coordiantes
if( (WS_CHILD & dwStyle) )
{
POINT pt = {0,0};
::ClientToScreen(m_hWnd, &pt);
::ScreenToClient(GetParent()->GetSafeHwnd(), &pt);
::OffsetRect(pRect, pt.x, pt.y);
}
return TRUE;
}
BOOL CNWnd::GetWindowRect(RECT *pRect) const
{
ASSERT(::IsWindow(m_hWnd));
return ::GetWindowRect(m_hWnd, pRect);
}
WNDPROC CNWnd::NavGetNavWndProc()
{
return &WindowProc;
}
BOOL CNWnd::IsWindowEnabled() const
{
ASSERT(::IsWindow(m_hWnd));
return ::IsWindowEnabled(m_hWnd);
}
BOOL CNWnd::EnableWindow(BOOL bEnable /*= TRUE*/)
{
ASSERT(::IsWindow(m_hWnd));
return ::EnableWindow(m_hWnd, bEnable);
}
BOOL CNWnd::ShowWindow(int nCmdShow) const
{
// do not call this function.
ASSERT(FALSE);
ASSERT(::IsWindow(m_hWnd));
return ::ShowWindow(m_hWnd, nCmdShow);
}
BOOL CNWnd::ShowWindow( int nCmdShow )
{
ASSERT(::IsWindow(m_hWnd));
CNDialog *pParentDlg = NULL;
DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE);
// if it's a child window, reset it's parent window's
// clip region.
if( WS_CHILD & dwStyle)
{
// pls ensure to pass only SW_SHOW or SW_HIDE to ShowWindow()
// functions for child windows.
ASSERT(nCmdShow == SW_SHOW || nCmdShow == SW_HIDE);
// intend to show it and its previous state is invisible
// or to hide it and its previous state is visible,
// then reset parent window's clip region.
pParentDlg = (CNDialog*)GetParent();
if( (nCmdShow == SW_SHOW && !(WS_VISIBLE & dwStyle)) )
pParentDlg->ResetChildRgn(this,TRUE);
else if( (nCmdShow == SW_HIDE && (WS_VISIBLE & dwStyle)) )
pParentDlg->ResetChildRgn(this,FALSE);
else
{
// Nothing to do. No need to reset child region.
}
}
return ::ShowWindow(m_hWnd, nCmdShow);
}
BOOL CNWnd::IsWindowVisible() const
{
ASSERT(::IsWindow(m_hWnd));
return ::IsWindowVisible(m_hWnd);
}
BOOL CNWnd::SetWindowPos(HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags )
{
ASSERT(::IsWindow(m_hWnd));
return ::SetWindowPos(m_hWnd, hWndInsertAfter,
x, y, cx, cy, nFlags );
}
UINT CNWnd::SetTimer(UINT nIDEvent, UINT nElapse,
void (CALLBACK* lpfnTimer)(HWND, UINT, UINT, DWORD))
{
ASSERT(::IsWindow(m_hWnd));
return ::SetTimer(m_hWnd, nIDEvent, nElapse, lpfnTimer);
}
BOOL CNWnd::KillTimer(UINT nIDEvent)
{
ASSERT(::IsWindow(m_hWnd));
return ::KillTimer(m_hWnd, nIDEvent);
}
void CNWnd::OnTimer(UINT nIDEvent)
{
}
void CNWnd::SetWindowText(LPCTSTR lpszString)
{
ASSERT(::IsWindow(m_hWnd));
::SetWindowText(m_hWnd, lpszString);
}
int CNWnd::GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const
{
ASSERT(::IsWindow(m_hWnd));
ASSERT(nMaxCount>0);
int nLen = ::GetWindowText(m_hWnd, lpszStringBuf, nMaxCount);
// The following code fragment is for compatible with WinCE.
// when we call the previous function to retrieve window text on
// a image static control, it will return the image resource ID,
// and first byte in the buffer will be set to 0xFF.
if( *((BYTE*) lpszStringBuf) == (BYTE)-1 )
{
lpszStringBuf[0] = (TCHAR)0;
nLen = 0;
}
return nLen;
}
int CNWnd::GetWindowText(CNString& rString) const
{
ASSERT(::IsWindow(m_hWnd));
const int BUFFERSIZE = 128;
TCHAR szBuf[BUFFERSIZE*sizeof(TCHAR)];
TCHAR *pBuf = NULL;
int nLen = GetWindowTextLength();
memset(szBuf, 0, sizeof(szBuf));
// try to store window text in array to avod dynamic memory allocation,
// if it's too large, alloc another memory block to hold it.
if( nLen < BUFFERSIZE )
{
nLen = GetWindowText(szBuf, BUFFERSIZE);
rString = szBuf;
}
else // too large to fit into the buffer array.
{
pBuf = (TCHAR*)malloc((nLen+1)*sizeof(TCHAR));
ASSERT( pBuf != NULL );
nLen = GetWindowText(pBuf, nLen+1);
rString = pBuf;
free(pBuf);
}
return nLen;
}
int CNWnd::GetWindowTextLength() const
{
ASSERT(::IsWindow(m_hWnd));
return ::GetWindowTextLength(m_hWnd);
}
void CNWnd::SetFont(HFONT hFont, BOOL bRedraw/* = TRUE*/)
{
ASSERT(::IsWindow(m_hWnd));
SendMessage(WM_SETFONT, (WPARAM)hFont, 0);
if ( bRedraw )
{
InvalidateRect(NULL);
}
return;
}
HFONT CNWnd::GetFont()
{
ASSERT(::IsWindow(m_hWnd));
return (HFONT)SendMessage(WM_GETFONT, 0, 0);
}
CNWnd* CNWnd::GetParent() const
{
ASSERT(::IsWindow(m_hWnd));
return m_pParentWnd;
}
CNWnd* CNWnd::SetParent(CNWnd* pWndNewParent)
{
ASSERT(::IsWindow(m_hWnd));
ASSERT(pWndNewParent != NULL);
ASSERT(::IsWindow(pWndNewParent->m_hWnd));
// it must be a child window.
ASSERT((::GetWindowLong(m_hWnd, GWL_STYLE) & WS_CHILD) );
m_pParentWnd = pWndNewParent;
::SetParent(m_hWnd, pWndNewParent->m_hWnd);
// the previous parent window need to be returned,
// improve it later.
return NULL;
}
HRGN CNWnd::GetUpdateRgn()
{
static HRGN s_hrgn = NULL;
RECT rect;
GetClientRect(&rect);
// RECT rcUpdate;
// GetUpdateRect(GetSafeHwnd(), &rcUpdate, FALSE);
// OffsetRect(&rcUpdate, rect.left, rect.top);
// SetRect(&rcUpdate,0,0,0,0);
if(s_hrgn == NULL)
s_hrgn = CreateRectRgn(rect.left, rect.top, rect.right, rect.bottom);
else
SetRectRgn(s_hrgn,rect.left, rect.top, rect.right, rect.bottom);
// return s_hrgn;
if( WS_CHILD & GetWindowLong(GetSafeHwnd(), GWL_STYLE) )
{
if( GetParent() == NGetActiveWindow() )
return s_hrgn;
}
return g_hRgnNULL;
}
//Definitions for the CNCtrl class
BOOL CNCtrl::Create(LPCTSTR lpszText, DWORD dwStyle,
const RECT& rect, CNWnd* pParentWnd, UINT nID)
{
CNWnd* pWnd = this;
return pWnd->Create(_T("NAVCTRL"), lpszText, dwStyle, rect, pParentWnd, nID);
}
void CNCtrl::OnPaint()
{
PAINTSTRUCT ps;
if( IsMemWindow() )
{
BeginPaint(GetSafeHwnd(), &ps);
// draw in memDC
HDC hdc = g_hdcUI;
HFONT hOldFont = (HFONT)::SelectObject(hdc, GetFont());
HRGN hOldRgn = (HRGN)::SelectObject(hdc, GetUpdateRgn());
OnDraw(hdc);
// restore GDI objects
SelectObject(hdc, hOldFont);
SelectObject(hdc, hOldRgn);
EndPaint(GetSafeHwnd(), &ps);
}
else
{
CNWnd::OnPaint();
}
}
void CNCtrl::OnDraw(HDC hdc)
{
// all controls in this application will be drawed by
// our own, so it will never send WM_PAINT to Default window
// procedure.
// In this framework, there is no way to call CNWnd::OnPaint(),
// if derived class doesn't overrite this function, we draw
// nothing, but return.
return;
}
/*
//Definitions for the CNApp class
//Function which returns a pointer to the CNApp object
CNApp* GetApp(){ return CNApp::GetApp(); }
//Static variable for the pointer to the CNApp object
CNApp* CNApp::sm_pTheApp = 0;
//To begin the framework inherit your application class from this one.
//Ensure you run only one instance of the class inherited from this.
CNApp::CNApp(HINSTANCE hInstance) : m_hInstance(hInstance)
{
//Test if this is the first instance of CNApp
if (GetApp() == 0)
{
sm_pTheApp = this;
}
else
{
//We get here if the framework is used incorrectly, as more than one instance
// of a CNApp derived class is started.
::MessageBox(NULL,_T("Error! An instance of CNApp (or a class derrived from CNApp) is already running"),_T("Error"),MB_OK);
}
}
CNApp::~CNApp()
{
sm_pTheApp = 0;
}
int CNApp::MessageLoop()
{
MSG msg;
int status;
while ( (status = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if ( status != -1 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -