-
Notifications
You must be signed in to change notification settings - Fork 16
/
ShowQRCodeForm.cs
249 lines (222 loc) · 6.27 KB
/
ShowQRCodeForm.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using KeePass.Plugins;
using KeePassLib.Utility;
namespace KeePassQRCodeView
{
public partial class ShowQRCodeForm : Form
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private const int ScreenPadding = 100;
private readonly Bitmap qrcode;
private readonly string title;
private readonly string field;
public ShowQRCodeForm(IPluginHost host, Bitmap qrcode, string title, string field)
{
Contract.Requires(qrcode != null);
Contract.Requires(title != null);
Contract.Requires(field != null);
InitializeComponent();
this.qrcode = qrcode;
this.title = title.Trim();
this.field = field.Trim();
DoubleBuffered = true;
Text = JoinIfNotEmpty(this.title, this.field);
var screenBounds = Screen.GetBounds(this);
if (screenBounds.Width <= ScreenPadding || screenBounds.Height <= ScreenPadding)
{
screenBounds = new Rectangle(0, 0, 800, 800);
}
var maxSize = Math.Min(Math.Min(screenBounds.Width - ScreenPadding, screenBounds.Height - ScreenPadding), Math.Min(qrcode.Width, qrcode.Height));
ClientSize = new Size(maxSize, maxSize);
BackgroundImageLayout = ImageLayout.Stretch;
BackgroundImage = qrcode;
printToolStripMenuItem.Image = host.Resources.GetObject("B16x16_FilePrint") as Image;
saveAsToolStripMenuItem.Image = host.Resources.GetObject("B16x16_FileSave") as Image;
}
private void ShowQRCodeForm_Click(object sender, EventArgs e)
{
var args = e as MouseEventArgs;
if (args == null || args.Button != MouseButtons.Left)
{
return;
}
Close();
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
var document = new PrintDocument();
document.PrintPage += (s, page) =>
{
var lineHeight = (int)Math.Ceiling(SystemFonts.DefaultFont.GetHeight(page.Graphics));
var descriptionBounds = new Rectangle(
page.MarginBounds.X,
page.MarginBounds.Y,
page.MarginBounds.Width,
lineHeight
);
page.Graphics.DrawString(
JoinIfNotEmpty(title, field),
SystemFonts.DefaultFont,
Brushes.Black,
descriptionBounds,
new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
}
);
var imageBounds = new Rectangle(
page.MarginBounds.X,
page.MarginBounds.Y + lineHeight,
page.MarginBounds.Width,
page.MarginBounds.Height - lineHeight
);
if (qrcode.Width < imageBounds.Width && qrcode.Height < imageBounds.Height)
{
imageBounds = new Rectangle(
imageBounds.X + (int)(imageBounds.Width * 0.5f - qrcode.Width * 0.5f),
imageBounds.Y,
qrcode.Width,
qrcode.Height
);
}
else
{
var scale = Math.Min(
imageBounds.Width / (float)qrcode.Width,
imageBounds.Height / (float)qrcode.Height
);
imageBounds = new Rectangle(
imageBounds.X + (int)(imageBounds.Width * 0.5f - (qrcode.Width * scale) * 0.5f),
imageBounds.Y,
(int)(qrcode.Width * scale),
(int)(qrcode.Height * scale)
);
}
page.Graphics.DrawImage(qrcode, imageBounds);
};
using (var print = new PrintDialog
{
Document = document
})
{
if (print.ShowDialog() == DialogResult.OK)
{
try
{
document.Print();
}
catch (Exception ex)
{
MessageService.ShowWarning(ex);
}
}
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var sfd = new SaveFileDialog
{
Filter = "Images|*.jpg;*.png;*.gif;*.tiff;*.bmp",
DefaultExt = "jpg"
})
{
if (sfd.ShowDialog() == DialogResult.OK)
{
var format = ImageFormat.Jpeg;
switch (Path.GetExtension(sfd.FileName).ToLower())
{
case ".png": format = ImageFormat.Png; break;
case ".gif": format = ImageFormat.Gif; break;
case ".tiff": format = ImageFormat.Tiff; break;
case ".bmp": format = ImageFormat.Bmp; break;
}
try
{
BackgroundImage.Save(sfd.FileName, format);
}
catch (Exception ex)
{
MessageService.ShowWarning(ex);
}
}
}
}
protected override void WndProc(ref Message m)
{
const int WMSZ_LEFT = 1;
const int WMSZ_RIGHT = 2;
const int WMSZ_TOP = 3;
const int WMSZ_TOPLEFT = 4;
const int WMSZ_TOPRIGHT = 5;
const int WMSZ_BOTTOM = 6;
const int WMSZ_BOTTOMLEFT = 7;
const int WMSZ_BOTTOMRIGHT = 8;
if (m.Msg == 0x214 /*WM_MOVING || WM_SIZING*/)
{
var rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
var w = rc.Right - rc.Left;
var h = rc.Bottom - rc.Top;
var z = w > h ? w : h;
switch ((int)m.WParam)
{
case WMSZ_LEFT:
rc.Bottom = rc.Top + w;
rc.Top = rc.Top + (h - (rc.Bottom - rc.Top)) / 2;
break;
case WMSZ_RIGHT:
rc.Bottom = rc.Top + w;
rc.Top = rc.Top + (h - (rc.Bottom - rc.Top)) / 2;
break;
case WMSZ_TOP:
rc.Right = rc.Left + h;
rc.Left = rc.Left + (w - (rc.Right - rc.Left)) / 2;
break;
case WMSZ_TOPLEFT:
rc.Top = rc.Bottom - z;
rc.Left = rc.Right - z;
break;
case WMSZ_TOPRIGHT:
rc.Top = rc.Bottom - z;
rc.Right = rc.Left + z;
break;
case WMSZ_BOTTOM:
rc.Right = rc.Left + h;
rc.Left = rc.Left + (w - (rc.Right - rc.Left)) / 2;
break;
case WMSZ_BOTTOMLEFT:
rc.Bottom = rc.Top + z;
rc.Left = rc.Right - z;
break;
case WMSZ_BOTTOMRIGHT:
rc.Bottom = rc.Top + z;
rc.Right = rc.Left + z;
break;
}
Marshal.StructureToPtr(rc, m.LParam, false);
m.Result = (IntPtr)1;
return;
}
base.WndProc(ref m);
}
private static string JoinIfNotEmpty(params string[] param)
{
return string.Join(" - ", param.Where(s => !string.IsNullOrEmpty(s)));
}
}
}