实际尺寸之间转换

This commit is contained in:
艾竹
2023-02-11 22:20:24 +08:00
parent 86082cb788
commit 59bfed341d
14 changed files with 487 additions and 265 deletions

View File

@@ -59,7 +59,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
{
if (this.AutoSize)
{
return (double)(Unit == Unit.Cm ? DipHelper.DipToCm(this.ActualWidth) : DipHelper.DipToInch(this.ActualWidth)) / this.Zoom;
return (double)(Unit == Unit.Cm ? ScreenHelper.WidthToCm(this.ActualWidth) : ScreenHelper.WidthToInch(this.ActualWidth)) / this.Zoom;
}
else
{
@@ -255,9 +255,9 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
var rect = new Rect(0, 0, RenderSize.Width, RenderSize.Height);
drawingContext.DrawRectangle(Background, null, rect);
double xDest = (Unit == Unit.Cm ? DipHelper.CmToDip(Length) : DipHelper.InchToDip(Length)) * this.Zoom;
double xDest = (Unit == Unit.Cm ? ScreenHelper.CmToWidth(Length) : ScreenHelper.InchToWidth(Length)) * this.Zoom;
drawingContext.DrawRectangle(null, BorderPen, new Rect(new Point(0.0, 0.0), new Point(xDest, Height)));
double chip = Unit == Unit.Cm ? DipHelper.CmToDip(Chip) : DipHelper.InchToDip(Chip);
double chip = Unit == Unit.Cm ? ScreenHelper.CmToWidth(Chip) : ScreenHelper.InchToWidth(Chip);
drawingContext.DrawLine(RedPen, new Point(chip, 0), new Point(chip, Height));
//画偏移位置之前的
@@ -268,21 +268,21 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
double d;
if (Unit == Unit.Cm)
{
d = DipHelper.CmToDip(dUnit) * this.Zoom;
d = ScreenHelper.CmToWidth(dUnit) * this.Zoom;
if (dUnit < Length)
{
for (int i = 1; i <= 9; i++)
{
if (i != 5)
{
double dMm = DipHelper.CmToDip(dUnit + 0.1 * i) * this.Zoom;
double dMm = ScreenHelper.CmToWidth(dUnit + 0.1 * i) * this.Zoom;
if (Marks == MarksLocation.Up)
drawingContext.DrawLine(ThinPen, new Point(dMm, 0), new Point(dMm, SegmentHeight / 3.0));
else
drawingContext.DrawLine(ThinPen, new Point(dMm, Height), new Point(dMm, Height - SegmentHeight / 3.0));
}
}
double dMiddle = DipHelper.CmToDip(dUnit + 0.5) * this.Zoom;
double dMiddle = ScreenHelper.CmToWidth(dUnit + 0.5) * this.Zoom;
if (Marks == MarksLocation.Up)
drawingContext.DrawLine(p, new Point(dMiddle, 0), new Point(dMiddle, SegmentHeight * 2.0 / 3.0));
else
@@ -291,30 +291,30 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
}
else
{
d = DipHelper.InchToDip(dUnit) * this.Zoom;
d = ScreenHelper.InchToWidth(dUnit) * this.Zoom;
if (dUnit < Length)
{
if (Marks == MarksLocation.Up)
{
double dQuarter = DipHelper.InchToDip(dUnit + 0.25) * this.Zoom;
double dQuarter = ScreenHelper.InchToWidth(dUnit + 0.25) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(dQuarter, 0),
new Point(dQuarter, SegmentHeight / 3.0));
double dMiddle = DipHelper.InchToDip(dUnit + 0.5) * this.Zoom;
double dMiddle = ScreenHelper.InchToWidth(dUnit + 0.5) * this.Zoom;
drawingContext.DrawLine(p, new Point(dMiddle, 0),
new Point(dMiddle, SegmentHeight * 2D / 3D));
double d3Quarter = DipHelper.InchToDip(dUnit + 0.75) * this.Zoom;
double d3Quarter = ScreenHelper.InchToWidth(dUnit + 0.75) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(d3Quarter, 0),
new Point(d3Quarter, SegmentHeight / 3.0));
}
else
{
double dQuarter = DipHelper.InchToDip(dUnit + 0.25) * this.Zoom;
double dQuarter = ScreenHelper.InchToWidth(dUnit + 0.25) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(dQuarter, Height),
new Point(dQuarter, Height - SegmentHeight / 3.0));
double dMiddle = DipHelper.InchToDip(dUnit + 0.5) * this.Zoom;
double dMiddle = ScreenHelper.InchToWidth(dUnit + 0.5) * this.Zoom;
drawingContext.DrawLine(p, new Point(dMiddle, Height),
new Point(dMiddle, Height - SegmentHeight * 2D / 3D));
double d3Quarter = DipHelper.InchToDip(dUnit + 0.75) * this.Zoom;
double d3Quarter = ScreenHelper.InchToWidth(dUnit + 0.75) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(d3Quarter, Height),
new Point(d3Quarter, Height - SegmentHeight / 3.0));
}
@@ -333,7 +333,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Arial"),
DipHelper.PtToDip(6),
9,
Brushes.DimGray);
ft.SetFontWeight(FontWeights.Regular);
ft.TextAlignment = TextAlignment.Center;
@@ -353,21 +353,21 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
double d;
if (Unit == Unit.Cm)
{
d = DipHelper.CmToDip(dUnit) * this.Zoom;
d = ScreenHelper.CmToWidth(dUnit) * this.Zoom;
if (dUnit < Length)
{
for (int i = 1; i <= 9; i++)
{
if (i != 5)
{
double dMm = DipHelper.CmToDip(dUnit + 0.1 * i) * this.Zoom;
double dMm = ScreenHelper.CmToWidth(dUnit + 0.1 * i) * this.Zoom;
if (Marks == MarksLocation.Up)
drawingContext.DrawLine(ThinPen, new Point(dMm, 0), new Point(dMm, SegmentHeight / 3.0));
else
drawingContext.DrawLine(ThinPen, new Point(dMm, Height), new Point(dMm, Height - SegmentHeight / 3.0));
}
}
double dMiddle = DipHelper.CmToDip(dUnit + 0.5) * this.Zoom;
double dMiddle = ScreenHelper.CmToWidth(dUnit + 0.5) * this.Zoom;
if (Marks == MarksLocation.Up)
drawingContext.DrawLine(p, new Point(dMiddle, 0), new Point(dMiddle, SegmentHeight * 2.0 / 3.0));
else
@@ -376,30 +376,30 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
}
else
{
d = DipHelper.InchToDip(dUnit) * this.Zoom;
d = ScreenHelper.InchToWidth(dUnit) * this.Zoom;
if (dUnit < Length)
{
if (Marks == MarksLocation.Up)
{
double dQuarter = DipHelper.InchToDip(dUnit + 0.25) * this.Zoom;
double dQuarter = ScreenHelper.InchToWidth(dUnit + 0.25) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(dQuarter, 0),
new Point(dQuarter, SegmentHeight / 3.0));
double dMiddle = DipHelper.InchToDip(dUnit + 0.5) * this.Zoom;
double dMiddle = ScreenHelper.InchToWidth(dUnit + 0.5) * this.Zoom;
drawingContext.DrawLine(p, new Point(dMiddle, 0),
new Point(dMiddle, SegmentHeight * 2D / 3D));
double d3Quarter = DipHelper.InchToDip(dUnit + 0.75) * this.Zoom;
double d3Quarter = ScreenHelper.InchToWidth(dUnit + 0.75) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(d3Quarter, 0),
new Point(d3Quarter, SegmentHeight / 3.0));
}
else
{
double dQuarter = DipHelper.InchToDip(dUnit + 0.25) * this.Zoom;
double dQuarter = ScreenHelper.InchToWidth(dUnit + 0.25) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(dQuarter, Height),
new Point(dQuarter, Height - SegmentHeight / 3.0));
double dMiddle = DipHelper.InchToDip(dUnit + 0.5) * this.Zoom;
double dMiddle = ScreenHelper.InchToWidth(dUnit + 0.5) * this.Zoom;
drawingContext.DrawLine(p, new Point(dMiddle, Height),
new Point(dMiddle, Height - SegmentHeight * 2D / 3D));
double d3Quarter = DipHelper.InchToDip(dUnit + 0.75) * this.Zoom;
double d3Quarter = ScreenHelper.InchToWidth(dUnit + 0.75) * this.Zoom;
drawingContext.DrawLine(ThinPen, new Point(d3Quarter, Height),
new Point(d3Quarter, Height - SegmentHeight / 3.0));
}
@@ -418,7 +418,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Arial"),
DipHelper.PtToDip(6),
9,
Brushes.DimGray);
ft.SetFontWeight(FontWeights.Regular);
ft.TextAlignment = TextAlignment.Center;
@@ -442,11 +442,11 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
//Size desiredSize;
//if (Unit == Unit.Cm)
//{
// desiredSize = new Size(DipHelper.CmToDip(Length), Height);
// desiredSize = new Size(ScreenHelper.CmToWidth(Length), Height);
//}
//else
//{
// desiredSize = new Size(DipHelper.InchToDip(Length), Height);
// desiredSize = new Size(ScreenHelper.InchToWidth(Length), Height);
//}
//return desiredSize;
}
@@ -474,145 +474,145 @@ namespace AIStudio.Wpf.DiagramDesigner.Additionals.Controls
Up, Down
}
/// <summary>
/// A helper class for DIP (Device Independent Pixels) conversion and scaling operations.
/// </summary>
public static class DipHelper
{
/// <summary>
/// Converts millimeters to DIP (Device Independant Pixels).
/// </summary>
/// <param name="mm">A millimeter value.</param>
/// <returns>A DIP value.</returns>
public static double MmToDip(double mm)
{
return CmToDip(mm / 10.0);
}
///// <summary>
///// A helper class for DIP (Device Independent Pixels) conversion and scaling operations.
///// </summary>
//public static class DipHelper
//{
// /// <summary>
// /// Converts millimeters to DIP (Device Independant Pixels).
// /// </summary>
// /// <param name="mm">A millimeter value.</param>
// /// <returns>A DIP value.</returns>
// public static double MmToDip(double mm)
// {
// return CmToDip(mm / 10.0);
// }
/// <summary>
/// Converts centimeters to DIP (Device Independant Pixels).
/// </summary>
/// <param name="cm">A centimeter value.</param>
/// <returns>A DIP value.</returns>
public static double CmToDip(double cm)
{
return (cm * 96.0 / 2.54);
}
// /// <summary>
// /// Converts centimeters to DIP (Device Independant Pixels).
// /// </summary>
// /// <param name="cm">A centimeter value.</param>
// /// <returns>A DIP value.</returns>
// public static double CmToDip(double cm)
// {
// return (cm * 96.0 / 2.54);
// }
/// <summary>
/// Converts inches to DIP (Device Independant Pixels).
/// </summary>
/// <param name="inch">An inch value.</param>
/// <returns>A DIP value.</returns>
public static double InchToDip(double inch)
{
return (inch * 96.0);
}
public static double DipToInch(double dip)
{
return dip / 96D;
}
// /// <summary>
// /// Converts inches to DIP (Device Independant Pixels).
// /// </summary>
// /// <param name="inch">An inch value.</param>
// /// <returns>A DIP value.</returns>
// public static double InchToDip(double inch)
// {
// return (inch * 96.0);
// }
// public static double DipToInch(double dip)
// {
// return dip / 96D;
// }
/// <summary>
/// Converts font points to DIP (Device Independant Pixels).
/// </summary>
/// <param name="pt">A font point value.</param>
/// <returns>A DIP value.</returns>
public static double PtToDip(double pt)
{
return (pt * 96.0 / 72.0);
}
// /// <summary>
// /// Converts font points to DIP (Device Independant Pixels).
// /// </summary>
// /// <param name="pt">A font point value.</param>
// /// <returns>A DIP value.</returns>
// public static double PtToDip(double pt)
// {
// return (pt * 96.0 / 72.0);
// }
/// <summary>
/// Converts DIP (Device Independant Pixels) to centimeters.
/// </summary>
/// <param name="dip">A DIP value.</param>
/// <returns>A centimeter value.</returns>
public static double DipToCm(double dip)
{
return (dip * 2.54 / 96.0);
}
// /// <summary>
// /// Converts DIP (Device Independant Pixels) to centimeters.
// /// </summary>
// /// <param name="dip">A DIP value.</param>
// /// <returns>A centimeter value.</returns>
// public static double DipToCm(double dip)
// {
// return (dip * 2.54 / 96.0);
// }
/// <summary>
/// Converts DIP (Device Independant Pixels) to millimeters.
/// </summary>
/// <param name="dip">A DIP value.</param>
/// <returns>A millimeter value.</returns>
public static double DipToMm(double dip)
{
return DipToCm(dip) * 10.0;
}
// /// <summary>
// /// Converts DIP (Device Independant Pixels) to millimeters.
// /// </summary>
// /// <param name="dip">A DIP value.</param>
// /// <returns>A millimeter value.</returns>
// public static double DipToMm(double dip)
// {
// return DipToCm(dip) * 10.0;
// }
/// <summary>
/// Gets the system DPI scale factor (compared to 96 dpi).
/// From http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx
/// Should not be called before the Loaded event (else XamlException mat throw)
/// </summary>
/// <returns>A Point object containing the X- and Y- scale factor.</returns>
private static Point GetSystemDpiFactor()
{
PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow);
Matrix m = source.CompositionTarget.TransformToDevice;
return new Point(m.M11, m.M22);
}
// /// <summary>
// /// Gets the system DPI scale factor (compared to 96 dpi).
// /// From http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx
// /// Should not be called before the Loaded event (else XamlException mat throw)
// /// </summary>
// /// <returns>A Point object containing the X- and Y- scale factor.</returns>
// private static Point GetSystemDpiFactor()
// {
// PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow);
// Matrix m = source.CompositionTarget.TransformToDevice;
// return new Point(m.M11, m.M22);
// }
private const double DpiBase = 96.0;
// private const double DpiBase = 96.0;
/// <summary>
/// Gets the system configured DPI.
/// </summary>
/// <returns>A Point object containing the X- and Y- DPI.</returns>
public static Point GetSystemDpi()
{
Point sysDpiFactor = GetSystemDpiFactor();
return new Point(
sysDpiFactor.X * DpiBase,
sysDpiFactor.Y * DpiBase);
}
// /// <summary>
// /// Gets the system configured DPI.
// /// </summary>
// /// <returns>A Point object containing the X- and Y- DPI.</returns>
// public static Point GetSystemDpi()
// {
// Point sysDpiFactor = GetSystemDpiFactor();
// return new Point(
// sysDpiFactor.X * DpiBase,
// sysDpiFactor.Y * DpiBase);
// }
/// <summary>
/// Gets the physical pixel density (DPI) of the screen.
/// </summary>
/// <param name="diagonalScreenSize">Size - in inch - of the diagonal of the screen.</param>
/// <returns>A Point object containing the X- and Y- DPI.</returns>
public static Point GetPhysicalDpi(double diagonalScreenSize)
{
Point sysDpiFactor = GetSystemDpiFactor();
double pixelScreenWidth = SystemParameters.PrimaryScreenWidth * sysDpiFactor.X;
double pixelScreenHeight = SystemParameters.PrimaryScreenHeight * sysDpiFactor.Y;
double formatRate = pixelScreenWidth / pixelScreenHeight;
// /// <summary>
// /// Gets the physical pixel density (DPI) of the screen.
// /// </summary>
// /// <param name="diagonalScreenSize">Size - in inch - of the diagonal of the screen.</param>
// /// <returns>A Point object containing the X- and Y- DPI.</returns>
// public static Point GetPhysicalDpi(double diagonalScreenSize)
// {
// Point sysDpiFactor = GetSystemDpiFactor();
// double pixelScreenWidth = SystemParameters.PrimaryScreenWidth * sysDpiFactor.X;
// double pixelScreenHeight = SystemParameters.PrimaryScreenHeight * sysDpiFactor.Y;
// double formatRate = pixelScreenWidth / pixelScreenHeight;
double inchHeight = diagonalScreenSize / Math.Sqrt(formatRate * formatRate + 1.0);
double inchWidth = formatRate * inchHeight;
// double inchHeight = diagonalScreenSize / Math.Sqrt(formatRate * formatRate + 1.0);
// double inchWidth = formatRate * inchHeight;
double xDpi = Math.Round(pixelScreenWidth / inchWidth);
double yDpi = Math.Round(pixelScreenHeight / inchHeight);
// double xDpi = Math.Round(pixelScreenWidth / inchWidth);
// double yDpi = Math.Round(pixelScreenHeight / inchHeight);
return new Point(xDpi, yDpi);
}
// return new Point(xDpi, yDpi);
// }
/// <summary>
/// Converts a DPI into a scale factor (compared to system DPI).
/// </summary>
/// <param name="dpi">A Point object containing the X- and Y- DPI to convert.</param>
/// <returns>A Point object containing the X- and Y- scale factor.</returns>
public static Point DpiToScaleFactor(Point dpi)
{
Point sysDpi = GetSystemDpi();
return new Point(
dpi.X / sysDpi.X,
dpi.Y / sysDpi.Y);
}
// /// <summary>
// /// Converts a DPI into a scale factor (compared to system DPI).
// /// </summary>
// /// <param name="dpi">A Point object containing the X- and Y- DPI to convert.</param>
// /// <returns>A Point object containing the X- and Y- scale factor.</returns>
// public static Point DpiToScaleFactor(Point dpi)
// {
// Point sysDpi = GetSystemDpi();
// return new Point(
// dpi.X / sysDpi.X,
// dpi.Y / sysDpi.Y);
// }
/// <summary>
/// Gets the scale factor to apply to a WPF application
/// so that 96 DIP always equals 1 inch on the screen (whatever the system DPI).
/// </summary>
/// <param name="diagonalScreenSize">Size - in inch - of the diagonal of the screen</param>
/// <returns>A Point object containing the X- and Y- scale factor.</returns>
public static Point GetScreenIndependentScaleFactor(double diagonalScreenSize)
{
return DpiToScaleFactor(GetPhysicalDpi(diagonalScreenSize));
}
}
// /// <summary>
// /// Gets the scale factor to apply to a WPF application
// /// so that 96 DIP always equals 1 inch on the screen (whatever the system DPI).
// /// </summary>
// /// <param name="diagonalScreenSize">Size - in inch - of the diagonal of the screen</param>
// /// <returns>A Point object containing the X- and Y- scale factor.</returns>
// public static Point GetScreenIndependentScaleFactor(double diagonalScreenSize)
// {
// return DpiToScaleFactor(GetPhysicalDpi(diagonalScreenSize));
// }
//}
}