209 lines
5.9 KiB
C#
209 lines
5.9 KiB
C#
using eCIAv2.Library.Devices;
|
||
using eCIAv2.Library.Devices.DIIO;
|
||
using eCIAv2.Library.Devices.Scanner;
|
||
using eCIAv2.WindowsApp.ViewModels;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using System.Runtime.InteropServices;
|
||
using static System.Net.Mime.MediaTypeNames;
|
||
|
||
namespace eCIAv2.WindowsApp
|
||
{
|
||
public partial class MainForm : Form
|
||
{
|
||
#region Custom TitleBar
|
||
|
||
[DllImport("user32.dll")]
|
||
public static extern bool ReleaseCapture();
|
||
|
||
[DllImport("user32.dll")]
|
||
public static extern IntPtr SendMessage(
|
||
IntPtr hWnd, int Msg, int wParam, int lParam);
|
||
|
||
private Panel titleBar;
|
||
|
||
private void CreateCustomTitle()
|
||
{
|
||
this.FormBorderStyle = FormBorderStyle.None;
|
||
this.WindowState = FormWindowState.Maximized;
|
||
this.MinimumSize = new Size(600, 400);
|
||
|
||
titleBar = new Panel();
|
||
titleBar.Height = 36;
|
||
titleBar.Dock = DockStyle.Top;
|
||
titleBar.BackColor = Color.FromArgb(40, 40, 40);
|
||
|
||
titleBar.MouseDown += TitleBar_MouseDown;
|
||
|
||
this.Controls.Add(titleBar);
|
||
|
||
CreateTitleIcon();
|
||
CreateTitleLabel();
|
||
CreateButtons();
|
||
}
|
||
|
||
private void CreateTitleIcon()
|
||
{
|
||
PictureBox icon = new PictureBox();
|
||
|
||
icon.Image = this.Icon.ToBitmap();
|
||
icon.SizeMode = PictureBoxSizeMode.StretchImage;
|
||
icon.Size = new Size(20, 20);
|
||
icon.Location = new Point(8, 8);
|
||
|
||
titleBar.Controls.Add(icon);
|
||
}
|
||
|
||
private void CreateTitleLabel()
|
||
{
|
||
Label lbl = new Label();
|
||
|
||
lbl.Text = this.Text;
|
||
lbl.ForeColor = Color.White;
|
||
lbl.AutoSize = true;
|
||
lbl.Location = new Point(36, 9);
|
||
|
||
titleBar.Controls.Add(lbl);
|
||
}
|
||
|
||
private void CreateButtons()
|
||
{
|
||
Button btnClose = CreateTitleButton("X");
|
||
btnClose.Click += (s, e) => this.Close();
|
||
|
||
Button btnMax = CreateTitleButton("<22><>");
|
||
btnMax.Click += (s, e) =>
|
||
{
|
||
ToggleMaximize();
|
||
};
|
||
|
||
Button btnMin = CreateTitleButton("<22><>");
|
||
btnMin.Click += (s, e) =>
|
||
{
|
||
this.WindowState = FormWindowState.Minimized;
|
||
};
|
||
|
||
titleBar.Controls.Add(btnMin);
|
||
titleBar.Controls.Add(btnMax);
|
||
titleBar.Controls.Add(btnClose);
|
||
}
|
||
|
||
private Button CreateTitleButton(string text)
|
||
{
|
||
Button btn = new Button();
|
||
|
||
btn.Text = text;
|
||
btn.Dock = DockStyle.Right;
|
||
btn.Width = 45;
|
||
btn.FlatStyle = FlatStyle.Flat;
|
||
btn.FlatAppearance.BorderSize = 0;
|
||
btn.ForeColor = Color.White;
|
||
btn.BackColor = Color.FromArgb(40, 40, 40);
|
||
|
||
btn.MouseEnter += (s, e) =>
|
||
{
|
||
btn.BackColor = Color.FromArgb(70, 70, 70);
|
||
};
|
||
|
||
btn.MouseLeave += (s, e) =>
|
||
{
|
||
btn.BackColor = Color.FromArgb(40, 40, 40);
|
||
};
|
||
|
||
return btn;
|
||
}
|
||
|
||
private void TitleBar_DoubleClick(object sender, EventArgs e)
|
||
{
|
||
ToggleMaximize();
|
||
}
|
||
|
||
private void TitleBar_MouseDown(object sender, MouseEventArgs e)
|
||
{
|
||
ReleaseCapture();
|
||
SendMessage(this.Handle, 0xA1, 0x2, 0);
|
||
}
|
||
|
||
private void ToggleMaximize()
|
||
{
|
||
if (this.WindowState == FormWindowState.Normal)
|
||
this.WindowState = FormWindowState.Maximized;
|
||
else
|
||
this.WindowState = FormWindowState.Normal;
|
||
}
|
||
|
||
protected override void WndProc(ref Message m)
|
||
{
|
||
const int HTLEFT = 10;
|
||
const int HTRIGHT = 11;
|
||
const int HTTOP = 12;
|
||
const int HTTOPLEFT = 13;
|
||
const int HTTOPRIGHT = 14;
|
||
const int HTBOTTOM = 15;
|
||
const int HTBOTTOMLEFT = 16;
|
||
const int HTBOTTOMRIGHT = 17;
|
||
|
||
const int WM_NCHITTEST = 0x84;
|
||
|
||
const int resizeArea = 8;
|
||
|
||
if (m.Msg == WM_NCHITTEST)
|
||
{
|
||
base.WndProc(ref m);
|
||
|
||
if ((int)m.Result == 0x01)
|
||
{
|
||
Point pos = this.PointToClient(new Point(m.LParam.ToInt32()));
|
||
|
||
if (pos.X <= resizeArea && pos.Y <= resizeArea)
|
||
m.Result = (IntPtr)HTTOPLEFT;
|
||
|
||
else if (pos.X >= Width - resizeArea && pos.Y <= resizeArea)
|
||
m.Result = (IntPtr)HTTOPRIGHT;
|
||
|
||
else if (pos.X <= resizeArea && pos.Y >= Height - resizeArea)
|
||
m.Result = (IntPtr)HTBOTTOMLEFT;
|
||
|
||
else if (pos.X >= Width - resizeArea && pos.Y >= Height - resizeArea)
|
||
m.Result = (IntPtr)HTBOTTOMRIGHT;
|
||
|
||
else if (pos.X <= resizeArea)
|
||
m.Result = (IntPtr)HTLEFT;
|
||
|
||
else if (pos.X >= Width - resizeArea)
|
||
m.Result = (IntPtr)HTRIGHT;
|
||
|
||
else if (pos.Y <= resizeArea)
|
||
m.Result = (IntPtr)HTTOP;
|
||
|
||
else if (pos.Y >= Height - resizeArea)
|
||
m.Result = (IntPtr)HTBOTTOM;
|
||
}
|
||
|
||
return;
|
||
}
|
||
|
||
base.WndProc(ref m);
|
||
}
|
||
|
||
protected override void OnHandleCreated(EventArgs e)
|
||
{
|
||
base.OnHandleCreated(e);
|
||
|
||
var screen = Screen.FromHandle(this.Handle);
|
||
this.MaximizedBounds = screen.WorkingArea;
|
||
}
|
||
|
||
#endregion
|
||
|
||
private readonly MainFromViewModel _vm;
|
||
|
||
public MainForm(MainFromViewModel vm)
|
||
{
|
||
InitializeComponent();
|
||
CreateCustomTitle();
|
||
|
||
_vm = vm;
|
||
}
|
||
}
|
||
}
|