HWND_TOPMOST)
// 對話框資源設為 POPUP 風格,移除 Title Bar
// 程式碼動態調整視窗大小貼合位圖
CBitmap m_Bitmap;
HBITMAP hBitmap = m_Logo.GetBitmap();
BITMAP bmp; m_Bitmap.GetBitmap(&bmp);
MoveWindow(0, 0, bmp.bmWidth, bmp.bmHeight);
m_Logo.MoveWindow(0, 0, bmp.bmWidth, bmp.bmHeight);
CenterWindow();
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
UINT CFloatWnd::OnNcHitTest(CPoint pt)
{
UINT nHitTest = CDialog::OnNcHitTest(pt);
if (nHitTest == HTCLIENT && ::GetAsyncKeyState(MK_LBUTTON) < 0)
nHitTest = HTCAPTION; // 告訴系統:使用者在拖曳標題列
return nHitTest;
}
// 啟用 WS_EX_LAYERED 擴展風格 (0x80000)
SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) ^ 0x80000);
// 動態載入 User32.dll 取得 SetLayeredWindowAttributes
void CFloatWnd::OnUpdateTransparent(int iTransparent)
{
HINSTANCE hInst = LoadLibrary(_T("User32.DLL"));
if (hInst) {
typedef BOOL (WINAPI *SLWA)(HWND, COLORREF, BYTE, DWORD);
SLWA pFun = (SLWA)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if (pFun) pFun(m_hWnd, 0, (BYTE)iTransparent, LWA_ALPHA); // LWA_ALPHA = 0x2
FreeLibrary(hInst);
}
}
void CFloatWnd::OnNcLButtonDblClk(UINT nFlags, CPoint point)
{
CWnd* pParent = GetParent();
if (!pParent->IsWindowVisible()) pParent->ShowWindow(SW_SHOW);
pParent->SetForegroundWindow();
CDialog::OnNcLButtonDblClk(nFlags, point);
}
注意:因 OnNcHitTest 回傳 HTCAPTION,雙擊會觸發 WM_NCLBUTTONDBLCLK 而非 WM_LBUTTONDBLCLK。
void CMainDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int iCurPos = m_Slider.GetPos();
// 處理 SB_LINELEFT, SB_LINERIGHT, SB_PAGELEFT, SB_PAGERIGHT, SB_THUMBTRACK...
m_Slider.SetPos(iCurPos);
pFloatWnd->OnUpdateTransparent(iCurPos);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
Windows XP SP2 + Visual C++ 6.0 編譯通過。完整原始碼包含右鍵選單、顯示/隱藏切換、程式結束等輔助功能。