using MGdu.WinFormUI;
using MyDb;
using Newtonsoft.Json.Linq;
using ryControls;
using ryControls.Pram;
using SkinPreview;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace RySkins
{
[ComVisible(true)]
public partial class SKinForm : GMForm
{
///
/// 无参数启动
///
public SKinForm() : base()
{
InitializeComponent();
}
private IntPtr ActiveHandle = IntPtr.Zero;
protected override void OnCreateControl()
{
//ActiveHandle= ryCommon.RyForm.GetActiveWindow();
base.OnCreateControl();
if (!this.DesignMode)
{
InitForm();
}
}
protected override void OnHandleCreated(EventArgs e)
{
ActiveHandle = ryCommon.RyForm.GetActiveWindow();
base.OnHandleCreated(e);
}
///
/// 以参数方式启动
///
///
public SKinForm(string[] args) : base()
{
InitializeComponent();
}
///
/// 以单参数方式启动
///
///
public SKinForm(string arg) : base()
{
InitializeComponent();
}
///
/// 窗口打开时间
///
public DateTime OpenTime { get; private set; } = DateTime.MinValue;
///
/// 是否记录使用情况
///
[Description("是否记录使用情况")]
public bool RecordUseTime { get; set; } = true;
///
/// 接收到参数
///
public static int MSG_RevPram { get; } = 112233;
private void InitForm()
{
OpenTime = DateTime.Now;
if (this.Parent==null)
{
if(this.StartPosition== FormStartPosition.CenterParent)
{
var handle = ActiveHandle;
var parent_handle= WinAPI.User32.GetParent(Handle);
//var dd= this.ParentForm;
if (parent_handle == IntPtr.Zero) { parent_handle = handle; }
var forms = Application.OpenForms;
for (int i = 0; i < forms.Count; i++)
{
if(forms[i].Handle==handle)
{
ShowInCenter(forms[i]);
break;
}
}
}
else if (this.StartPosition == FormStartPosition.CenterScreen)
{
ShowInCenter(null);
}
}
}
///
/// 显示在父窗体中间,如果没有父窗体,则显示在显示器中间
///
///
public void ShowInCenter(Form parent)
{
ShowInCenter(this,parent);
}
///
/// 显示在父窗体中间,如果没有父窗体,则显示在显示器中间
///
///
///
public static void ShowInCenter(Form CurForm, Form parent)
{
CurForm.WindowState = FormWindowState.Normal;
CurForm.StartPosition = FormStartPosition.Manual;
var screen = Screen.FromControl(CurForm);
if (parent==null) //如果没有父窗体,则显示在显示器中间
{
CurForm.Location = new Point((screen.WorkingArea.Width- CurForm.Width) /2,(screen.WorkingArea.Height- CurForm.Height) /2);
return;
}
if (parent.WindowState== FormWindowState.Minimized || !parent.Visible) //如果父窗体处于最小化,或者父窗体不可见
{
CurForm.Location = new Point((screen.WorkingArea.Width - CurForm.Width) / 2, (screen.WorkingArea.Height - CurForm.Height) / 2);
return;
}
var x = parent.Left+(parent.Width - CurForm.Width) / 2;
var y =parent.Top+ (parent.Height - CurForm.Height) / 2;
if((x+ CurForm.Width) > screen.WorkingArea.X+ screen.WorkingArea.Width)
{
x = screen.WorkingArea.X + screen.WorkingArea.Width - CurForm.Width;
}
if ((y + CurForm.Height) > screen.WorkingArea.Y + screen.WorkingArea.Height)
{
y = screen.WorkingArea.Y + screen.WorkingArea.Height - CurForm.Height;
}
if (x < 0) { x = 0; }
if (y < 0) { y = 0; }
CurForm.Location = new Point(x, y);
}
///
/// 激活窗体
///
public void ActiveSkinForm()
{
ryCommon.RyForm.BringToTop(Handle);
ryCommon.RyForm.SetActiveWindow(Handle);
}
///
/// 显示窗体
///
/// 指定要显示的窗体类型名
public static void ShowForm(Type type)
{
RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type);
form.Show();
}
///
/// 显示窗体
///
/// 指定要显示的窗体类型名
/// 参数
public static void ShowForm(Type type, string arg)
{
RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type, new object[] { arg });
form.Show();
}
///
/// 显示窗体
///
/// 指定要显示的窗体类型名
/// 参数
public static void ShowForm(Type type, string[] arg)
{
RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type, arg);
form.Show();
}
///
/// 显示模式窗体
///
/// 指定要显示的窗体类型名
/// 参数
public static void ShowDialogForm(Type type, string[] arg)
{
RySkins.SKinForm form = (RySkins.SKinForm)Activator.CreateInstance(type, arg);
form.ShowDialog();
form.Dispose();
}
///
/// 显示窗体,如果当前已经显示过,则激活窗体。
///
/// 指定要显示的窗体类型名
public static void ShowFormOne(Form parent, Type type)
{
ShowFormOne(parent,type,null);
}
///
/// 显示窗体,如果当前已经显示过,则激活窗体。
///
/// 指定要显示的窗体类型名
/// 参数
public static void ShowFormOne(Form parent, Type type, string[] arg)
{
var forms = Application.OpenForms;
for (int i = 0; i < forms.Count; i++)
{
if(forms[i].GetType()==type)
{
JObject jo = new JObject();
for (int j = 0; j < arg.Length; j++)
{
jo.Add("arg_"+j.ToString(), arg[j]);
}
//窗口已存在,发送消息来传送数据
RyWin32.SendMsg(parent.Handle, forms[i].Handle, MSG_RevPram, jo.ToString());
if (forms[i] is RySkins.SKinForm)
{
var skin_form = (RySkins.SKinForm)forms[i];
skin_form.ShowInCenter(parent);
skin_form.ActiveSkinForm();
skin_form.Show();
return;
}
else
{
RySkins.SKinForm.ShowInCenter(forms[i],parent);
forms[i].Show();
return;
}
}
}
Form form;
if (arg == null)
{ form = (Form)Activator.CreateInstance(type); }
else
{
form = (Form)Activator.CreateInstance(type, new object[] {arg });
}
ShowInCenter(form,parent);
ryCommon.RyForm.BringToTop(form.Handle);
ryCommon.RyForm.SetActiveWindow(form.Handle);
form.Show();
}
private void SKinForm_Load(object sender, EventArgs e)
{
if (!this.DesignMode)
{ base.XTheme = SkinSetting.FormSkin; }
//Opacity = 0.97;
}
private void SKinForm_FormClosed(object sender, FormClosedEventArgs e)
{
}
}
}