-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.c
127 lines (101 loc) · 2.49 KB
/
preview.c
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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>
#include <assert.h>
#include "thetisskinmaker.h"
#include "resources.h"
#define CLASS_NAME L"Win32ThetisSkinMakerPreview"
#define IMAGE_WIDTH 450
size_t bitmapWidth = 0, bitmapHeight = 0;
static void
OnPreviewWindowCreate(HWND hwnd)
{
HINSTANCE instance;
HWND image;
instance = GetModuleHandle(NULL);
image = CreateWindowExW(0,
WC_STATICW,
L"",
WS_CHILD | WS_VISIBLE | SS_BITMAP | SS_REALSIZECONTROL,
0,
0,
bitmapWidth,
bitmapHeight,
hwnd,
(HMENU) IDC_PREVIEW_STATIC_BITMAP,
instance,
NULL);
CHECK(image);
SendMessage(image, STM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) imageBitmap);
}
static LRESULT CALLBACK
PreviewWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_CREATE:
OnPreviewWindowCreate(hwnd);
break;
case WM_SIZE:
{
HWND bitmapStatic;
bitmapStatic = GetDlgItem(hwnd, IDC_PREVIEW_STATIC_BITMAP);
assert(bitmapStatic != NULL);
SetWindowPos(bitmapStatic,
NULL,
0,
0,
LOWORD(lparam),
HIWORD(lparam),
0);
}
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
void
OnPreviewButtonClick(HWND hwnd)
{
HINSTANCE instance;
WNDCLASSW wc = {0};
BITMAP bitmap;
HWND previewWindow;
RECT rect = {0};
instance = GetModuleHandle(NULL);
assert(imageBitmap != NULL);
if(!GetObject(imageBitmap, sizeof(bitmap), &bitmap))
{
ERROR_BOX(L"Failed to get object for bitmap!");
return;
}
/* the whole window needs to be repainted */
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = PreviewWndProc;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
RegisterClassW(&wc);
float ratio = (float)IMAGE_WIDTH / bitmap.bmWidth;
float new_height = ((float) bitmap.bmHeight) * ratio;
rect.right = IMAGE_WIDTH;
rect.bottom = (int) new_height;
/* get the normal size of the window, including window decorations */
AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, FALSE, 0);
bitmapWidth = rect.right - rect.left;
bitmapHeight = rect.bottom - rect.top;
previewWindow = CreateWindowExW(0,
CLASS_NAME,
L"Image preview",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
bitmapWidth,
bitmapHeight,
hwnd,
NULL,
instance,
NULL);
CHECK(previewWindow);
ShowWindow(previewWindow, SW_NORMAL);
}