-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreviewForm.cs
161 lines (132 loc) · 3.95 KB
/
PreviewForm.cs
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
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using static MainLibrary.MainLib;
namespace AutoPixelArt
{
public partial class PreviewForm : Form
{
private bool keepRatio;
public PreviewForm()
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Lang == "ru" ? "ru" : "en-US");
InitializeComponent();
}
private void PreviewForm_Load(object sender, System.EventArgs e)
{
savePicItem.Image = saveUpPicItem.Image = GetIcon("saveIcon");
previewPic.Size = ClientSize;
chromeWidth = Width - ClientSize.Width;
chromeHeight = Height - ClientSize.Height;
constantWidth = previewPic.Width;
constantHeight = previewPic.Height;
keepRatio = true;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.S))
{
SavePicture(false);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
//https://stackoverflow.com/a/10231213/12136394
#region Resizer
private float constantWidth;
private float constantHeight;
private int chromeWidth;
private int chromeHeight;
// From Windows SDK
private const int WM_SIZING = 0x214;
private const int WMSZ_LEFT = 1;
private const int WMSZ_RIGHT = 2;
private const int WMSZ_TOP = 3;
private const int WMSZ_BOTTOM = 6;
struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
protected override void WndProc(ref Message m)
{
if (keepRatio && m.Msg == WM_SIZING)
{
RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
int w = rc.Right - rc.Left - chromeWidth;
int h = rc.Bottom - rc.Top - chromeHeight;
switch (m.WParam.ToInt32()) // Resize handle
{
case WMSZ_LEFT:
case WMSZ_RIGHT:
// Left or right handles, adjust height
rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
break;
case WMSZ_TOP:
case WMSZ_BOTTOM:
// Top or bottom handles, adjust width
rc.Right = rc.Left + chromeWidth + (int)(constantWidth * h / constantHeight);
break;
case WMSZ_LEFT + WMSZ_TOP:
case WMSZ_LEFT + WMSZ_BOTTOM:
// Top-left or bottom-left handles, adjust width
rc.Left = rc.Right - chromeWidth - (int)(constantWidth * h / constantHeight);
break;
case WMSZ_RIGHT + WMSZ_TOP:
// Top-right handle, adjust height
rc.Top = rc.Bottom - chromeHeight - (int)(constantHeight * w / constantWidth);
break;
case WMSZ_RIGHT + WMSZ_BOTTOM:
// Bottom-right handle, adjust height
rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
break;
}
Marshal.StructureToPtr(rc, m.LParam, true);
}
base.WndProc(ref m);
}
#endregion
public void UpdatePicture(Bitmap pic)
{
previewPic.Image = pic;
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
}
public void SetTransparentColor(Color c)
{
TransparencyKey = BackColor = c;
previewPic.BackColor = c;
}
private void PreviewForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.OpenForms["PaletteForm"]?.Dispose();
}
private void savePicItem_Click(object sender, System.EventArgs e)
{
SavePicture(false);
}
private void saveUpPicItem_Click(object sender, System.EventArgs e)
{
SavePicture(true);
}
private void keepRatioItem_Click(object sender, System.EventArgs e)
{
keepRatio = !keepRatio;
keepRatioItem.Checked = keepRatio;
}
}
public class PictureBoxWithInterpolationMode : PictureBox
{
public InterpolationMode InterpolationMode { get; set; }
protected override void OnPaint(PaintEventArgs paintEventArgs)
{
paintEventArgs.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
paintEventArgs.Graphics.InterpolationMode = InterpolationMode;
base.OnPaint(paintEventArgs);
}
}
}