From 8e4d37a8ea74dd3091c6962f846cca08c7f6704e Mon Sep 17 00:00:00 2001 From: spx Date: Wed, 14 Mar 2012 02:52:35 +0000 Subject: [PATCH] - Now retries UpdateLayeredWindow if it fails for some reason. (E.g. when SetLayeredWindowAttributes is called by other application process.) - Replaced Get(Set)WindowLong with Get(Set)WindowLongPtr. - Code cosmetics. --- Library/Dialog.cpp | 2 +- Library/DialogManage.cpp | 2 +- Library/MeterWindow.cpp | 113 ++++++++++++++++++++------------------- Library/MeterWindow.h | 2 + Library/System.cpp | 8 +-- 5 files changed, 67 insertions(+), 60 deletions(-) diff --git a/Library/Dialog.cpp b/Library/Dialog.cpp index 903b3a63..f471ebb9 100644 --- a/Library/Dialog.cpp +++ b/Library/Dialog.cpp @@ -67,7 +67,7 @@ INT_PTR CDialog::OnActivate(WPARAM wParam, LPARAM lParam) void CDialog::SetDialogRTL() { - SetWindowLong(m_Window, GWL_EXSTYLE, GetWindowLong(m_Window, GWL_EXSTYLE) | WS_EX_LAYOUTRTL); + SetWindowLongPtr(m_Window, GWL_EXSTYLE, GetWindowLongPtr(m_Window, GWL_EXSTYLE) | WS_EX_LAYOUTRTL); } /* diff --git a/Library/DialogManage.cpp b/Library/DialogManage.cpp index a09c9160..cfa35f15 100644 --- a/Library/DialogManage.cpp +++ b/Library/DialogManage.cpp @@ -376,7 +376,7 @@ void CDialogManage::CTabSkins::Initialize() // Get rid of the EDITTEXT control border item = GetDlgItem(m_Window, IDC_MANAGESKINS_DESCRIPTION_TEXT); - SetWindowLong(item, GWL_EXSTYLE, GetWindowLong(item, GWL_EXSTYLE) &~ WS_EX_CLIENTEDGE); + SetWindowLongPtr(item, GWL_EXSTYLE, GetWindowLongPtr(item, GWL_EXSTYLE) &~ WS_EX_CLIENTEDGE); SetWindowPos(item, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER); item = GetDlgItem(m_Window, IDC_MANAGESKINS_DISPLAYMONITOR_BUTTON); diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index 6e409e8e..0893f5ec 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -313,6 +313,24 @@ void CMeterWindow::IgnoreAeroPeek() } } +void CMeterWindow::AddWindowExStyle(LONG_PTR flag) +{ + LONG_PTR style = GetWindowLongPtr(m_Window, GWL_EXSTYLE); + if ((style & flag) == 0) + { + SetWindowLongPtr(m_Window, GWL_EXSTYLE, style | flag); + } +} + +void CMeterWindow::RemoveWindowExStyle(LONG_PTR flag) +{ + LONG_PTR style = GetWindowLongPtr(m_Window, GWL_EXSTYLE); + if ((style & flag) != 0) + { + SetWindowLongPtr(m_Window, GWL_EXSTYLE, style & ~flag); + } +} + /* ** Unloads the skin with delay to avoid crash (and for fade to complete). ** @@ -400,11 +418,7 @@ void CMeterWindow::Refresh(bool init, bool all) InitializeMeters(); // Remove transparent flag - LONG style = GetWindowLong(m_Window, GWL_EXSTYLE); - if ((style & WS_EX_TRANSPARENT) != 0) - { - SetWindowLong(m_Window, GWL_EXSTYLE, style & ~WS_EX_TRANSPARENT); - } + RemoveWindowExStyle(WS_EX_TRANSPARENT); m_Hidden = m_WindowStartHidden; @@ -636,7 +650,7 @@ void CMeterWindow::ChangeZPos(ZPOSITION zPos, bool all) // Find the "backmost" topmost window while (winPos = ::GetNextWindow(winPos, GW_HWNDPREV)) { - if (GetWindowLong(winPos, GWL_EXSTYLE) & WS_EX_TOPMOST) + if (GetWindowLongPtr(winPos, GWL_EXSTYLE) & WS_EX_TOPMOST) { // Insert after the found window if (0 != SetWindowPos(m_Window, winPos, 0, 0, 0, 0, ZPOS_FLAGS)) @@ -2874,9 +2888,7 @@ void CMeterWindow::UpdateTransparency(int alpha, bool reset) { if (reset) { - // Add the window flag - LONG style = GetWindowLong(m_Window, GWL_EXSTYLE); - SetWindowLong(m_Window, GWL_EXSTYLE, style | WS_EX_LAYERED); + AddWindowExStyle(WS_EX_LAYERED); } BLENDFUNCTION blendPixelFunction = {AC_SRC_OVER, 0, alpha, AC_SRC_ALPHA}; @@ -2888,7 +2900,14 @@ void CMeterWindow::UpdateTransparency(int alpha, bool reset) HDC dcMemory = CreateCompatibleDC(dcScreen); SelectObject(dcMemory, m_DIBSectionBuffer); - UpdateLayeredWindow(m_Window, dcScreen, &ptWindowScreenPosition, &szWindow, dcMemory, &ptSrc, 0, &blendPixelFunction, ULW_ALPHA); + BOOL ret = UpdateLayeredWindow(m_Window, dcScreen, &ptWindowScreenPosition, &szWindow, dcMemory, &ptSrc, 0, &blendPixelFunction, ULW_ALPHA); + if (!ret) + { + // Retry after resetting WS_EX_LAYERED flag + RemoveWindowExStyle(WS_EX_LAYERED); + AddWindowExStyle(WS_EX_LAYERED); + UpdateLayeredWindow(m_Window, dcScreen, &ptWindowScreenPosition, &szWindow, dcMemory, &ptSrc, 0, &blendPixelFunction, ULW_ALPHA); + } ReleaseDC(0, dcScreen); DeleteDC(dcMemory); @@ -2899,9 +2918,7 @@ void CMeterWindow::UpdateTransparency(int alpha, bool reset) { if (reset) { - // Remove the window flag - LONG style = GetWindowLong(m_Window, GWL_EXSTYLE); - SetWindowLong(m_Window, GWL_EXSTYLE, style & ~WS_EX_LAYERED); + RemoveWindowExStyle(WS_EX_LAYERED); } } } @@ -3129,11 +3146,7 @@ void CMeterWindow::ShowWindowIfAppropriate() if (!inside || keyDown) { // If Alt, shift or control is down, remove the transparent flag - LONG style = GetWindowLong(m_Window, GWL_EXSTYLE); - if ((style & WS_EX_TRANSPARENT) != 0) - { - SetWindowLong(m_Window, GWL_EXSTYLE, style & ~WS_EX_TRANSPARENT); - } + RemoveWindowExStyle(WS_EX_TRANSPARENT); } } @@ -3327,11 +3340,7 @@ LRESULT CMeterWindow::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam) { if (m_ClickThrough) { - LONG style = GetWindowLong(m_Window, GWL_EXSTYLE); - if ((style & WS_EX_TRANSPARENT) == 0) - { - SetWindowLong(m_Window, GWL_EXSTYLE, style | WS_EX_TRANSPARENT); - } + AddWindowExStyle(WS_EX_TRANSPARENT); } if (!m_Hidden) @@ -3366,8 +3375,8 @@ LRESULT CMeterWindow::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam) if (!m_ClickThrough || keyDown) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCMOUSEMOVE) { @@ -3599,11 +3608,7 @@ void CMeterWindow::SetClickThrough(bool b) if (!m_ClickThrough) { // Remove transparent flag - LONG style = GetWindowLong(m_Window, GWL_EXSTYLE); - if ((style & WS_EX_TRANSPARENT) != 0) - { - SetWindowLong(m_Window, GWL_EXSTYLE, style & ~WS_EX_TRANSPARENT); - } + RemoveWindowExStyle(WS_EX_TRANSPARENT); } if (m_MouseOver) @@ -3775,8 +3780,8 @@ LRESULT CMeterWindow::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam) if (m_WindowDraggable && !m_Rainmeter->GetDisableDragging()) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); MapWindowPoints(NULL, m_Window, &pos, 1); int x1 = m_DragMargins.left; @@ -4002,8 +4007,8 @@ LRESULT CMeterWindow::OnSettingChange(UINT uMsg, WPARAM wParam, LPARAM lParam) LRESULT CMeterWindow::OnLeftButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCLBUTTONDOWN) { @@ -4034,8 +4039,8 @@ LRESULT CMeterWindow::OnLeftButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam) LRESULT CMeterWindow::OnLeftButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCLBUTTONUP) { @@ -4058,8 +4063,8 @@ LRESULT CMeterWindow::OnLeftButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam) LRESULT CMeterWindow::OnLeftButtonDoubleClick(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCLBUTTONDBLCLK) { @@ -4085,8 +4090,8 @@ LRESULT CMeterWindow::OnLeftButtonDoubleClick(UINT uMsg, WPARAM wParam, LPARAM l LRESULT CMeterWindow::OnRightButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCRBUTTONDOWN) { @@ -4109,8 +4114,8 @@ LRESULT CMeterWindow::OnRightButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam) LRESULT CMeterWindow::OnRightButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); // Handle buttons HandleButtons(pos, BUTTONPROC_MOVE); @@ -4132,8 +4137,8 @@ LRESULT CMeterWindow::OnRightButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam) LRESULT CMeterWindow::OnRightButtonDoubleClick(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCRBUTTONDBLCLK) { @@ -4159,8 +4164,8 @@ LRESULT CMeterWindow::OnRightButtonDoubleClick(UINT uMsg, WPARAM wParam, LPARAM LRESULT CMeterWindow::OnMiddleButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCMBUTTONDOWN) { @@ -4183,8 +4188,8 @@ LRESULT CMeterWindow::OnMiddleButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam LRESULT CMeterWindow::OnMiddleButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCMBUTTONUP) { @@ -4207,8 +4212,8 @@ LRESULT CMeterWindow::OnMiddleButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam) LRESULT CMeterWindow::OnMiddleButtonDoubleClick(UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pos; - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); if (uMsg == WM_NCMBUTTONDBLCLK) { @@ -4245,8 +4250,8 @@ LRESULT CMeterWindow::OnContextMenu(UINT uMsg, WPARAM wParam, LPARAM lParam) } else { - pos.x = (SHORT)LOWORD(lParam); - pos.y = (SHORT)HIWORD(lParam); + pos.x = GET_X_LPARAM(lParam); + pos.y = GET_Y_LPARAM(lParam); // Transform the point to client rect POINT posc = {pos.x - rect.left, pos.y - rect.top}; @@ -4593,8 +4598,8 @@ LRESULT CMeterWindow::OnMove(UINT uMsg, WPARAM wParam, LPARAM lParam) // and in parent-client coordinates for child windows. // Store the new window position - m_ScreenX = (SHORT)LOWORD(lParam); - m_ScreenY = (SHORT)HIWORD(lParam); + m_ScreenX = GET_X_LPARAM(lParam); + m_ScreenY = GET_Y_LPARAM(lParam); SetWindowPositionVariables(m_ScreenX, m_ScreenY); diff --git a/Library/MeterWindow.h b/Library/MeterWindow.h index 5de8fbe2..10f052b4 100644 --- a/Library/MeterWindow.h +++ b/Library/MeterWindow.h @@ -336,6 +336,8 @@ private: bool DoMoveAction(int x, int y, MOUSE mouse); bool ResizeWindow(bool reset); void IgnoreAeroPeek(); + void AddWindowExStyle(LONG_PTR flag); + void RemoveWindowExStyle(LONG_PTR flag); void BlurBehindWindow(BOOL fEnable); void SetWindowPositionVariables(int x, int y); void SetWindowSizeVariables(int w, int h); diff --git a/Library/System.cpp b/Library/System.cpp index ca92fa21..fe3bf6dc 100644 --- a/Library/System.cpp +++ b/Library/System.cpp @@ -814,7 +814,7 @@ void CSystem::PrepareHelperWindow(HWND WorkerW) HWND hwnd = WorkerW; while (hwnd = ::GetNextWindow(hwnd, GW_HWNDPREV)) { - if (GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST) + if (GetWindowLongPtr(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST) { WCHAR className[64], windowText[64]; @@ -832,7 +832,7 @@ void CSystem::PrepareHelperWindow(HWND WorkerW) if (logging) { LogWithArgs(LOG_DEBUG, L"System: HelperWindow: hwnd=0x%p (WorkerW=0x%p), hwndInsertAfter=0x%p (\"%s\" %s) - %s", - c_HelperWindow, WorkerW, hwnd, windowText, className, (GetWindowLong(c_HelperWindow, GWL_EXSTYLE) & WS_EX_TOPMOST) ? L"TOPMOST" : L"NORMAL"); + c_HelperWindow, WorkerW, hwnd, windowText, className, (GetWindowLongPtr(c_HelperWindow, GWL_EXSTYLE) & WS_EX_TOPMOST) ? L"TOPMOST" : L"NORMAL"); } return; } @@ -849,7 +849,7 @@ void CSystem::PrepareHelperWindow(HWND WorkerW) if (logging) { LogWithArgs(LOG_DEBUG, L"System: HelperWindow: hwnd=0x%p (WorkerW=0x%p), hwndInsertAfter=HWND_TOPMOST - %s", - c_HelperWindow, WorkerW, (GetWindowLong(c_HelperWindow, GWL_EXSTYLE) & WS_EX_TOPMOST) ? L"TOPMOST" : L"NORMAL"); + c_HelperWindow, WorkerW, (GetWindowLongPtr(c_HelperWindow, GWL_EXSTYLE) & WS_EX_TOPMOST) ? L"TOPMOST" : L"NORMAL"); } } else @@ -860,7 +860,7 @@ void CSystem::PrepareHelperWindow(HWND WorkerW) if (logging) { LogWithArgs(LOG_DEBUG, L"System: HelperWindow: hwnd=0x%p (WorkerW=0x%p), hwndInsertAfter=HWND_BOTTOM - %s", - c_HelperWindow, WorkerW, (GetWindowLong(c_HelperWindow, GWL_EXSTYLE) & WS_EX_TOPMOST) ? L"TOPMOST" : L"NORMAL"); + c_HelperWindow, WorkerW, (GetWindowLongPtr(c_HelperWindow, GWL_EXSTYLE) & WS_EX_TOPMOST) ? L"TOPMOST" : L"NORMAL"); } } }