using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PaintDotNet.ImageCollect; using PaintDotNet.Base.SettingModel; using PaintDotNet.Camera; using PaintDotNet.Base.CommTool; using System.IO; using Metis.Setting; using PaintDotNet.Setting; using System.Runtime.InteropServices; using System.Threading; namespace PaintDotNet.Preview2 { internal partial class Preview2SettingDialog : FloatingToolForm { private ExposureSbControl _exposureControl; private WhiteBalanceControl _wbControl; private GrayModeControl _grayModeControl; private SaturationControl _saturationControl; private FrameControl _frameControl; private OtherControl _otherControl; private RotateControl _rotateControl; private DisplayControl _displayControl; private AppWorkspace _app; private ICamera m_carmera => CameraManager.CurrentCamera; internal Preview2SettingDialog(DocumentWorkspaceWindow doc) { InitializeComponent(); InitializeLanguageText(); _app = (AppWorkspace)doc.AppWorkspaceTop; toolkitEditControlL.Initialize((AppWorkspace)doc.AppWorkspaceTop, Preview2Context.ToolbarModelLeft); toolkitEditControlR.Initialize((AppWorkspace)doc.AppWorkspaceTop, Preview2Context.ToolbarModelRight); foreach (TabPage item in metroTabControl1.TabPages) { // item.Padding = new Padding(2); // item.BackColor = Color.Transparent; } InitializePageCamera(); InitializeImages(); InitializePageImage(); InitializePreview2Config(); this.Load += Preview2SettingDialog_Load; } private void InitializeLanguageText() { this.btnCamRefresh.Text = PdnResources.GetString("Preview2Setting.SaveCurrentConfig"); this.btnSave.Text = PdnResources.GetString("Preview2Setting.SaveCurrentConfig"); this.btnAdd.Text = PdnResources.GetString("Preview2Setting.AddAndSaveConfig"); this.btnDel.Text = PdnResources.GetString("Preview2Setting.DeleteCurrentConfig"); this.label2.Text = PdnResources.GetString("Preview2Setting.SelectConfig") + ":"; this.label1.Text = PdnResources.GetString("Preview2Setting.SelectCamera") + ":"; this.tabPageCamera.Text = PdnResources.GetString("Menu.ImageCollection.CameraSetting.Text"); this.tabPageImage.Text = PdnResources.GetString("Preview2Setting.ImgSettings"); this.tbgBase.Text = PdnResources.GetString("Menu.BasicSettings.text"); this.groupBox2.Text = PdnResources.GetString("Menu.Tools.GridLine.Text"); this.btnCross.Text = PdnResources.GetString("Menu.imagecapture.Previewwindow.reticle.text"); this.btnCircle.Text = PdnResources.GetString("Menu.circular.Text"); this.btnRect.Text = PdnResources.GetString("Menu.LabelAction.DrawRectangle.Text"); this.btnGrid.Text = PdnResources.GetString("Menu.Tools.GridLine.Text"); this.groupBox1.Text = PdnResources.GetString("Menu.BasicSettings.text"); this.btnGridSet.Text = PdnResources.GetString("Menu.Setting.GridSetting.Text"); this.btnRulerSet.Text = PdnResources.GetString("Menu.Setting.RuleSetting.Text"); this.label3.Text = PdnResources.GetString("Preview2Setting.CameraAlias") + ":"; this.ckbCapFocus.Text = PdnResources.GetString("Preview2Setting.AuxiliaryFocusing"); this.ckbCapRuler.Text = PdnResources.GetString("Menu.imagecapture.Previewwindow.Shootingruler.text"); this.ckbCapClose.Text = PdnResources.GetString("Preview2Setting.ClosePictures"); this.tabPage4.Text = PdnResources.GetString("Preview2Setting.Sidebar"); this.groupBox4.Text = PdnResources.GetString("Preview2Setting.SidebarPosition"); this.rdbR.Text = PdnResources.GetString("Preview2Setting.Right"); this.rdbL.Text = PdnResources.GetString("Preview2Setting.Left"); this.label5.Text = PdnResources.GetString("Preview2Setting.Left"); this.label4.Text = PdnResources.GetString("Preview2Setting.SidebarDisplay") + ":"; this.ckbR.Text = PdnResources.GetString("Preview2Setting.Right"); this.ckbL.Text = PdnResources.GetString("Preview2Setting.Left"); this.Text = PdnResources.GetString("Preview2Setting.PreviewConfig"); } void InitializePageCamera() { var pg = tabPageCamera; _exposureControl = new ExposureSbControl(); _exposureControl.Dock = DockStyle.Top; _wbControl = new WhiteBalanceControl(); _wbControl.Dock = DockStyle.Top; _grayModeControl = new GrayModeControl(); _grayModeControl.Dock = DockStyle.Top; _saturationControl = new SaturationControl(); _saturationControl.Dock = DockStyle.Top; _frameControl = new FrameControl(); _frameControl.Dock = DockStyle.Top; pg.Controls.Add(_frameControl); pg.Controls.Add(_saturationControl); pg.Controls.Add(_wbControl); pg.Controls.Add(_grayModeControl); pg.Controls.Add(_exposureControl); } void InitializePageImage() { _rotateControl = new RotateControl(); _rotateControl.Dock = DockStyle.Top; _rotateControl.OnRotated += () => { _frameControl?.Transfor(); }; _otherControl = new OtherControl(); _otherControl.Dock = DockStyle.Top; _displayControl = new DisplayControl(); _displayControl.Dock = DockStyle.Top; tabPageImage.Controls.Add(_displayControl); tabPageImage.Controls.Add(_otherControl); tabPageImage.Controls.Add(_rotateControl); } void InitializeImages() { btnAdd.Image = PdnResources.GetImageResource("Icons.p2add.png").Reference; btnDel.Image = PdnResources.GetImageResource("Icons.p2delet.png").Reference; btnSave.Image = PdnResources.GetImageResource("Icons.p2save.png").Reference; btnCamRefresh.Image = PdnResources.GetImageResource("Icons.p2camrefresh.png").Reference; } string _cameraNameEx; void InitializePreview2Config() { ckbCapClose.Checked = Preview2Context.P2Config.CaptureClose; ckbCapFocus.Checked = Preview2Context.P2Config.CaptureFocus; ckbCapRuler.Checked = Preview2Context.P2Config.CaptureRuler; ckbL.Checked = (Preview2Context.P2Config.ToolkitSide & 1) > 0; ckbR.Checked = (Preview2Context.P2Config.ToolkitSide & 2) > 0; BtnCheckChange(btnGrid, Preview2Context.P2Config.DrawGrid); BtnCheckChange(btnRect, Preview2Context.P2Config.DrawRect); BtnCheckChange(btnCircle, Preview2Context.P2Config.DrawCircle); BtnCheckChange(btnCross, Preview2Context.P2Config.DrawCross); } private void Preview2SettingDialog_Load(object sender, EventArgs e) { TopMost = false; List fileNames = CameraConfigs.GetInstance().GetAllConfigFiles(); cmbConfig.Items.AddRange(fileNames.ToArray()); UpdateCamList(); InitCameraSettings(); timer1.Enabled = true; this.cmbConfig.SelectedIndexChanged += new System.EventHandler(this.cmbConfig_SelectedIndexChanged); OnCameraNameChanged(); UpdateConfigOnCameraChanged(); ConfigModel.OnCameraNameExChanged += OnCameraNameChanged; CameraManager.OnCameraDrop += OnCameraDrop; } private void OnCameraDrop() { UpdateCamList(); } private void OnCameraNameChanged() { txbNameEx.Text = m_carmera.GetExofName(); } private void InitCameraSettings() { _exposureControl.Initialize(); _wbControl.Initialize(); _saturationControl.Initialize(); _frameControl.Initialize(); _rotateControl.Initialize(); _otherControl.Initialize(); _grayModeControl.Initialize(); } private void UpdateCameraSettings() { _exposureControl.UpdateSettings(); _wbControl.Initialize(); _saturationControl.UpdateSettings(); _frameControl.UpdateSettings(); _rotateControl.Initialize(); _otherControl.UpdateSettings(); _grayModeControl.Initialize(); } private void timer1_Tick(object sender, EventArgs e) { metroTabControl1.Enabled = CameraManager.IsLive; if (!CameraManager.IsLive) return; _exposureControl.UpdateDisplay(); _wbControl.UpdateDisplay(); var isgay = CameraConfigs.Settings.Monochromatic == 0; _wbControl.Enabled = isgay; _saturationControl.Enabled = isgay; btnAdd.Enabled = !cmbConfig.Items.Contains(cmbConfig.Text); btnDel.Enabled = cmbConfig.Items.Contains(cmbConfig.Text) && cmbConfig.Text != "CameraParam"; } #region Draw Grid public static event Action OnDrawGridChanged; private void BtnCheckChange(Button btn, bool b) { btn.FlatStyle = FlatStyle.Flat; btn.BackColor = b ? Color.FromArgb(0, 174, 219) : SystemColors.Control; } private void btnGrid_Click(object sender, EventArgs e) { Preview2Context.P2Config.DrawGrid = !Preview2Context.P2Config.DrawGrid; BtnCheckChange(btnGrid, Preview2Context.P2Config.DrawGrid); OnDrawGridChanged?.Invoke(); } private void btnRect_Click(object sender, EventArgs e) { Preview2Context.P2Config.DrawRect = !Preview2Context.P2Config.DrawRect; BtnCheckChange(btnRect, Preview2Context.P2Config.DrawRect); OnDrawGridChanged?.Invoke(); } private void btnCircle_Click(object sender, EventArgs e) { Preview2Context.P2Config.DrawCircle = !Preview2Context.P2Config.DrawCircle; BtnCheckChange(btnCircle, Preview2Context.P2Config.DrawCircle); OnDrawGridChanged?.Invoke(); } private void btnCross_Click(object sender, EventArgs e) { Preview2Context.P2Config.DrawCross = !Preview2Context.P2Config.DrawCross; BtnCheckChange(btnCross, Preview2Context.P2Config.DrawCross); OnDrawGridChanged?.Invoke(); } #endregion #region Config operate private void cmbConfig_SelectedIndexChanged(object sender, EventArgs e) { var name = cmbConfig.Text; var cpm = CameraConfigs.GetInstance().GetCameraParamModel(name); OnConfigModify?.Invoke(0, name); Startup.instance.cameraParamModel = cpm; Startup.instance.configModel.CameraConfig = name; SaveConfigModel(); CameraConfigs.GetInstance().CameraParamInit(); UpdateCameraSettings(); Preview2Context.P2Config.SetCamCfg(m_carmera, (string)cmbConfig.SelectedItem); } private void btnSave_Click(object sender, EventArgs e) { CameraConfigs.GetInstance().Save(); } /// /// 参数1[ int]:0选择,1添加,2删除 参数2[string]:变更参数名 /// public Action OnConfigModify; private void btnAdd_Click(object sender, EventArgs e) { btnAdd.Enabled = false; CameraConfigs.GetInstance().SaveAs(cmbConfig.Text); cmbConfig.Items.Add(cmbConfig.Text); cmbConfig.Refresh(); OnConfigModify?.Invoke(1, cmbConfig.Text); } private void btnDel_Click(object sender, EventArgs e) { var name = cmbConfig.Text; if (name == "CameraParam") return; OnConfigModify?.Invoke(2, name); CameraConfigs.GetInstance().Delete(name); cmbConfig.Items.Remove(name); cmbConfig.SelectedItem = "CameraParam"; } #endregion private void cmbConfig_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (cmbConfig.Items.Contains(cmbConfig.Text)) return; btnAdd.Enabled = true; } } private void SaveConfigModel() { string configXml = XmlSerializeHelper.XmlSerialize(Startup.instance.configModel); string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\Config.xml"; FileOperationHelper.WriteStringToFile(configXml, filePath, FileMode.Create); } private void Preview2SettingDialog_FormClosing(object sender, FormClosingEventArgs e) { Preview2Context.SaveP2Config(); SaveConfigModel(); ConfigModel.OnCameraNameExChanged -= OnCameraNameChanged; CameraManager.OnCameraDrop -= OnCameraDrop; } private void ckbCapClose_Click(object sender, EventArgs e) { Preview2Context.P2Config.CaptureClose = ckbCapClose.Checked; } private void ckbCapRuler_Click(object sender, EventArgs e) { Preview2Context.P2Config.CaptureRuler = ckbCapRuler.Checked; } private void ckbCapFocus_Click(object sender, EventArgs e) { Preview2Context.P2Config.CaptureFocus = ckbCapFocus.Checked; } public static event Action OnToolkitSideChanged; private void ToolkitsideChanged_Click(object sender, EventArgs e) { var value = 0; if (ckbL.Checked) { value += 1; } if (ckbR.Checked) { value += 2; } Preview2Context.P2Config.ToolkitSide = value; OnToolkitSideChanged?.Invoke(); } private void txbNameEx_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { m_carmera.SetExofName(txbNameEx.Text); ConfigModel.OnCameraNameExChanged.Invoke(); } } private void cmbCamera_SelectedIndexChanged(object sender, EventArgs e) { try { CameraManager.GetInstance().SetCurrentCamera(cmbCamera.SelectedIndex); ConfigModel.OnCameraNameExChanged.Invoke(); UpdateConfigOnCameraChanged(); if (!CameraManager.CurrentCamera.IsOpen()) throw new Exception(); } catch { MessageBox.Show("相机已失效,请检查相机连接。"); } } private void UpdateConfigOnCameraChanged() { var name = Preview2Context.P2Config.GetCamCfg(m_carmera); if (cmbConfig.Items.Contains(name)) cmbConfig.SelectedItem = name; else cmbConfig.SelectedItem = CommonDefine.DefaultCamCfg; } private void ToolkitEditSelect_Click(object sender, EventArgs e) { toolkitEditControlL.Visible = rdbL.Checked; toolkitEditControlR.Visible = rdbR.Checked; } /// /// 跟随父窗体移动 /// internal void FormMove(int x, int y) { x = this.Location.X + x; y = this.Location.Y + y; x = Math.Max(0, x); y = Math.Max(0, y); var screen = Screen.GetWorkingArea(this); if (x + Width > screen.Width) x = screen.Width - Width; if (y + Height > screen.Height) y = screen.Height - Height; Location = new Point(x, y); } private void btnGridSet_Click(object sender, EventArgs e) { using (GridSettingForm af = new GridSettingForm(_app)) { af.StartPosition = FormStartPosition.CenterScreen; af.ShowDialog(_app); } } private void btnRulerSet_Click(object sender, EventArgs e) { using (RulerSettingDialog af = new RulerSettingDialog(_app)) { af.StartPosition = FormStartPosition.CenterScreen; af.ShowDialog(_app); } } #region 防止 private Point _mouseDownPos; private bool _move; protected override void WndProc(ref Message m) { RECT nativeRect; switch (m.Msg) { case 0x20: int lp = m.LParam.ToInt32(); if ((lp & 0xFFFF) == 2 && ((lp >> 0x10) & 0xFFFF) == 0x201) { _mouseDownPos = Control.MousePosition; _move = true; } break; case 0x231: if (_move) { Rectangle rect = Screen.GetWorkingArea(this); nativeRect = new RECT( _mouseDownPos.X - Location.X, _mouseDownPos.Y - Location.Y, rect.Right - (Bounds.Right - _mouseDownPos.X), rect.Bottom - (Bounds.Bottom - _mouseDownPos.Y)); ClipCursor(ref nativeRect); } break; case 0x0232: if (_move) { nativeRect = new RECT(Screen.GetWorkingArea(this)); //ClipCursor(ref nativeRect); _move = false; } break; } base.WndProc(ref m); } [DllImport("user32.dll")] public static extern bool ClipCursor(ref RECT lpRect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; public RECT(int left, int top, int right, int bottom) { Left = left; Top = top; Right = right; Bottom = bottom; } public RECT(Rectangle rect) { Left = rect.Left; Top = rect.Top; Right = rect.Right; Bottom = rect.Bottom; } public Rectangle Rect { get { return new Rectangle( Left, Top, Right - Left, Bottom - Top); } } public Size Size { get { return new Size(Right - Left, Bottom - Top); } } public static RECT FromXYWH(int x, int y, int width, int height) { return new RECT(x, y, x + width, y + height); } public static RECT FromRectangle(Rectangle rect) { return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom); } } #endregion private void btnCamRefresh_Click(object sender, EventArgs e) { new MessageDailog() { Title = "重载相机列表", Message = "重新连接相机程序将进入挂起状态,持续10s左右,是否继续?", Message1 = "正在重连相机请稍后...", Handler = () => { CameraManager.GetInstance().UnInitManager(); CameraManager.GetInstance().InitManager(); btnCamRefresh.Enabled = true; UpdateCamList(); } }.ShowDialog(); } private void UpdateCamList() { cmbCamera.Items.Clear(); cmbCamera.Items.AddRange(CameraManager.GetInstance().GetCameraNameList().ToArray()); var str = m_carmera.GetName(); if (!string.IsNullOrWhiteSpace(str)) cmbCamera.SelectedItem = str; } } }