Add snipping tool

This commit is contained in:
Serghei Cebotari 2024-11-06 23:43:00 +03:00
parent a065c4c699
commit dbaa8b111a
11 changed files with 355 additions and 185 deletions

View File

@ -4,57 +4,57 @@ namespace RhSolutions.AddIn;
public sealed class RhSolutionsAddIn : IExcelAddIn public sealed class RhSolutionsAddIn : IExcelAddIn
{ {
public static Application Excel { get; private set; } public static Application Excel { get; private set; }
public static ServiceProvider ServiceProvider { get; private set; } public static ServiceProvider ServiceProvider { get; private set; }
public static IAddInConfiguration Configuration { get; private set; } public static IAddInConfiguration Configuration { get; private set; }
public void AutoOpen() public void AutoOpen()
{ {
IServiceCollection Services = new ServiceCollection(); IServiceCollection Services = new ServiceCollection();
Services.AddHttpClient() Services.AddHttpClient()
.AddMemoryCache() .AddMemoryCache()
.AddSingleton((Application)ExcelDnaUtil.Application) .AddSingleton((Application)ExcelDnaUtil.Application)
.AddSingleton<IAddInConfiguration, AddInConfiguration>() .AddSingleton<IAddInConfiguration, AddInConfiguration>()
.AddSingleton<IDatabaseClient, DatabaseClient>() .AddSingleton<IDatabaseClient, DatabaseClient>()
.AddSingleton<ICurrencyClient, CurrencyClient>() .AddSingleton<ICurrencyClient, CurrencyClient>()
.AddTransient<IFileDialog, FileDialog>(); .AddTransient<IFileDialog, FileDialog>();
Services.AddSingleton<WriterFactory>(); Services.AddSingleton<WriterFactory>();
Services.AddTransient<NewPriceWriter>() Services.AddTransient<NewPriceWriter>()
.AddTransient<IWriter, NewPriceWriter>(s => s.GetService<NewPriceWriter>()); .AddTransient<IWriter, NewPriceWriter>(s => s.GetService<NewPriceWriter>());
Services.AddTransient<DxfWriter>() Services.AddTransient<DxfWriter>()
.AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>()); .AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>());
Services.AddTransient<CurrentPriceWriter>() Services.AddTransient<CurrentPriceWriter>()
.AddTransient<IWriter, CurrentPriceWriter>(s => s.GetService<CurrentPriceWriter>()); .AddTransient<IWriter, CurrentPriceWriter>(s => s.GetService<CurrentPriceWriter>());
Services.AddSingleton<ReaderFactory>(); Services.AddSingleton<ReaderFactory>();
Services.AddTransient<ExcelReader>() Services.AddTransient<ExcelReader>()
.AddTransient<IReader, ExcelReader>(s => s.GetService<ExcelReader>()); .AddTransient<IReader, ExcelReader>(s => s.GetService<ExcelReader>());
Services.AddTransient<GuessReader>() Services.AddTransient<GuessReader>()
.AddTransient<IReader, GuessReader>(s => s.GetService<GuessReader>()); .AddTransient<IReader, GuessReader>(s => s.GetService<GuessReader>());
Services.AddSingleton<FittingsCalculatorFactory>(); Services.AddSingleton<FittingsCalculatorFactory>();
Services.AddTransient<CouplingsCalculator>() Services.AddTransient<CouplingsCalculator>()
.AddTransient<IFittingsCalculator, CouplingsCalculator>(s => s.GetService<CouplingsCalculator>()); .AddTransient<IFittingsCalculator, CouplingsCalculator>(s => s.GetService<CouplingsCalculator>());
Services.AddTransient<SleevesCalculator>() Services.AddTransient<SleevesCalculator>()
.AddTransient<IFittingsCalculator, SleevesCalculator>(s => s.GetService<SleevesCalculator>()); .AddTransient<IFittingsCalculator, SleevesCalculator>(s => s.GetService<SleevesCalculator>());
Services.AddSingleton<ToolFactory>(); Services.AddSingleton<ToolFactory>();
ServiceProvider = Services.BuildServiceProvider(); ServiceProvider = Services.BuildServiceProvider();
Configuration = ServiceProvider.GetService<IAddInConfiguration>(); Configuration = ServiceProvider.GetService<IAddInConfiguration>();
Excel = ServiceProvider.GetService<Application>(); Excel = ServiceProvider.GetService<Application>();
EventsUtil.Initialize(); EventsUtil.Initialize();
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12; SecurityProtocolType.Tls12;
ServicePointManager.DefaultConnectionLimit = 50; ServicePointManager.DefaultConnectionLimit = 50;
} }
public void AutoClose() public void AutoClose()
{ {
EventsUtil.Uninitialize(); EventsUtil.Uninitialize();
} }
} }

View File

@ -9,127 +9,130 @@ namespace RhSolutions.Controllers;
[ComVisible(true)] [ComVisible(true)]
public class RibbonController : ExcelRibbon public class RibbonController : ExcelRibbon
{ {
private static IRibbonUI ribbonUi; private static IRibbonUI ribbonUi;
private static bool _workbookIsValid; private static bool _workbookIsValid;
public override string GetCustomUI(string RibbonID) public override string GetCustomUI(string RibbonID)
{ {
return @" return @"
<customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='LoadImage'> <customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui' loadImage='LoadImage'>
<ribbon> <ribbon>
<tabs> <tabs>
<tab id='rau' label='RhSolutions'> <tab id='rau' label='RhSolutions'>
<group id='priceList' label='Прайс-лист'> <group id='priceList' label='Прайс-лист'>
<button id='export' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' image='RhSolutions' onAction='OnToolPressed'/> <button id='export' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' image='RhSolutions' onAction='OnToolPressed'/>
<button id='convert' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/> <button id='convert' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/>
<button id='merge' label='Объединить' size='normal' imageMso='Copy' onAction='OnToolPressed'/> <button id='merge' label='Объединить' size='normal' imageMso='Copy' onAction='OnToolPressed'/>
<button id='guess' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='large' imageMso='ControlWizards' onAction='OnToolPressed'/> <button id='guess' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='large' imageMso='ControlWizards' onAction='OnToolPressed'/>
</group> </group>
<group id='fittingsCalc' label='Расчет фитингов'> <group id='fittingsCalc' label='Расчет фитингов'>
<button id='fillsleeves' getEnabled='GetFittingsCalcEnabled' label='Гильзы' size='large' image='Sleeve' onAction='OnToolPressed'/> <button id='fillsleeves' getEnabled='GetFittingsCalcEnabled' label='Гильзы' size='large' image='Sleeve' onAction='OnToolPressed'/>
<button id='fillcouplings' getEnabled='GetFittingsCalcEnabled' label='Муфты' size='large' image='Coupling' onAction='OnToolPressed'/> <button id='fillcouplings' getEnabled='GetFittingsCalcEnabled' label='Муфты' size='large' image='Coupling' onAction='OnToolPressed'/>
</group> </group>
<group id='exportTab' label='Экспорт'> <group id='exportTab' label='Экспорт'>
<button id='dxfexport' getEnabled='GetDxfEnabled' label='DXF' size='large' image='DXF' onAction='OnToolPressed'/> <button id='dxfexport' getEnabled='GetDxfEnabled' label='DXF' size='large' image='DXF' onAction='OnToolPressed'/>
</group> </group>
<group id='settings' getLabel='GetVersionLabel'> <group id='importTab' label='OCR'>
<button id='setPriceList' getLabel='GetPriceListPathLabel' size='large' image='RhSolutions' onAction='OnSetPricePressed'/> <button id='ocr' label='Распознать таблицу' size='large' imageMso='TableInsert' onAction='OnToolPressed'/>
</group> </group>
</tab> <group id='settings' getLabel='GetVersionLabel'>
</tabs> <button id='setPriceList' getLabel='GetPriceListPathLabel' size='large' image='RhSolutions' onAction='OnSetPricePressed'/>
</ribbon> </group>
</customUI>"; </tab>
} </tabs>
</ribbon>
</customUI>";
}
public void RibbonLoad(IRibbonUI sender) public void RibbonLoad(IRibbonUI sender)
{ {
ribbonUi = sender; ribbonUi = sender;
} }
public static void RefreshControl(string id) public static void RefreshControl(string id)
{ {
ribbonUi?.InvalidateControl(id); ribbonUi?.InvalidateControl(id);
} }
public void OnSetPricePressed(IRibbonControl control) public void OnSetPricePressed(IRibbonControl control)
{ {
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetService<IFileDialog>(); IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetService<IFileDialog>();
string file = dialog.GetFile(); string file = dialog.GetFile();
if (!string.IsNullOrEmpty(file)) if (!string.IsNullOrEmpty(file))
{ {
RhSolutionsAddIn.Configuration.SetPriceListPath(file); RhSolutionsAddIn.Configuration.SetPriceListPath(file);
RhSolutionsAddIn.Configuration.SaveSettings(); RhSolutionsAddIn.Configuration.SaveSettings();
} }
} }
public void OnToolPressed(IRibbonControl control) public void OnToolPressed(IRibbonControl control)
{ {
try try
{ {
var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>(); var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>();
using Tool tool = toolFactory.GetTool(control.Id); using Tool tool = toolFactory.GetTool(control.Id);
tool.Execute(); tool.Execute();
} }
catch (Exception exception) catch (Exception exception)
{ {
MessageBox.Show(exception.Message, MessageBox.Show(exception.Message,
"Ошибка", "Ошибка",
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Information); MessageBoxIcon.Information);
RhSolutionsAddIn.Excel.StatusBar = false; RhSolutionsAddIn.Excel.StatusBar = false;
return; return;
} }
} }
public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid; public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid; public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetFittingsCalcEnabled(IRibbonControl control) => _workbookIsValid; public bool GetFittingsCalcEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook != null && !_workbookIsValid; public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook != null && !_workbookIsValid;
public bool GetExportEnabled(IRibbonControl control) public bool GetExportEnabled(IRibbonControl control)
{ {
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null) if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
return false; return false;
else else
{ {
Range selection = RhSolutionsAddIn.Excel.Selection; Range selection = RhSolutionsAddIn.Excel.Selection;
return selection.Columns.Count == 2; return selection.Columns.Count == 2;
} }
} }
public string GetVersionLabel(IRibbonControl control) public string GetVersionLabel(IRibbonControl control)
{ {
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
return $"v{version}"; return $"v{version}";
} }
public string GetPriceListPathLabel(IRibbonControl control) public string GetPriceListPathLabel(IRibbonControl control)
{ {
string name = RhSolutionsAddIn.Configuration.GetPriceListFileName(); string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
return string.IsNullOrEmpty(name) ? "Указать шаблонный файл" : name; return string.IsNullOrEmpty(name) ? "Указать шаблонный файл" : name;
} }
public static void UpdateWorkbookValidation() public static void UpdateWorkbookValidation()
{ {
if (RhSolutionsAddIn.Excel.ActiveWorkbook == null) if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
_workbookIsValid = false; _workbookIsValid = false;
else else
{ {
Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet; Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
_workbookIsValid = worksheet.IsValidSource(); _workbookIsValid = worksheet.IsValidSource();
} }
} }
public static void EnsurePriseListExists() public static void EnsurePriseListExists()
{ {
string pricelistPath = RhSolutionsAddIn.Configuration.GetPriceListPath(); string pricelistPath = RhSolutionsAddIn.Configuration.GetPriceListPath();
if (!File.Exists(pricelistPath)) if (!File.Exists(pricelistPath))
{ {
RhSolutionsAddIn.Configuration.SetPriceListPath(string.Empty); RhSolutionsAddIn.Configuration.SetPriceListPath(string.Empty);
} }
} }
} }

View File

@ -23,6 +23,7 @@
<Reference Path="System.Runtime.CompilerServices.Unsafe.dll" Pack="true" /> <Reference Path="System.Runtime.CompilerServices.Unsafe.dll" Pack="true" />
<Reference Path="System.Threading.Tasks.Extensions.dll" Pack="true" /> <Reference Path="System.Threading.Tasks.Extensions.dll" Pack="true" />
<Reference Path="System.ValueTuple.dll" Pack="true" /> <Reference Path="System.ValueTuple.dll" Pack="true" />
<Reference Path="SnippingTool.dll" Pack="true" />
<Image Name='RhSolutions' Path='Images\RhSolutions.png' Pack='true' /> <Image Name='RhSolutions' Path='Images\RhSolutions.png' Pack='true' />
<Image Name='DXF' Path='Images\DXF.png' Pack='true' /> <Image Name='DXF' Path='Images\DXF.png' Pack='true' />
<Image Name='Sleeve' Path='Images\Sleeve.png' Pack='true' /> <Image Name='Sleeve' Path='Images\Sleeve.png' Pack='true' />

View File

@ -26,6 +26,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\RhSolutions.ProductSku\RhSolutions.ProductSku.csproj" /> <ProjectReference Include="..\RhSolutions.ProductSku\RhSolutions.ProductSku.csproj" />
<ProjectReference Include="..\SnippingTool\SnippingTool.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="Images\Coupling.png"> <None Update="Images\Coupling.png">

View File

@ -0,0 +1,22 @@
#if !NET472
using System.Runtime.Versioning;
#endif
namespace RhSolutions.Tools;
internal class OcrTool : Tool
{
public OcrTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory)
{
}
public override void Execute()
{
var bmp = SnippingTool.SnippingTool.Snip();
if (bmp != null)
{
// Do something with the bitmap
//...
}
}
}

View File

@ -9,22 +9,22 @@ namespace RhSolutions.Tools;
#endif #endif
internal abstract class Tool : IDisposable internal abstract class Tool : IDisposable
{ {
protected readonly ReaderFactory _readerFactory; protected readonly ReaderFactory _readerFactory;
protected readonly WriterFactory _writerFactory; protected readonly WriterFactory _writerFactory;
protected IReader _reader; protected IReader _reader;
protected IWriter _writer; protected IWriter _writer;
public Tool(ReaderFactory readerFactory, WriterFactory writerFactory) public Tool(ReaderFactory readerFactory, WriterFactory writerFactory)
{ {
_readerFactory = readerFactory; _readerFactory = readerFactory;
_writerFactory = writerFactory; _writerFactory = writerFactory;
} }
public void Dispose() public void Dispose()
{ {
_reader?.Dispose(); _reader?.Dispose();
_writer?.Dispose(); _writer?.Dispose();
} }
public abstract void Execute(); public abstract void Execute();
} }

View File

@ -2,23 +2,24 @@
internal class ToolFactory internal class ToolFactory
{ {
static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>(); static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>();
static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>(); static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>();
static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService<FittingsCalculatorFactory>(); static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService<FittingsCalculatorFactory>();
public Tool GetTool(string toolName) public Tool GetTool(string toolName)
{ {
Tool tool = toolName switch Tool tool = toolName switch
{ {
"export" => new ExportTool(readerFactory, writerFactory), "export" => new ExportTool(readerFactory, writerFactory),
"convert" => new ConvertTool(readerFactory, writerFactory), "convert" => new ConvertTool(readerFactory, writerFactory),
"merge" => new MergeTool(readerFactory, writerFactory), "merge" => new MergeTool(readerFactory, writerFactory),
"dxfexport" => new DxfTool(readerFactory, writerFactory), "dxfexport" => new DxfTool(readerFactory, writerFactory),
"guess" => new GuessTool(readerFactory, writerFactory), "guess" => new GuessTool(readerFactory, writerFactory),
"fillsleeves" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Sleeves"), "fillsleeves" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Sleeves"),
"fillcouplings" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Couplings"), "fillcouplings" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Couplings"),
_ => throw new Exception($"Неизвестный инструмент {toolName}"), "ocr" => new OcrTool(readerFactory, writerFactory),
}; _ => throw new Exception($"Неизвестный инструмент {toolName}"),
return tool; };
} return tool;
}
} }

View File

@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RhSolutions.Tests", "RhSolu
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.ProductSku", "RhSolutions.ProductSku\RhSolutions.ProductSku.csproj", "{59CD05D0-71E0-4027-968A-8BE89A6FDCEF}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.ProductSku", "RhSolutions.ProductSku\RhSolutions.ProductSku.csproj", "{59CD05D0-71E0-4027-968A-8BE89A6FDCEF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnippingTool", "SnippingTool\SnippingTool.csproj", "{DDB517C7-DF61-4C26-B691-956D0E5906C3}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{59CD05D0-71E0-4027-968A-8BE89A6FDCEF}.Debug|Any CPU.Build.0 = Debug|Any CPU {59CD05D0-71E0-4027-968A-8BE89A6FDCEF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59CD05D0-71E0-4027-968A-8BE89A6FDCEF}.Release|Any CPU.ActiveCfg = Release|Any CPU {59CD05D0-71E0-4027-968A-8BE89A6FDCEF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59CD05D0-71E0-4027-968A-8BE89A6FDCEF}.Release|Any CPU.Build.0 = Release|Any CPU {59CD05D0-71E0-4027-968A-8BE89A6FDCEF}.Release|Any CPU.Build.0 = Release|Any CPU
{DDB517C7-DF61-4C26-B691-956D0E5906C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDB517C7-DF61-4C26-B691-956D0E5906C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDB517C7-DF61-4C26-B691-956D0E5906C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDB517C7-DF61-4C26-B691-956D0E5906C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

38
SnippingTool/SnippingTool.Designer.cs generated Normal file
View File

@ -0,0 +1,38 @@
namespace SnippingTool;
partial class SnippingTool
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}

View File

@ -0,0 +1,86 @@
namespace SnippingTool;
public partial class SnippingTool : Form
{
public static Image? Snip()
{
var rc = Screen.PrimaryScreen.Bounds;
using Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
using Graphics gr = Graphics.FromImage(bmp);
gr.CopyFromScreen(0, 0, 0, 0, bmp.Size);
using var snipper = new SnippingTool(bmp);
return snipper.ShowDialog() == DialogResult.OK ? snipper.Image : null;
}
public SnippingTool(Image screenShot)
{
InitializeComponent();
BackgroundImage = screenShot;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
DoubleBuffered = true;
}
public Image? Image { get; set; }
private Rectangle rcSelect = new Rectangle();
private Point pntStart;
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
pntStart = e.Location;
rcSelect = new Rectangle(e.Location, new Size(0, 0));
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
int x1 = Math.Min(e.X, pntStart.X);
int y1 = Math.Min(e.Y, pntStart.Y);
int x2 = Math.Max(e.X, pntStart.X);
int y2 = Math.Max(e.Y, pntStart.Y);
rcSelect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (rcSelect.Width <= 0 || rcSelect.Height <= 0)
{
return;
}
Image = new Bitmap(rcSelect.Width, rcSelect.Height);
using Graphics gr = Graphics.FromImage(Image);
gr.DrawImage(BackgroundImage, new Rectangle(0, 0, Image.Width, Image.Height),
rcSelect, GraphicsUnit.Pixel);
DialogResult = DialogResult.OK;
}
protected override void OnPaint(PaintEventArgs e)
{
using Brush br = new SolidBrush(Color.FromArgb(120, Color.White));
int x1 = rcSelect.X; int x2 = rcSelect.X + rcSelect.Width;
int y1 = rcSelect.Y; int y2 = rcSelect.Y + rcSelect.Height;
e.Graphics.FillRectangle(br, new Rectangle(0, 0, x1, Height));
e.Graphics.FillRectangle(br, new Rectangle(x2, 0, Width - x2, Height));
e.Graphics.FillRectangle(br, new Rectangle(x1, 0, x2 - x1, y1));
e.Graphics.FillRectangle(br, new Rectangle(x1, y2, x2 - x1, Height - y2));
using Pen pen = new Pen(Color.Red, 3);
e.Graphics.DrawRectangle(pen, rcSelect);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
DialogResult = DialogResult.Cancel;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<LangVersion>10</LangVersion>
<TargetFrameworks>net472;net6.0-windows</TargetFrameworks>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>