-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColourSelectable.c
More file actions
142 lines (116 loc) · 3.33 KB
/
Copy pathColourSelectable.c
File metadata and controls
142 lines (116 loc) · 3.33 KB
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
#include "ColourSelectable.h"
#include "Colours.h"
#include "ColourPicker.h"
#include <windows.h>
#include <stdio.h>
static ATOM ColourSelectableClass = 0;
void ColourSelectable_RegisterClass(HINSTANCE hInstance)
{
WNDCLASS cls;
if (ColourSelectableClass != 0)
return;
cls.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
cls.lpfnWndProc = ColourSelectable_WndProc;
cls.cbClsExtra = 0;
cls.cbWndExtra = 0;
cls.hInstance = hInstance;
cls.hIcon = LoadIcon (NULL, IDI_APPLICATION);
cls.hCursor = LoadCursor (NULL, IDC_HAND);
cls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
cls.lpszMenuName = NULL;
cls.lpszClassName = COLOURSELECTABLE_CLASSNAME;
ColourSelectableClass = RegisterClass (&cls);
}
HWND ColourSelectable_Create(HINSTANCE hInstance, int x, int y, int width, int height, HWND hParent, ColourSelectable_Data *csd)
{
HWND hWnd = NULL;
RECT rect;
ColourSelectable_RegisterClass(hInstance);
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, COLOURSELECTABLE_CLASSNAME, "", WS_VISIBLE | WS_CHILD,
x, y, width, height, hParent, NULL, hInstance, NULL);
if (!hWnd) return NULL;
// Ensure the inner size is correct
GetClientRect(hWnd, &rect);
MoveWindow(hWnd, x, y, width + (width - rect.right), height + (height - rect.bottom), TRUE);
SetWindowLong(hWnd, GWL_USERDATA, (long)csd);
return hWnd;
}
LRESULT CALLBACK ColourSelectable_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
ColourSelectable_OnPaint(hWnd);
return 0;
case WM_ERASEBKGND:
return 0;
case WM_LBUTTONDOWN:
{
SetCapture(hWnd);
return 0;
}
case WM_LBUTTONUP:
{
ColourSelectable_Data *csd = (ColourSelectable_Data*)GetWindowLong(hWnd, GWL_USERDATA);
RECT me;
POINT pt;
if (GetCapture() != hWnd)
return 0;
ReleaseCapture();
GetClientRect(hWnd, &me);
pt.x = (short)LOWORD(lParam);
pt.y = (short)HIWORD(lParam);
if (!PtInRect(&me, pt))
return 0;
*csd->current = *csd->mine;
RedrawColourBlocks();
return 0;
}
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
void ColourSelectable_OnPaint(HWND hWnd)
{
PAINTSTRUCT ps;
RECT rect;
HDC hDC;
HBITMAP hBM, hBMOld;
BITMAPINFO bmi;
int x, y, height, width;
UINT * ptPixels;
Colour *c;
ColourSelectable_Data *csd;
// Start painting
BeginPaint(hWnd, &ps);
// Find our area to paint
GetClientRect(hWnd, &rect);
// Find the height/width
height = (rect.bottom - rect.top);
width = (rect.right - rect.left);
// Prep bitmap info
ZeroMemory(&bmi,sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
// Off-screen DC
hDC = CreateCompatibleDC(ps.hdc);
hBM = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (void **)&ptPixels, NULL, 0);
hBMOld = (HBITMAP)SelectObject(hDC, hBM);
csd = (ColourSelectable_Data*)GetWindowLong(hWnd, GWL_USERDATA);
c = csd->mine;
// Fill colour
for (x = 0; x < width; x++)
for (y = 0; y < height; y++)
ptPixels[x + y * width] = RealRGB(c->r, c->g, c->b);
// Copy data over to real DC
BitBlt(ps.hdc, rect.left, rect.top, (rect.right - rect.left),
(rect.bottom - rect.top), hDC, rect.left, rect.top, SRCCOPY);
SelectObject(hDC, hBMOld);
DeleteObject(hBM);
DeleteDC(hDC);
// End painting
EndPaint(hWnd, &ps);
}