aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/General/gen_ml/banner.cpp
blob: f701625f94a156e619c548249423df5b26de3847 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "banner.h"
#include "graphics.h"


MLBanner::MLBanner(void)
{
	bmpBck		= NULL;
	bmpLogo		= NULL;
	bmpLogoMask	= NULL;
	bmpBanner   = NULL;
	m_hwnd      = NULL;
	oldWndProc	= NULL;

	color1 = RGB(0,0,0);
	color2 = RGB(255,255,255);

	hInstance = NULL;
	logoResId = 0; 
	bgndResId = 0;

	SetRect(&rcBanner, 0,0,0,0);
}
MLBanner::~MLBanner(void)
{
	DestroyImages();
	SetWindowLong(m_hwnd, GWL_WNDPROC, (LONG)oldWndProc);
	oldWndProc	= NULL;
}

void MLBanner::SetColors(int color1, int color2)
{
	this->color1 = color1;
	this->color2 = color2;
	ReloadImages();
}

void MLBanner::SetImages(HINSTANCE hInstance, int bgndResId, int logoResId)
{
	this->hInstance = hInstance;
	this->logoResId = logoResId;
	this->bgndResId = bgndResId;
	ReloadImages();
}

void MLBanner::ReloadImages(void)
{
	DestroyImages();
	if (hInstance)
	{
		if (bgndResId) 
		{
			bmpBck = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(bgndResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
			if (bmpBck) bmpBck = PatchBitmapColors24(bmpBck, color1, color2, Filter1);
		}
		if (logoResId)
		{
			bmpLogo = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(logoResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
			if (bmpLogo) 
			{
				bmpLogoMask = CreateBitmapMask(bmpLogo, 1,1);
			}
		}
	}

}

void MLBanner::DestroyImages(void)
{
	if (bmpBck) DeleteObject(bmpBck);
	bmpBck = NULL;
	
	if (bmpLogo) DeleteObject(bmpLogo);
	bmpLogo = NULL;

	if (bmpLogoMask) DeleteObject(bmpLogoMask);
	bmpLogoMask = NULL;

	if (bmpBanner) DeleteObject(bmpBanner);
	bmpBanner = NULL;
}



void MLBanner::UpdateBunnerBmp(void)
{
	if (bmpBanner) DeleteObject(bmpBanner);
	
	HDC hdc = GetDC(m_hwnd);
	
	bmpBanner = CreateCompatibleBitmap(hdc, rcBanner.right, rcBanner.bottom);
	HDC memDstDC = CreateCompatibleDC (hdc);
	HDC memSrcDC = CreateCompatibleDC (hdc);
    SelectObject(memDstDC, bmpBanner);
	SelectObject(memSrcDC, bmpBck);

	for (int i = 0; i < rcBanner.right; i++)
	{
		BitBlt(memDstDC,  
        i,0, 
        1, rcBanner.bottom, 
        memSrcDC, 
        0,0, 
       SRCCOPY); 
	   
	}
	
	BITMAP bm;
    GetObject(bmpLogo, sizeof(BITMAP), &bm);

	SelectObject(memSrcDC, bmpLogoMask);
	BitBlt(memDstDC,  
		6, 
		max(2, (rcBanner.bottom - bm.bmHeight) / 2),
		min(rcBanner.right - 4, bm.bmWidth), 
		min(rcBanner.bottom - 2, bm.bmHeight), 
		memSrcDC, 
		0,0, 
		SRCAND);

    SelectObject(memSrcDC, bmpLogo);
   	BitBlt(memDstDC,  
		6, 
		max(2, (rcBanner.bottom - bm.bmHeight) / 2),
		min(rcBanner.right - 4, bm.bmWidth), 
		min(rcBanner.bottom - 2, bm.bmHeight), 
		memSrcDC, 
		0,0, 
		SRCPAINT);

	ReleaseDC(m_hwnd, hdc); 
	DeleteDC(memDstDC);
	DeleteDC(memSrcDC);

}

void MLBanner::Init(HWND hwnd)
{
	m_hwnd = hwnd;
	SetWindowLong(hwnd,GWL_USERDATA,(LONG)this);
	oldWndProc= (WNDPROC) SetWindowLong(hwnd, GWL_WNDPROC, (LONG)newWndProc);
	UpdateBunnerBmp();
}

BOOL CALLBACK MLBanner::newWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
{
	MLBanner *banner = (MLBanner*)GetWindowLong(hwndDlg, GWL_USERDATA);
	
	switch(uMsg)
	{
		case WM_SIZE:
			if (SIZE_MINIMIZED != wParam)
			{
				SetRect(&banner->rcBanner, 0,0,LOWORD(lParam),HIWORD(lParam));
				banner->UpdateBunnerBmp();
			}
			break;
		case WM_ERASEBKGND:
			{
				HDC hdc = GetDC(hwndDlg);
				if (banner->bmpBanner)
				{
					HDC memSrcDC = CreateCompatibleDC (hdc);
					SelectObject(memSrcDC, banner->bmpBanner);
					StretchBlt(	hdc,  
								banner->rcBanner.left,
								banner->rcBanner.top,
								banner->rcBanner.right - banner->rcBanner.left, 
								banner->rcBanner.bottom - banner->rcBanner.top, 
								memSrcDC, 
								banner->rcBanner.left,
								banner->rcBanner.top,
								banner->rcBanner.right - banner->rcBanner.left, 
								banner->rcBanner.bottom - banner->rcBanner.top, 
								SRCCOPY); 
					DeleteDC(memSrcDC);
				}
				ReleaseDC(hwndDlg, hdc);
			}
			return TRUE;
		case WM_PAINT:
			{
				PAINTSTRUCT pt;
				HDC hdc = BeginPaint(hwndDlg, &pt);
				if (!banner->bmpBanner)
				{
					SetRect(&banner->rcBanner, 0,0,pt.rcPaint.right - pt.rcPaint.left, pt.rcPaint.bottom - pt.rcPaint.top);
					banner->UpdateBunnerBmp();
				}
				if (banner->bmpBanner)
				{
					HDC memSrcDC = CreateCompatibleDC (hdc);
					SelectObject(memSrcDC, banner->bmpBanner);
					StretchBlt(	hdc,  
								pt.rcPaint.left,
								pt.rcPaint.top,
								pt.rcPaint.right - pt.rcPaint.left, 
								pt.rcPaint.bottom - pt.rcPaint.top, 
								memSrcDC, 
								pt.rcPaint.left,
								pt.rcPaint.top,
								pt.rcPaint.right - pt.rcPaint.left, 
								pt.rcPaint.bottom - pt.rcPaint.top, 
								SRCCOPY); 
					DeleteDC(memSrcDC);
					ValidateRect(hwndDlg, &pt.rcPaint);
				}
				EndPaint(hwndDlg, &pt);
			}
			break;
	}

	return CallWindowProc(banner->oldWndProc, hwndDlg, uMsg, wParam, lParam);
}