📄 statusbarmsgwnd.cpp
字号:
/*******************************************************************************
File: StatusBarMsgWnd.cpp
********************************************************************************/
#include "stdafx.h"
#include "StatusBarMsgWnd.h"
#include <mmsystem.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*----------------------------------------------------------------------------
Message map
----------------------------------------------------------------------------*/
BEGIN_MESSAGE_MAP(CStatusBarMsgWnd, CFrameWnd)
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_SETCURSOR()
ON_WM_DESTROY()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
END_MESSAGE_MAP()
/*-----------------------------------------------------------------------------
Function : CStatusBarMsgWnd::CStatusBarMsgWnd()
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(
CString strMsg,
unsigned int nWndWidth,
unsigned int nWndHeight,
unsigned int nMsgTimeOut,
unsigned int nMsgWndCreationDelay,
CRect rectMsgRect,
CWnd* pWndParent) : m_strMsg(strMsg), m_rectMsgRect(rectMsgRect)
{
m_nWndWidth = nWndWidth;
m_nWndHeight = nWndHeight;
m_pWndParent = pWndParent;
m_nMsgTimeOut = nMsgTimeOut;
m_nMsgWndCreationDelay = nMsgWndCreationDelay;
m_bMouseOverWnd = FALSE;
m_hCursor = NULL;
}
CStatusBarMsgWnd::~CStatusBarMsgWnd()
{
}
void CStatusBarMsgWnd::PopWndForLeftStatusBar()
{
CRect t_rect(0, 0, 0, 0);
Create(NULL, NULL, WS_VISIBLE | WS_OVERLAPPEDWINDOW, t_rect, m_pWndParent);
CRect rectDesktopWithoutTaskbar; // The desktop area
// Get the desktop area
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rectDesktopWithoutTaskbar, 0);
// Calculate the actual width of the Window and its position
m_nWndLeft = rectDesktopWithoutTaskbar.left;
m_nWndTop = rectDesktopWithoutTaskbar.bottom - m_nWndHeight;
m_nWndRight = m_nWndLeft + m_nWndWidth;
m_nWndBottom = m_nWndTop + m_nWndHeight;
m_nWndSize = 0; // The height of window is zero before showing
SetTimer(IDT_POP_WINDOW_TIMER, m_nMsgWndCreationDelay, NULL);
}
void CStatusBarMsgWnd::PopWndForRightStatusBar()
{
PopWndForBottomStatusBar();
}
void CStatusBarMsgWnd::PopWndForTopStatusBar()
{
CRect t_rect(0, 0, 0, 0);
Create(NULL, NULL, WS_VISIBLE | WS_OVERLAPPEDWINDOW, t_rect, m_pWndParent);
CRect rectDesktopWithoutTaskbar; // The desktop area
// Get the desktop area
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rectDesktopWithoutTaskbar, 0);
// Calculate the actual width of the Window and its position in screen co-ordinates
m_nWndLeft = rectDesktopWithoutTaskbar.right - m_nWndWidth;
m_nWndTop = rectDesktopWithoutTaskbar.top;
m_nWndRight = m_nWndLeft + m_nWndWidth;
m_nWndBottom = m_nWndTop + m_nWndHeight;
m_nWndSize = 0; // The height of window is zero before showing
SetTimer(IDT_POP_WINDOW_TIMER, m_nMsgWndCreationDelay, NULL);
}
void CStatusBarMsgWnd::PopWndForBottomStatusBar()
{
CRect t_rect(0, 0, 0, 0);
Create(NULL, NULL, WS_VISIBLE | WS_OVERLAPPEDWINDOW, t_rect, m_pWndParent);
CRect rectDesktopWithoutTaskbar; // The desktop area
// Get the desktop area
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rectDesktopWithoutTaskbar, 0);
// Calculate the actual width of the Window and its position in screen co-ordinates
m_nWndLeft = rectDesktopWithoutTaskbar.right - m_nWndWidth;
m_nWndTop = rectDesktopWithoutTaskbar.bottom - m_nWndHeight;
m_nWndRight = m_nWndLeft + m_nWndWidth;
m_nWndBottom = m_nWndTop + m_nWndHeight;
m_nWndSize = 0; // The height of window is zero before showing
SetTimer(IDT_POP_WINDOW_TIMER, m_nMsgWndCreationDelay, NULL);
}
void CStatusBarMsgWnd::PopMsg()
{
PlaySound("alarm.wav",NULL,SND_FILENAME);
if (CheckIfStatusBarBottom()) // Most frequent case is status bar at bottom
{
PopWndForBottomStatusBar();
}
else
{
if (CheckIfStatusBarTop())
{
PopWndForTopStatusBar();
}
else
{
if (CheckIfStatusBarLeft())
{
PopWndForLeftStatusBar();
}
else
{
m_nStatusBarPos = STP_RIGHT; // Force it, as no need for calling "CheckIfStatusBarRight()
PopWndForRightStatusBar();
}
}
}
}
BOOL CStatusBarMsgWnd::CheckIfStatusBarLeft()
{
unsigned int nAvailableScreenTop;
unsigned int nAvailableScreenLeft;
CRect rectDesktopWithoutTaskbar; // The desktop area without status bar
// Get the desktop area minus the status
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rectDesktopWithoutTaskbar, 0);
nAvailableScreenLeft = rectDesktopWithoutTaskbar.left;
nAvailableScreenTop = rectDesktopWithoutTaskbar.top;
if ((nAvailableScreenLeft > 0) && (nAvailableScreenTop == 0))
{
m_nStatusBarPos = STP_LEFT;
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CStatusBarMsgWnd::CheckIfStatusBarRight()
{
unsigned int nAvailableScreenWidth;
unsigned int nAvailableScreenHeight;
unsigned int nActualScreenWidth;
unsigned int nActualScreenHeight;
// Calculate the actual screen height and width
nActualScreenWidth = ::GetSystemMetrics(SM_CXFULLSCREEN);
nActualScreenHeight = ::GetSystemMetrics(SM_CYFULLSCREEN);
CRect rectDesktopWithoutTaskbar; // The desktop area without status bar
// Get the desktop area minus the status
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rectDesktopWithoutTaskbar, 0);
nAvailableScreenWidth = rectDesktopWithoutTaskbar.Width();
nAvailableScreenHeight = rectDesktopWithoutTaskbar.Height();
if ((nAvailableScreenWidth != nActualScreenWidth) &&
(nAvailableScreenHeight == nActualScreenHeight))
{
m_nStatusBarPos = STP_RIGHT;
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CStatusBarMsgWnd::CheckIfStatusBarTop()
{
unsigned int nAvailableScreenTop;
unsigned int nAvailableScreenLeft;
CRect rectDesktopWithoutTaskbar; // The desktop area without status bar
// Get the desktop area minus the status
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rectDesktopWithoutTaskbar, 0);
nAvailableScreenLeft = rectDesktopWithoutTaskbar.left;
nAvailableScreenTop = rectDesktopWithoutTaskbar.top;
if ((nAvailableScreenLeft == 0) && (nAvailableScreenTop > 0))
{
m_nStatusBarPos = STP_TOP;
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CStatusBarMsgWnd::CheckIfStatusBarBottom()
{
unsigned int nAvailableScreenWidth;
unsigned int nAvailableScreenBottom;
unsigned int nActualScreenWidth;
unsigned int nActualScreenBottom;
// Calculate the actual screen height and width
nActualScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
nActualScreenBottom = ::GetSystemMetrics(SM_CYSCREEN);
CRect rectDesktopWithoutTaskbar; // The desktop area without status bar
// Get the desktop area minus the status
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rectDesktopWithoutTaskbar, 0);
nAvailableScreenWidth = rectDesktopWithoutTaskbar.Width();
nAvailableScreenBottom = rectDesktopWithoutTaskbar.bottom;
if ((nAvailableScreenWidth == nActualScreenWidth) &&
(nAvailableScreenBottom < nActualScreenBottom))
{
m_nStatusBarPos = STP_BOTTOM;
return TRUE;
}
else
{
return FALSE;
}
}
void CStatusBarMsgWnd::OnSize(unsigned int nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
//SendMessage(WM_MOUSEMOVE, 0);
CFont* pFont = NULL;
CClientDC dc(this);
if (m_bMouseOverWnd)
{
pFont = dc.SelectObject(&m_fontMessageUnderline);
}
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);
//dc.DrawText(m_strMsg, &m_rectMsgRect, DT_WORDBREAK | DT_CENTER);
}
int CStatusBarMsgWnd::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
ModifyStyle(WS_CAPTION, 0, SWP_FRAMECHANGED); // removes title bar
// Start creating two fonts, one underlined , other non underlined
//
// LOGFONT structure for font properties
LOGFONT lf;
::ZeroMemory (&lf, sizeof (lf));
lf.lfHeight = 100;
lf.lfWeight = FW_BOLD;
lf.lfUnderline = TRUE;
::strcpy (lf.lfFaceName, _T("Arial"));
// Prepare for an underlined font
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -