using System; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; using Katteker.Gui.Properties; namespace Katteker.Gui { public sealed partial class UpdateWindow : Form { private readonly UpdateManager _updateManager; private readonly ReleaseEntry _entry; private const int HTCAPTION = 0x2; private const int WM_NCLBUTTONDOWN = 0xA1; /// /// The Update Window /// public UpdateWindow(UpdateManager updateManager, ReleaseEntry entry) { _updateManager = updateManager; _entry = entry; Font = SystemFonts.MessageBoxFont; Application.EnableVisualStyles(); if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware(); InitializeComponent(); } private string Changelog => _updateManager.ChangelogFilename; private string PublishPath => _updateManager.UrlOrPath; /// /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing) { components?.Dispose(); } base.Dispose(disposing); } [DllImport("User32.dll")] private static extern bool ReleaseCapture(); [DllImport("User32.dll")] private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); private void CloseBtn_Click(object sender, EventArgs e) { Close(); } private void MaximizeBtn_Click(object sender, EventArgs e) { WindowState = WindowState != FormWindowState.Maximized ? FormWindowState.Maximized : FormWindowState.Normal; } private void MinimizeBtn_Click(object sender, EventArgs e) { WindowState = FormWindowState.Minimized; } private void Titlebar_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && e.Clicks >= 2) { MaximizeBtn_Click(sender, e); return; } if (e.Button != MouseButtons.Left) return; ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } private void UpdateWindow_Activated(object sender, EventArgs e) { titleLayoutPanel.BackColor = SystemColors.ControlDarkDark; titleLayoutPanel.ForeColor = SystemColors.Control; } private void UpdateWindow_Deactivate(object sender, EventArgs e) { titleLayoutPanel.BackColor = SystemColors.Control; titleLayoutPanel.ForeColor = SystemColors.ControlText; } private async void UpdateWindow_Load(object sender, EventArgs e) { var changelogContent = await ChangelogHelper.LoadChangelogAsync(Changelog, PublishPath).ConfigureAwait(false); changelogContent = changelogContent.ChangelogAsHtml(Path.GetExtension(Changelog)); Invoke(new Action(() => changelogBrowser.DocumentText = changelogContent)); if (_entry == null) { Invoke(new Action(() => { WriteTitle(Resources.No_update_available); })); } else { var latest = _entry.Version; Invoke(new Action(() => { WriteTitle(Resources.You_can_update_to_Version + latest); updBtn.Visible = true; })); } } private async void UpdBtn_Click(object sender, EventArgs e) { Invoke(new Action(() => progressBar1.Visible = true)); try { var progress = new Progress(x => Invoke(new Action(() => { progressBar1.Value = x; }))); await _updateManager.UpdateAppAsync(progress).ConfigureAwait(false); WriteTitle(Resources.You_re_up_to_date); var messResult = MessageBox.Show( Resources.Installed_new_Version, Resources.New_Version, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (messResult == DialogResult.Yes) { DialogResult = DialogResult.OK; Close(); } } catch (Exception ex) { MessageBox.Show(Resources.CouldNotUpdateYourApplication + Environment.NewLine + ex.Message, Gui.Properties.Resources.Updater, MessageBoxButtons.OK, MessageBoxIcon.Error); WriteTitle(Resources.Updater); } finally { Invoke(new Action(() => { progressBar1.Visible = false; updBtn.Visible = false; })); } } private void WriteTitle(string text) { Invoke(new Action(() => { Text = text; titlebar.Text = text; })); } } }