-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
161 lines (124 loc) · 4.82 KB
/
Main.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
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
// Main.cpp
#include "SVGDocuments.h" // Đảm bảo bạn có lớp SVGDocuments để xử lý SVG
#include <gdiplus.h>
// Biến toàn cục
float rotationAngle = 30.0f; // Góc xoay cố định
bool hasRendered = false; // Cờ để xác định đã render hay chưa
int windowWidth = 800; // Chiều rộng cửa sổ mặc định
int windowHeight = 600; // Chiều cao cửa sổ mặc định
std::string FileName = "svg-01.svg"; // Path
// Hàm thực hiện phép xoay đồ họa
void RotateGraphics(Gdiplus::Graphics& graphics, float angle, int width, int height) {
float centerX = width / 2;
float centerY = height / 2;
// Tạo đối tượng Matrix cho phép xoay
Gdiplus::Matrix matrix;
// Xoay quanh điểm trung tâm
matrix.RotateAt(angle, Gdiplus::PointF(centerX, centerY));
// Áp dụng phép xoay
graphics.SetTransform(&matrix);
}
// Hàm scale đồ họa để phù hợp với kích thước cửa sổ
void ScaleGraphics(Gdiplus::Graphics& graphics, int originalWidth, int originalHeight, int newWidth, int newHeight) {
float scaleX = static_cast<float>(newWidth) / originalWidth;
float scaleY = static_cast<float>(newHeight) / originalHeight;
// Áp dụng scale theo tỷ lệ
graphics.ScaleTransform(scaleX, scaleY);
}
// Hàm xử lý vẽ lên cửa sổ
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static SVGDocuments svg; // Đối tượng SVGDocuments toàn cục
switch (uMsg) {
case WM_PAINT: {
// Khởi tạo Graphics object
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT window;
GetWindowRect(hwnd, &window);
// Khởi tạo GDI+ Graphics
Gdiplus::Graphics graphics(hdc);
// Xóa màn hình để tránh nhấp nháy
graphics.Clear(Gdiplus::Color(255, 255, 255)); // Màu nền trắng (RGB)
// Tính toán scale và rotation
RotateGraphics(graphics, rotationAngle, windowWidth, windowHeight);
ScaleGraphics(graphics, 800, 600, windowWidth, windowHeight);
Viewbox* viewbox = new Viewbox();
// Tải và render file SVG
if (!hasRendered) { // Tải SVG lần đầu
svg.loadFile(FileName, viewbox); // Đảm bảo đường dẫn chính xác
hasRendered = true;
}
viewbox->render(graphics, window);
// Render SVG với các transform áp dụng
svg.render(graphics);
delete viewbox;
EndPaint(hwnd, &ps);
return 0;
}
case WM_SIZE: {
// Cập nhật kích thước cửa sổ
RECT rect;
GetClientRect(hwnd, &rect);
windowWidth = rect.right - rect.left;
windowHeight = rect.bottom - rect.top;
// Yêu cầu vẽ lại
InvalidateRect(hwnd, NULL, TRUE);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int main(int argc, char* argv[]) {
try {
// Handle command-line argument for SVG file path
if (argc > 1) {
FileName = argv[1]; // Use the argument passed
}
std::cout << "Loading SVG from: " << FileName << std::endl;
// Initialize GDI+
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
// Define window class
const wchar_t* className = L"SVGWindowClass";
WNDCLASSW wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = className;
// Register window class
if (!RegisterClassW(&wc)) {
MessageBoxW(NULL, L"Window class registration failed!", L"Error", MB_OK | MB_ICONERROR);
return 0;
}
// Create the window
HWND hwnd = CreateWindowExW(0, className, L"SVG Renderer", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, wc.hInstance, NULL);
if (hwnd == NULL) {
MessageBoxW(NULL, L"CreateWindowEx failed!", L"Error", MB_OK | MB_ICONERROR);
return 0;
}
// Show the window
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
// Main event loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Shutdown GDI+
Gdiplus::GdiplusShutdown(gdiplusToken);
}
catch (const std::exception& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
}
catch (...) {
MessageBoxW(NULL, L"An unknown error occurred!", L"Error", MB_OK | MB_ICONERROR);
return -1;
}
return 0;
}