DuiLib : 做一個沒有工作列圖示的Dialog

DuiLib : 做一個沒有工作列圖示的Dialog

看到有些軟體在桌面右下角彈框時, 工作列沒有圖示, 看起來像是從托盤程式彈出的。

實際上是一個單獨的exe, 在Create時, 將 WS_EX_APPWINDOW 遮蔽掉了,就有這效果.

這次, 將設定Create風格的實現直接挪到DuiLib中, 形成2個虛擬函式. 

如果要實現的Dlg需要不同的風格, 就過載這兩個虛擬函式.  

這麼搞, 以後再也不會忘記設定風格的程式碼如何寫. 因為GetStyle和GetExStyle很容易記憶.

工程下載點: src_prj_dlg_no_task_bar_icon_2015_0718_2000.zip

編譯環境 : vs2010 vc DuiLib

效果圖:

\DuiLib\Utils\WinImplBase.h
virtual LONG GetStyle();
virtual LONG GetExStyle();
};
\srcMain\dlgNotify\MainDlg.h
virtual void InitWindow();
virtual LONG GetStyle();
\DuiLib\Utils\WinImplBase.cpp
LRESULT WindowImplBase::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
LONG styleValue = 0;
styleValue = GetStyle();
::SetWindowLong(*this, GWL_STYLE, styleValue);
styleValue = GetExStyle();
::SetWindowLong(*this, GWL_EXSTYLE, styleValue);
//...
LONG WindowImplBase::GetStyle()
{
LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
styleValue &= ~WS_CAPTION;
styleValue |= WS_CLIPSIBLINGS;
styleValue |= WS_CLIPCHILDREN;
return styleValue;
}
LONG WindowImplBase::GetExStyle()
{
LONG styleValue = ::GetWindowLong(*this, GWL_EXSTYLE);
return styleValue;
}
\srcMain\dlgNotify\MainDlg.cpp
LONG CMainDlg::GetStyle()
{
long dwStyle = __super::GetStyle();
dwStyle &= ~WS_MAXIMIZEBOX;
return dwStyle;
}
LONG CMainDlg::GetExStyle()
{
long dwStyle = __super::GetExStyle();
dwStyle |= WS_EX_TOOLWINDOW;
dwStyle &= ~(WS_EX_APPWINDOW);
return dwStyle;
}

為了將窗體挪到桌面的右下角, 參考 CWindowWnd::CenterWindow(), 寫了一個 CMainDlg::MoveMyWindowToDesktopRightBottom()

臨時用一下, 沒有進行重構.

void CMainDlg::MoveMyWindowToDesktopRightBottom()
{
ASSERT(::IsWindow(m_hWnd));
ASSERT((GetWindowStyle(m_hWnd)&WS_CHILD)==0);
RECT rcDlg = { 0 };
::GetWindowRect(m_hWnd, &rcDlg);
RECT rcArea = { 0 };
RECT rcCenter = { 0 };
HWND hWnd=*this;
HWND hWndParent = ::GetParent(m_hWnd);
HWND hWndCenter = ::GetWindowOwner(m_hWnd);
if (hWndCenter!=NULL)
hWnd=hWndCenter;
// 處理多顯示器模式下螢幕居中
MONITORINFO oMonitor = {};
oMonitor.cbSize = sizeof(oMonitor);
::GetMonitorInfo(::MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), &oMonitor);
rcArea = oMonitor.rcWork;
if( hWndCenter == NULL )
rcCenter = rcArea;
else
::GetWindowRect(hWndCenter, &rcCenter);
int DlgWidth = rcDlg.right - rcDlg.left;
int DlgHeight = rcDlg.bottom - rcDlg.top;
// Find dialog's upper left based on rcCenter
int xLeft = rcCenter.right - DlgWidth;
int yTop = rcCenter.bottom - DlgHeight;
// 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;
::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}