Files
CustomerPanel-Test/libs/formHelper.cs
2026-03-05 09:56:57 +01:00

235 lines
9.3 KiB
C#

using System;
using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.Reflection;
using C4IT.Logging;
using C4IT.Graphics;
using static C4IT.Logging.cLogManager;
namespace C4IT_CustomerPanel.libs
{
class FormHelper
{
public static TaskBarLocation GetTaskBarLocation()
{
var Screens = System.Windows.Forms.Screen.AllScreens;
if (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Left > 0)
return TaskBarLocation.Left;
if (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Top > 0)
return TaskBarLocation.Top;
if (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Left == 0
&& System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width < System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width)
return TaskBarLocation.Right;
return TaskBarLocation.Bottom;
}
public static Point CenterOfScreen()
{
System.Drawing.Rectangle screenRect = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
// get the Screen Boundy
Size clientSize = new Size(screenRect.Width / 2, screenRect.Height / 2); // set the size of the form
Point location = new Point(screenRect.Width / 2 - clientSize.Width / 2, screenRect.Height / 2 - clientSize.Height / 2);
return location;
}
public static System.Windows.Point GetLocation(int formWidth, int formHeight, double scaleDPI)
{
var CM = MethodBase.GetCurrentMethod();
LogMethodBegin(CM);
try
{
TaskBarLocation taskBarloc = GetTaskBarLocation();
System.Windows.Point p = new System.Windows.Point((int)(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right / scaleDPI) - formWidth,
(int)(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom / scaleDPI) - formHeight);
switch (taskBarloc)
{
case TaskBarLocation.Bottom:
p.X = (int)(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right) - formWidth;
p.Y = (int)(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom) - formHeight;
break;
case TaskBarLocation.Top:
p.X = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - formWidth;
p.Y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Top;
break;
case TaskBarLocation.Left:
p.X = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Left;
p.Y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom - formHeight;
break;
case TaskBarLocation.Right:
p.X = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right - formWidth;
p.Y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom - formHeight;
break;
}
var lstLogs = new List<string>()
{
"Window position determination:",
" logical coordinates:",
string.Format(" Working area: ({0},{1})-({2},{3})", System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Left, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Top, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom),
string.Format(" Window size: {0},{1}", formWidth, formHeight),
string.Format(" Resuling taskbar location: {0}", taskBarloc.ToString()),
string.Format(" Resulting Window Position: {0},{1}", p.X, p.Y),
" physical dimensions:",
};
foreach (var scr in System.Windows.Forms.Screen.AllScreens)
{
var Scaling = NotifyerSupport.GetScaling(scr);
var DPI = NotifyerSupport.GetDPI(scr);
lstLogs.Add(String.Format(" {0}: ({1},{2})-({3},{4}), {5} DPI = x{6}", scr.DeviceName, scr.Bounds.Left, scr.Bounds.Top, scr.Bounds.Right, scr.Bounds.Bottom, DPI, Scaling));
}
cLogManager.DefaultLogger.LogList(LogLevels.Debug, lstLogs);
return p;
}
catch (Exception E)
{
LogException(E);
throw;
}
finally
{
LogMethodEnd(CM);
}
}
public static DropShadowEffect GetDropShadow()
{
DropShadowEffect myDropShadowEffect = new DropShadowEffect();
//DropShadowBitmapEffect myDropShadowEffect = new DropShadowBitmapEffect();
// Set the color of the shadow to Black.
Color myShadowColor = new Color
{
ScA = 1,
ScB = 0,
ScG = 0,
ScR = 0
};
// Note that the alpha value is ignored by Color property. The Opacity property is used to control the alpha.
myDropShadowEffect.Color = myShadowColor;
// Set the direction of where the shadow is cast to 320 degrees.
myDropShadowEffect.Direction = 320;
// Set the depth of the shadow being cast.
myDropShadowEffect.ShadowDepth = 5;
// Set the shadow opacity to half opaque or in other words - half transparent.
// The range is 0-1.
myDropShadowEffect.Opacity = 0.5;
return myDropShadowEffect;
}
public static System.Drawing.Icon GetNewsIcon(System.Drawing.Icon originalIcon)
{
System.Drawing.Graphics canvas;
System.Drawing.Bitmap iconBitmap = new System.Drawing.Bitmap(32, 32);
canvas = System.Drawing.Graphics.FromImage(iconBitmap);
canvas.DrawIcon(originalIcon, 0, 0);
System.Drawing.SolidBrush redBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
// Create location and size of ellipse.
int x = 16;
int y = 16;
int width = 16;
int height = 16;
// Fill ellipse on screen.
canvas.FillEllipse(redBrush, x, y, width, height);
canvas.Dispose();
return System.Drawing.Icon.FromHandle(iconBitmap.GetHicon());
}
public static System.Drawing.Icon GetErrorIcon(System.Drawing.Icon originalIcon)
{
System.Drawing.Graphics canvas;
System.Drawing.Bitmap iconBitmap = new System.Drawing.Bitmap(32, 32);
System.Drawing.Bitmap errBitmap = new System.Drawing.Bitmap(32, 32);
canvas = System.Drawing.Graphics.FromImage(iconBitmap);
canvas.DrawIcon(originalIcon, 0, 0);
System.Drawing.SolidBrush orangeBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
// Create location and size of ellipse.
int x = 16;
int y = 16;
int width = 16;
int height = 16;
// Fill ellipse on screen.
canvas.FillEllipse(orangeBrush, x, y, width, height);
canvas.Dispose();
return System.Drawing.Icon.FromHandle(iconBitmap.GetHicon());
}
public static bool GetIsFlickering(UIElement element)
{
return (bool)element.GetValue(IsFlickeringProperty);
}
public static void FlickeringDisconnectImage(UIElement element, bool value)
{
element.Visibility = value ? Visibility.Visible : Visibility.Hidden;
element.SetValue(IsFlickeringProperty, value);
}
public static readonly DependencyProperty IsFlickeringProperty =
DependencyProperty.RegisterAttached("IsFlickering", typeof(bool), typeof(FormHelper), new UIPropertyMetadata(false, FlickeringDisconnectImageChanged));
static void FlickeringDisconnectImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
StartAnimation(d as UIElement);
else
StopAnimation(d as UIElement);
}
private static void StartAnimation(UIElement element)
{
DoubleAnimation da = new DoubleAnimation
{
From = 1,
To = 0,
Duration = new Duration(TimeSpan.FromSeconds(2)),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
element.BeginAnimation(UIElement.OpacityProperty, da);
}
private static void StopAnimation(UIElement element)
{
element.BeginAnimation(UIElement.OpacityProperty, null);
}
}
}