You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
8.1 KiB
C#

using DevExpress.XtraEditors.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WellWorkDataUI.CustomControls
{
public partial class frmExportWells : DevExpress.XtraEditors.XtraForm
{
public frmExportWells(List<string> wells)
{
this.InitializeComponent();
// 启用双缓冲,减少闪烁
this.SetDoubleBuffered(this.flowWellsPanel);
this.SetDoubleBuffered(this); // 也可以为整个窗体启用双缓冲
this.flowWellsPanel.Padding = new Padding(10);
// 添加 CheckBox 控件
foreach (var well in wells)
{
CheckBox check = new CheckBox();
check.Text = well;
check.BackColor = Color.Transparent;
check.Margin = new Padding(10);
this.flowWellsPanel.Controls.Add(check);
}
// 鼠标选择区域的事件
this.flowWellsPanel.MouseDown += this.FlowPanel_MouseDown;
this.flowWellsPanel.MouseMove += this.FlowPanel_MouseMove;
this.flowWellsPanel.MouseUp += this.FlowPanel_MouseUp;
// 确保 FlowLayoutPanel 支持绘制
this.flowWellsPanel.Paint += this.FlowLayoutPanel_Paint;
}
public List<string> SelectedWells = new List<string>();
private bool isSelecting = false;
private Point selectionStart;
private Rectangle selectionRect = Rectangle.Empty;
// MouseDown 事件
private void FlowPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.isSelecting = true;
this.selectionStart = e.Location;
this.selectionRect = new Rectangle(e.Location, Size.Empty);
this.flowWellsPanel.Invalidate(); // 强制重绘 FlowLayoutPanel
}
}
// MouseMove 事件
private void FlowPanel_MouseMove(object sender, MouseEventArgs e)
{
if (this.isSelecting)
{
// 更新选择区域
this.selectionRect = new Rectangle(
Math.Min(this.selectionStart.X, e.X),
Math.Min(this.selectionStart.Y, e.Y),
Math.Abs(e.X - this.selectionStart.X),
Math.Abs(e.Y - this.selectionStart.Y));
this.flowWellsPanel.Invalidate(); // 强制重绘 FlowLayoutPanel
}
}
// MouseUp 事件
private void FlowPanel_MouseUp(object sender, MouseEventArgs e)
{
if (this.isSelecting)
{
this.isSelecting = false;
// 对每个井项控件进行检测,若其 Bounds 与 selectionRect 有交集,则勾选该 CheckBox
foreach (Control ctrl in this.flowWellsPanel.Controls)
{
if (ctrl is CheckBox checkBox)
{
Rectangle ctrlRect = ctrl.Bounds;
if (this.selectionRect.IntersectsWith(ctrlRect))
{
// 勾选该 CheckBox
checkBox.Checked = !checkBox.Checked;
}
}
}
this.selectionRect = Rectangle.Empty;
this.flowWellsPanel.Invalidate(); // 清除选择区域
}
}
// 设置双缓冲
private void SetDoubleBuffered(Control control)
{
var propertyInfo = control.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (propertyInfo != null)
{
propertyInfo.SetValue(control, true);
}
}
// 处理 FlowLayoutPanel 的绘制事件
private void FlowLayoutPanel_Paint(object sender, PaintEventArgs e)
{
if (this.selectionRect != Rectangle.Empty)
{
// 绘制选择框
using (Brush b = new SolidBrush(Color.FromArgb(50, Color.Transparent)))
{
e.Graphics.FillRectangle(b, this.selectionRect); // 填充选择区域
}
using (Pen p = new Pen(Color.Gray, 2))
{
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; // 设置虚线样式
e.Graphics.DrawRectangle(p, this.selectionRect); // 绘制选择区域边框
}
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void selectAllButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
foreach (Control ctrl in this.flowWellsPanel.Controls)
{
if (ctrl is CheckBox checkBox)
{
checkBox.Checked = true;
}
}
}
private void reverseAllButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
foreach (Control ctrl in this.flowWellsPanel.Controls)
{
if (ctrl is CheckBox checkBox)
{
checkBox.Checked = !checkBox.Checked;
}
}
}
private void repositoryItemSearchControl1_ButtonClick(object sender, ButtonPressedEventArgs e)
{
this.onSearchControl_Search();
}
private void repositoryItemSearchControl1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.onSearchControl_Search();
}
}
private void onSearchControl_Search()
{
using (SplashHelper splash = SplashHelper.Create())
{
SplashHelper.SetCaption("正在搜索");
try
{
string keyword = this.barManager1.ActiveEditor.EditValue?.ToString();
if (!string.IsNullOrEmpty(keyword))
{
foreach (Control ctrl in this.flowWellsPanel.Controls)
{
if (ctrl is CheckBox checkBox)
{
// 检查文本是否包含搜索关键词
if (checkBox.Text.Contains(keyword))
{
checkBox.Visible = true; // 显示匹配的 CheckBox
}
else
{
checkBox.Visible = false; // 隐藏不匹配的 CheckBox
}
}
}
}
else
{
// 如果搜索框为空,显示所有的 CheckBox
foreach (Control ctrl in this.flowWellsPanel.Controls)
{
if (ctrl is CheckBox checkBox)
{
checkBox.Visible = true;
}
}
}
}
catch (Exception ex)
{
}
}
}
private void exportButton_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.flowWellsPanel.Controls)
{
if (ctrl is CheckBox checkBox && checkBox.Checked)
{
SelectedWells.Add(checkBox.Text);
}
}
if (SelectedWells.Count == 0)
{
MessageBox.Show("请选择需要导出的井号!", "提示");
return;
}
this.DialogResult = DialogResult.OK;
}
}
}