📄 statusbarmsgwnd.cpp
字号:
m_fontMessageUnderline.CreatePointFontIndirect(&lf);
// Prepare an non undelined font
lf.lfUnderline = FALSE;
m_fontMessageNoUnderline.CreatePointFontIndirect(&lf);
// Initialize the cursor.
m_hCursor = ::LoadCursor(NULL, IDC_ARROW); // Use IDC_HAND if it compiles
return 0;
}
void CStatusBarMsgWnd::OnMouseMove(UINT nFlags, CPoint point)
{
// Register the tracking of Mouse entering and leaveing the window
// for WM_MOUSEHOVER and WM_MOUSELEAVE
TRACKMOUSEEVENT t_MouseEvent;
t_MouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
t_MouseEvent.dwFlags = TME_LEAVE | TME_HOVER;
t_MouseEvent.hwndTrack = m_hWnd;
t_MouseEvent.dwHoverTime = 1;
::_TrackMouseEvent(&t_MouseEvent);
}
void CStatusBarMsgWnd::OnTimer(UINT nIDEvent)
{
switch (nIDEvent)
{
case IDT_POP_WINDOW_TIMER: // When the window comes up
{
switch (m_nStatusBarPos)
{
case STP_BOTTOM:
case STP_RIGHT:
{
++m_nWndSize;
if (m_nWndSize > m_nWndHeight)
{
KillTimer(IDT_POP_WINDOW_TIMER);
SetTimer(IDT_SHOW_WINDOW_TIMER, m_nMsgTimeOut, NULL);
}
else
{
// Keep sizing the window, show it
SetWindowPos(
&wndTopMost,
m_nWndLeft,
m_nWndBottom - m_nWndSize -5,
m_nWndWidth - 10,
m_nWndSize,
SWP_SHOWWINDOW
);
}
}
break;
case STP_TOP:
{
++m_nWndSize;
if (m_nWndSize > m_nWndHeight)
{
KillTimer(IDT_POP_WINDOW_TIMER);
SetTimer(IDT_SHOW_WINDOW_TIMER, m_nMsgTimeOut, NULL);
}
else
{
// Keep sizing the window, collapse it
SetWindowPos(
&wndTopMost,
m_nWndLeft,
m_nWndTop,
m_nWndWidth - 10,
m_nWndSize,
SWP_SHOWWINDOW
);
}
}
break;
case STP_LEFT:
{
++m_nWndSize;
if (m_nWndSize > m_nWndHeight)
{
KillTimer(IDT_POP_WINDOW_TIMER);
SetTimer(IDT_SHOW_WINDOW_TIMER, m_nMsgTimeOut, NULL);
}
else
{
// Keep sizing the window, collpase it
SetWindowPos(
&wndTopMost,
m_nWndLeft + 10,
m_nWndBottom - m_nWndSize - 5,
m_nWndWidth,
m_nWndSize,
SWP_SHOWWINDOW
);
}
}
break;
}
}
break;
case IDT_SHOW_WINDOW_TIMER:
{
KillTimer(IDT_SHOW_WINDOW_TIMER);
SetTimer(IDT_COLLAPSE_WINDOW_TIMER, m_nMsgWndCreationDelay, NULL);
}
break;
case IDT_COLLAPSE_WINDOW_TIMER:
{
switch (m_nStatusBarPos)
{
case STP_BOTTOM:
case STP_RIGHT:
{
--m_nWndSize;
if (m_nWndSize <= 0)
{
KillTimer(IDT_COLLAPSE_WINDOW_TIMER);
m_nWndSize = 0;
delete this;
}
else
{
// Keep showing the window, collapse it
SetWindowPos(
&wndTopMost,
m_nWndLeft,
m_nWndBottom - m_nWndSize - 5,
m_nWndWidth - 10,
m_nWndSize,
SWP_SHOWWINDOW
);
}
}
break;
case STP_TOP:
{
--m_nWndSize;
if (m_nWndSize <= 0)
{
KillTimer(IDT_COLLAPSE_WINDOW_TIMER);
m_nWndSize = 0;
delete this;
}
else
{
SetWindowPos(
&wndTopMost,
m_nWndLeft,
m_nWndTop,
m_nWndWidth - 10,
m_nWndSize,
SWP_SHOWWINDOW
);
}
}
break;
case STP_LEFT:
{
--m_nWndSize;
if (m_nWndSize <= 0)
{
KillTimer(IDT_COLLAPSE_WINDOW_TIMER);
m_nWndSize = 0;
delete this;
}
else
{
SetWindowPos(
&wndTopMost,
m_nWndLeft + 10,
m_nWndBottom - m_nWndSize - 5,
m_nWndWidth,
m_nWndSize,
SWP_SHOWWINDOW
);
}
}
break;
}
}
break;
}
}
LRESULT CStatusBarMsgWnd::OnMouseHover(WPARAM w, LPARAM l)
{
if (m_bMouseOverWnd == FALSE) // Mouse was not on window
{
CClientDC dc(this);
CFont* pFont = dc.SelectObject(&m_fontMessageUnderline);
// Show with the message with the new font
dc.DrawText(m_strMsg, &m_rectMsgRect, DT_WORDBREAK | DT_CENTER);
dc.SelectObject(pFont); // restore the DC to its original state
m_bMouseOverWnd = TRUE; // Mouse is now over window
}
return 0;
}
LRESULT CStatusBarMsgWnd::OnMouseLeave(WPARAM w, LPARAM l)
{
if (m_bMouseOverWnd) // Mouse was over window, now it is leaving
{
CClientDC dc(this);
CFont* pFont = dc.SelectObject(&m_fontMessageNoUnderline);
// Show with the message with the new font
dc.DrawText(m_strMsg, &m_rectMsgRect, DT_WORDBREAK | DT_CENTER);
dc.SelectObject(pFont); // Restore DC back to its original state
m_bMouseOverWnd = FALSE; // Mouse is not over window
}
return 0;
}
BOOL CStatusBarMsgWnd::OnSetCursor(CWnd* pWnd , UINT nHitTest , UINT message)
{
if (nHitTest == HTCLIENT)
{
::SetCursor(m_hCursor); // Set cursor to HAND type when mouse is over window
return TRUE;
}
return CFrameWnd::OnSetCursor (pWnd, nHitTest, message);
}
void CStatusBarMsgWnd::OnDestroy(void)
{
::CloseHandle(m_hCursor); // Free the cursor as it is a shared resource..
CFrameWnd::OnDestroy();
}
void CStatusBarMsgWnd::OnPaint()
{
CFont* pFont = NULL;
CPaintDC dc(this);
COLORREF clrOld = dc.GetTextColor();
if (m_bMouseOverWnd)
{
pFont = dc.SelectObject(&m_fontMessageUnderline);
dc.SetTextColor(RGB(255,0,0));
}
else
{
pFont = dc.SelectObject(&m_fontMessageNoUnderline);
}
// Show with the message with the new font
dc.DrawText(m_strMsg, &m_rectMsgRect, DT_WORDBREAK | DT_CENTER);
dc.SelectObject(pFont); // restore the DC to its original state
dc.SetTextColor(clrOld);
}
void CStatusBarMsgWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
::AfxMessageBox(_T("您已接受消息了!"));
}
/*-----------------------------------------------------------------------------
Function : CStatusBarMsgWnd::CreateObject()
Abstract : Creates an CStatusBarMsgWnd, constructor is not public because we
want to force heap creation for better response for the
parent window. So we use SetTimer in PopWnd() and return.
So no blocking with Sleep() API.
Parameters :
1. strMsg -> Message to be shown in the window
2. nWndWidth -> Width of the message window
3. nWndHeight -> Height if the message window
4. nMsgTimeOut -> Seconds the window remains stationary
5. nMsgWndCreationDelay -> Seconds in which the window gets shown
4. pParent -> Pointer to the parent window
5 rectMsgRect -> Rectangle in the window where the message will be
Return Value : none
Exceptions : none
Revisions : none
----------------------------------------------------------------------------*/
CStatusBarMsgWnd* CStatusBarMsgWnd::CreateObject(CString strMsg,
unsigned int nWndWidth,
unsigned int nWndHeight,
unsigned int nMsgTimeOut,
unsigned int nMsgWndCreationDelay,
CRect rectMsgRect,
CWnd* pWndParent)
{
CStatusBarMsgWnd* t_StatusBarMsgWnd = new CStatusBarMsgWnd(
strMsg,
nWndWidth,
nWndHeight,
nMsgTimeOut,
nMsgWndCreationDelay,
rectMsgRect,
pWndParent
);
return (t_StatusBarMsgWnd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -