using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; using Katteker.Gui.Properties; namespace Katteker.Gui { public sealed partial class UpdateWindow : Form { private const int HTCAPTION = 0x2; private const int WM_NCLBUTTONDOWN = 0xA1; /// /// The Update Window /// public UpdateWindow() { Font = SystemFonts.MessageBoxFont; Application.EnableVisualStyles(); if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware(); InitializeComponent(); } private string Changelog => UpdateManager.ChangelogFilename; /// /// Last release entry. /// public ReleaseEntry ReleaseEntry { get; set; } /// /// Update manager /// public UpdateManager UpdateManager { get; set; } 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) return; ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } private async void UpdateWindow_Load(object sender, EventArgs e) { var changelog = await ChangelogHelper.LoadChangelogAsync(Changelog, PublishPath).ConfigureAwait(false); Invoke(new Action(() => changelogBrowser.DocumentText = changelog)); if (ReleaseEntry == null) { Invoke(new Action(() => { WriteTitle(Resources.No_update_available); })); } else { var latest = ReleaseEntry.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); } catch (Exception ex) { WriteTitle(ex.Message); } finally { Invoke(new Action(() => { progressBar1.Visible = false; updBtn.Visible = false; var messResult = MessageBox.Show( Resources.Installed_new_Version, Resources.New_Version, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (messResult == DialogResult.Yes) { DialogResult = DialogResult.OK; Close(); } })); } } private void WriteTitle(string text) { Invoke(new Action(() => { Text = text; titlebar.Text = text; })); } } }