Sat, 20 Jul 2019 13:09:31 +0200
Fixes from pull request #516 review
| 14743 | 1 | /* MinimizeToTray |
| 2 | * | |
| 3 | * A couple of routines to show how to make it produce a custom caption | |
| 4 | * animation to make it look like we are minimizing to and maximizing | |
| 5 | * from the system tray | |
| 6 | * | |
| 7 | * These routines are public domain, but it would be nice if you dropped | |
| 8 | * me a line if you use them! | |
| 9 | * | |
| 10 | * 1.0 29.06.2000 Initial version | |
| 11 | * 1.1 01.07.2000 The window retains it's place in the Z-order of windows | |
| 12 | * when minimized/hidden. This means that when restored/shown, it doesn't | |
| 13 | * always appear as the foreground window unless we call SetForegroundWindow | |
| 14 | * | |
| 15 | * Copyright 2000 Matthew Ellis <m.t.ellis@bigfoot.com> | |
| 16 | */ | |
| 17 | #include <windows.h> | |
| 18 | #include "MinimizeToTray.h" | |
| 19 | ||
| 20 | #define DEFAULT_RECT_WIDTH 150 | |
| 21 | #define DEFAULT_RECT_HEIGHT 30 | |
| 22 | ||
| 23 | static void GetTrayWndRect(LPRECT lpTrayRect) { | |
| 24 | APPBARDATA appBarData; | |
| 25 | HWND hShellTrayWnd = FindWindowEx(NULL, NULL, TEXT("Shell_TrayWnd"), | |
| 26 | NULL); | |
| 27 | ||
| 28 | if(hShellTrayWnd) { | |
| 29 | HWND hTrayNotifyWnd = FindWindowEx(hShellTrayWnd, NULL, | |
| 30 | TEXT("TrayNotifyWnd"), NULL); | |
| 31 | ||
| 32 | if(hTrayNotifyWnd) { | |
| 33 | GetWindowRect(hTrayNotifyWnd,lpTrayRect); | |
| 34 | return; | |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | appBarData.cbSize = sizeof(appBarData); | |
| 39 | if(SHAppBarMessage(ABM_GETTASKBARPOS, &appBarData)) { | |
| 40 | switch(appBarData.uEdge) { | |
| 41 | case ABE_LEFT: | |
| 42 | case ABE_RIGHT: | |
| 43 | lpTrayRect->top = appBarData.rc.bottom - 100; | |
| 44 | lpTrayRect->bottom = appBarData.rc.bottom - 16; | |
| 45 | lpTrayRect->left = appBarData.rc.left; | |
| 46 | lpTrayRect->right = appBarData.rc.right; | |
| 47 | break; | |
| 48 | case ABE_TOP: | |
| 49 | case ABE_BOTTOM: | |
| 50 | lpTrayRect->top = appBarData.rc.top; | |
| 51 | lpTrayRect->bottom = appBarData.rc.bottom; | |
| 52 | lpTrayRect->left = appBarData.rc.right - 100; | |
| 53 | lpTrayRect->right = appBarData.rc.right - 16; | |
| 54 | break; | |
| 55 | } | |
| 56 | return; | |
| 57 | } | |
| 58 | ||
| 59 | hShellTrayWnd = FindWindowEx(NULL, NULL, TEXT("Shell_TrayWnd"), NULL); | |
| 60 | if(hShellTrayWnd) { | |
| 61 | GetWindowRect(hShellTrayWnd, lpTrayRect); | |
| 62 | if(lpTrayRect->right-lpTrayRect->left > DEFAULT_RECT_WIDTH) | |
| 63 | lpTrayRect->left = lpTrayRect->right - DEFAULT_RECT_WIDTH; | |
| 64 | if(lpTrayRect->bottom-lpTrayRect->top > DEFAULT_RECT_HEIGHT) | |
| 65 | lpTrayRect->top=lpTrayRect->bottom - DEFAULT_RECT_HEIGHT; | |
| 66 | ||
| 67 | return; | |
| 68 | } | |
| 69 | ||
| 70 | SystemParametersInfo(SPI_GETWORKAREA, 0, lpTrayRect, 0); | |
| 71 | lpTrayRect->left = lpTrayRect->right - DEFAULT_RECT_WIDTH; | |
| 72 | lpTrayRect->top = lpTrayRect->bottom - DEFAULT_RECT_HEIGHT; | |
| 73 | } | |
| 74 | ||
| 75 | static BOOL GetDoAnimateMinimize(void) { | |
| 76 | ANIMATIONINFO ai; | |
| 77 | ||
| 78 | ai.cbSize = sizeof(ai); | |
| 79 | SystemParametersInfo(SPI_GETANIMATION, sizeof(ai), &ai, 0); | |
| 80 | ||
| 81 | return ai.iMinAnimate ? TRUE : FALSE; | |
| 82 | } | |
| 83 | ||
| 84 | void MinimizeWndToTray(HWND hWnd) { | |
| 85 | ||
| 86 | if(!IsWindowVisible(hWnd)) | |
| 87 | return; | |
| 88 | ||
| 89 | if(GetDoAnimateMinimize()) { | |
| 90 | RECT rcFrom, rcTo; | |
| 91 | ||
| 92 | GetWindowRect(hWnd, &rcFrom); | |
| 93 | GetTrayWndRect(&rcTo); | |
| 94 | ||
| 95 | DrawAnimatedRects(hWnd, IDANI_CAPTION, &rcFrom, &rcTo); | |
| 96 | } | |
| 97 | ||
| 98 | ShowWindow(hWnd, SW_HIDE); | |
| 99 | } | |
| 100 | ||
| 101 | void RestoreWndFromTray(HWND hWnd) { | |
| 102 | ||
| 103 | if(IsWindowVisible(hWnd)) | |
| 104 | return; | |
| 105 | ||
| 106 | if(GetDoAnimateMinimize()) { | |
| 107 | RECT rcFrom, rcTo; | |
| 108 | GetTrayWndRect(&rcFrom); | |
| 109 | GetWindowRect(hWnd, &rcTo); | |
| 110 | ||
| 111 | DrawAnimatedRects(hWnd, IDANI_CAPTION, &rcFrom, &rcTo); | |
| 112 | } | |
| 113 | ||
| 114 | ShowWindow(hWnd, SW_SHOW); | |
| 115 | SetActiveWindow(hWnd); | |
| 116 | SetForegroundWindow(hWnd); | |
| 117 | } | |
| 118 |