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.

150 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.Metro;
using POSV.Component;
namespace POSV
{
public partial class TransparentShortcut : BaseForm
{
private ePointerSide _pointerSide = ePointerSide.Left;
private Rectangle _pointerBounds = Rectangle.Empty;
private readonly Control _content = null;
private readonly Control _overlay = null;
public TransparentShortcut(Control overlay , double opacity , Control content , Rectangle pointerBounds)
{
this.SetStyle(ControlStyles.UserPaint , true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint , true);
this.SetStyle(ControlStyles.ResizeRedraw , true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer , true);
InitializeComponent();
this._overlay = overlay;
this._content = content;
this._pointerBounds = pointerBounds;
//无边框
this.FormBorderStyle = FormBorderStyle.None;
//透明度
this.Opacity = opacity;
//最大化/最小化
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
//窗口位置和大小
this.StartPosition = FormStartPosition.Manual;
Rectangle rect = new Rectangle(overlay.PointToScreen(Point.Empty) , overlay.Size);
this.Location = new Point(rect.X , rect.Y);
this.Size = new Size(rect.Width , rect.Height);
this.flyout.PointerSide = this._pointerSide;
this.flyout.Content = this._content;
if (this.flyout.Content is AbstractFlyoutPanelEx)
{
AbstractFlyoutPanelEx target = this.flyout.Content as AbstractFlyoutPanelEx;
target.CancelButtonClick += (o , args) =>
{
if (target.IsDisposed) return;
this.flyout.Close();
};
target.AcceptButtonClick += (o , args) =>
{
this.flyout.Close();
};
}
}
public TransparentShortcut(Control overlay , Control content , Rectangle pointerBounds):this(overlay,.3,content,pointerBounds)
{
}
public virtual Size GetContentSize()
{
Size size = this._content.Size;
size.Width += 12 + 2;
size.Height += 2;
return size;
}
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
//以当前点击按钮的Top为基准,计算上下两部分空白高度
int y_top = this._pointerBounds.Y - this.Location.Y - 30;
int y_bottom = this.Location.Y + this.Size.Height - this._pointerBounds.Y - 30;
//内容控件的显示区域
Rectangle flyoutBounds = new Rectangle();
flyoutBounds.Size = GetContentSize();
int pointerOffset = 10;
if (y_top >= flyoutBounds.Size.Height / 2 && y_bottom >= flyoutBounds.Size.Height / 2)
{
//居中
flyoutBounds.Location = new Point(this.Location.X , _pointerBounds.Location.Y - (flyoutBounds.Size.Height - this._pointerBounds.Size.Height - 24) / 2);
pointerOffset = _pointerBounds.Location.Y - flyoutBounds.Location.Y + 12;
}
else if (y_top < flyoutBounds.Size.Height / 2 && y_bottom >= flyoutBounds.Size.Height / 2)
{
//居下
flyoutBounds.Location = new Point(this.Location.X , this._pointerBounds.Y);
pointerOffset = this._pointerBounds.Size.Height / 2 - 12;
}
else
{
//居上
flyoutBounds.Location = new Point(this.Location.X , (_pointerBounds.Location.Y + this._pointerBounds.Size.Height - flyoutBounds.Size.Height));
pointerOffset = _pointerBounds.Location.Y - flyoutBounds.Location.Y + 12;
}
flyout.Show(flyoutBounds , this._pointerSide, pointerOffset , this._content);
}
private void OnFlyoutClosed(object sender , FormClosedEventArgs e)
{
this.Close();
}
private void OnFlyoutShowing(object sender , FlyoutShowingEventArgs e)
{
}
private void OnFlyoutClosing(object sender, FormClosingEventArgs e)
{
var item = sender as Flyout;
if (item.Content != null && item.Content is AbstractFlyoutPanelEx)
{
var target = item.Content as AbstractFlyoutPanelEx;
target.CloseFlyout();
}
}
}
}