forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayeredWindowHelperST.cpp
120 lines (109 loc) · 3.76 KB
/
LayeredWindowHelperST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "stdafx.h"
#include "LayeredWindowHelperST.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CLayeredWindowHelperST::CLayeredWindowHelperST()
{
m_hDll = GetModuleHandle(_T("user32"));
}
CLayeredWindowHelperST::~CLayeredWindowHelperST()
{
}
// This function adds the WS_EX_LAYERED style to the specified window.
//
// Parameters:
// [IN] Handle to the window and, indirectly, the class to which the window belongs.
// Windows 95/98/Me: The SetWindowLong function may fail if the window
// specified by the hWnd parameter does not belong to the same process
// as the calling thread.
//
// Return value:
// Non zero
// Function executed successfully.
// Zero
// Function failed. To get extended error information, call ::GetLastError().
//
LONG CLayeredWindowHelperST::AddLayeredStyle(HWND hWnd)
{
return ::SetWindowLong(hWnd, GWL_EXSTYLE, ::GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
} // End of AddLayeredStyle
// This function removes the WS_EX_LAYERED style from the specified window.
//
// Parameters:
// [IN] Handle to the window and, indirectly, the class to which the window belongs.
// Windows 95/98/Me: The SetWindowLong function may fail if the window
// specified by the hWnd parameter does not belong to the same process
// as the calling thread.
//
// Return value:
// Non zero
// Function executed successfully.
// Zero
// Function failed. To get extended error information, call ::GetLastError().
//
LONG CLayeredWindowHelperST::RemoveLayeredStyle(HWND hWnd)
{
return ::SetWindowLong(hWnd, GWL_EXSTYLE, ::GetWindowLong(hWnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
} // End of RemoveLayeredStyle
// This function sets the opacity and transparency color key of a layered window.
//
// Parameters:
// [IN] hWnd
// Handle to the layered window.
// [IN] crKey
// A COLORREF value that specifies the transparency color key to be used when
// composing the layered window. All pixels painted by the window in this color will be transparent.
// To generate a COLORREF, use the RGB() macro.
// [IN] bAlpha
// Alpha value used to describe the opacity of the layered window.
// When bAlpha is 0, the window is completely transparent.
// When bAlpha is 255, the window is opaque.
// [IN] dwFlags
// Specifies an action to take. This parameter can be one or more of the following values:
// LWA_COLORKEY Use crKey as the transparency color.
// LWA_ALPHA Use bAlpha to determine the opacity of the layered window.
//
// Return value:
// TRUE
// Function executed successfully.
// FALSE
// Function failed. To get extended error information, call ::GetLastError().
//
BOOL CLayeredWindowHelperST::SetLayeredWindowAttributes(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
{
BOOL bRetValue = TRUE;
if (m_hDll)
{
lpfnSetLayeredWindowAttributes pFn = NULL;
pFn = (lpfnSetLayeredWindowAttributes)GetProcAddress(m_hDll, "SetLayeredWindowAttributes");
if (pFn)
{
bRetValue = pFn(hWnd, crKey, bAlpha, dwFlags);
} // if
else bRetValue = FALSE;
} // if
return bRetValue;
} // End of SetLayeredWindowAttributes
// This function sets the percentage of opacity or transparency of a layered window.
//
// Parameters:
// [IN] hWnd
// Handle to the layered window.
// [IN] byPercentage
// Percentage (from 0 to 100)
//
// Return value:
// TRUE
// Function executed successfully.
// FALSE
// Function failed. To get extended error information, call ::GetLastError().
//
BOOL CLayeredWindowHelperST::SetTransparentPercentage(HWND hWnd, UINT byPercentage)
{
// Do not accept values greater than 100%
if (byPercentage > 100) byPercentage = 100;
return SetLayeredWindowAttributes(hWnd, 0, (BYTE)(255 * byPercentage/100), LWA_ALPHA);
} // End of SetTransparentPercentage