| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Plugins.Vpp.Views
- {
- /// <summary>
- /// UCDispVpp.xaml 的交互逻辑
- /// </summary>
- public partial class UCDispVpp : UserControl
- {
- public UCDispVpp()
- {
- InitializeComponent();
- }
- #region 属性
- int[] _RowColNumber = new int[2];
- internal object obj = new object();
- public int[] RowColNumber
- {
- get { return _RowColNumber; }
- set
- {
- lock (obj)
- {
- AddHalconCv(value[0], value[1]);
- _RowColNumber = value;
- }
- }
- }
- private int DoubleVppNumber = -1;
- private VppWindow DoubleWindow = null;
- public List<VppWindow> vppWindow = new List<VppWindow>();
- private static UCDispVpp _instance = new UCDispVpp();
- public static UCDispVpp Instance
- {
- get { return _instance; }
- }
- #endregion
- #region 方法
- /// <summary>
- /// 添加Vpp窗体控件:
- /// - 按 GetColRow 最优行列布局,但只创建 num 个窗体(不补空白格);
- /// - 数量除不尽时,最后一个窗体直接跨列填满本行剩余位置。
- /// </summary>
- /// <param name="num">画面数量</param>
- /// <param name="array">排列方式:1=水平方向优先,2=垂直方向优先</param>
- public void AddHalconCv(int num, int array)
- {
- try
- {
- UCDispVpp.Instance.vppWindow.Clear();
- Task.Delay(100).Wait();
- UCDispVpp.Instance.GetColRow(num, out int row, out int col);
- if (array == 2) (row, col) = (col, row); // 垂直方向优先:交换行列倾向
- foreach (var item in UCDispVpp.Instance.vppWindow)
- {
- Application.Current.Dispatcher.Invoke(() =>
- {
- item.MouseDouble -= UCDispVpp_MouseDoubleClick;
- });
- }
- if (DoubleVppNumber >=0&& DoubleWindow!=null)
- Application.Current.Dispatcher.Invoke(() => UCDispVppChildren_MouseDoubleClick(DoubleWindow, DoubleVppNumber));
- Application.Current.Dispatcher.Invoke(() => UCDispVpp.Instance.VppGrid.Children.Clear());
- Application.Current.Dispatcher.Invoke(new Action(() =>
- {
- // 重建等分行列定义
- var grid = UCDispVpp.Instance.VppGrid;
- grid.RowDefinitions.Clear();
- grid.ColumnDefinitions.Clear();
- for (int r = 0; r < row; r++)
- grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
- for (int c = 0; c < col; c++)
- grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- }));
- Task.Delay(100).Wait();
- for (int i = 0; i < num; i++)
- {
- Application.Current.Dispatcher.Invoke(() =>
- {
- UCDispVpp.Instance.vppWindow.Add(new VppWindow(i + 1));
- });
- Task.Delay(100).Wait();
- }
- for (int i = 0; i < num; i++)
- {
- Application.Current.Dispatcher.Invoke(() =>
- {
- var w = UCDispVpp.Instance.vppWindow[i];
- int r, c;
- if (array == 2)
- {
- // 垂直优先:按列顺序填充(先填满一列再换下一列)
- c = i / row;
- r = i % row;
- // 数量除不尽时:最后一个窗体跨行填满本列剩余位置(垂直合页)
- if (i == num - 1 && row - r > 1)
- Grid.SetRowSpan(w, row - r);
- }
- else
- {
- // 水平优先:按行顺序填充(先填满一行再换下一行)
- r = i / col;
- c = i % col;
- // 数量除不尽时:最后一个窗体跨列填满本行剩余位置(水平合页,不补空白格)
- if (i == num - 1 && col - c > 1)
- Grid.SetColumnSpan(w, col - c);
- }
- Grid.SetRow(w, r);
- Grid.SetColumn(w, c);
- UCDispVpp.Instance.VppGrid.Children.Add(w);
- w.MouseDouble += UCDispVpp_MouseDoubleClick;
- });
- Task.Delay(100).Wait();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private void UCDispVpp_MouseDoubleClick(VppWindow window, int VppNumber)
- {
- DoubleVppNumber = VppNumber;
- DoubleWindow=window;
- window.MouseDouble -= UCDispVpp_MouseDoubleClick;
- VppGrid.Visibility = Visibility.Collapsed;
- UCDispVpp.Instance.VppGrid.Children.Remove(window);
- MaxVppGrid.Children.Clear();
- MaxVppGrid.Children.Add(window);
- MaxVppGrid.Visibility = Visibility.Visible;
- window.SetStatusBarVisible(true); // enlarge: show coordinate status bar
- window.MouseDouble += UCDispVppChildren_MouseDoubleClick;
- }
- private void UCDispVppChildren_MouseDoubleClick(VppWindow window, int VppNumber)
- {
- MaxVppGrid.Children.Clear();
- MaxVppGrid.Visibility = Visibility.Collapsed;
- //UCDispVpp.Instance.VppGrid.Children.Add(window);
- UCDispVpp.Instance.VppGrid.Children.Insert(VppNumber - 1, window);
- window.MouseDouble -= UCDispVppChildren_MouseDoubleClick;
- window.MouseDouble += UCDispVpp_MouseDoubleClick;
- VppGrid.Visibility = Visibility.Visible;
- window.SetStatusBarVisible(false); // restore grid preview: hide coordinate status bar
- DoubleVppNumber = -1;
- DoubleWindow = null;
- }
- #endregion
- #region 计算行列
- /// <summary>
- /// 查询能被整除的最大数
- /// </summary>
- /// <param name="target"></param>
- /// <returns></returns>
- public static int FindLargestDivisor(int target)
- {
- int max = target; // 初始最大数为目标数本身
- List<int> list = new List<int>();
- for (int i = 1; i < target; i++)
- {
- if (target % i == 0)
- {
- list.Add(i);
- //max = i;
- }
- }
- max = list[list.Count / 2];
- return max;
- }
- /// <summary>
- /// 计算出最优行数和列数
- /// </summary>
- /// <param name="target">总计数量</param>
- /// <param name="col">列数</param>
- /// <param name="row">行数</param>
- public void GetColRow(int target, out int row, out int col)
- {
- if (target <= 2)
- {
- row = target > 0 ? target : 1;
- col = 1;
- return;
- }
- col = FindLargestDivisor(target);
- Console.WriteLine("1:" + col);
- if (col == 1)
- {
- col = FindLargestDivisor(target + 1);
- row = (target + 1) / col;
- }
- else
- row = target / col;
- }
- #endregion
- }
- }
|