using Container.Common; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Windows.Threading; namespace Container.ChildWindows { /// /// WindowButton.xaml 的交互逻辑 /// public partial class WindowButton : Window { Class_Log log = new Class_Log();//日志对象 App app = ((App)System.Windows.Application.Current); Point _TicstartPoint;//鼠标按下位置 Point _TicendPoint;//鼠标抬起位置 //Point mousePostion; //初始化 public WindowButton() { InitializeComponent(); this.Top = app.ScreenHeight - this.Height - 50; this.Left = app.ScreenWidth - this.Width-65; mLastInputInfo = new LASTINPUTINFO(); mLastInputInfo.cbSize = Marshal.SizeOf(mLastInputInfo); mIdleTimer = new System.Windows.Threading.DispatcherTimer(); mIdleTimer.Tick += new EventHandler(IdleTime);//起个Timer一直获取当前时间 mIdleTimer.Interval = new TimeSpan(0, 0, 0, 1, 0); ; //mIdleTimer.Start(); } //窗体关闭事件 private void Windows_Closed(object sender, EventArgs e) { try { Process[] process = Process.GetProcesses(); foreach (Process prc in process) { if (prc.ProcessName.Equals(System.Diagnostics.Process.GetCurrentProcess().ProcessName)) { if (app.AppList != null && app.AppList.Count > 1) { WindowButton win = new WindowButton(); win.Show(); } } } } catch (Exception ex) { log.WriteLogFile("窗体关闭异常:" + ex.ToString(), "MainWindowError"); } } //鼠标按下事件 private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { _TicstartPoint = new Point(this.Left, this.Top); if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); //SendMessage(new WindowInteropHelper(this).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } } //鼠标抬起事件 private void Btn_MouseUp(object sender, MouseButtonEventArgs e) { try { _TicendPoint = new Point(this.Left, this.Top); //X轴滑动的距离 double offsetY = _TicstartPoint.Y - _TicendPoint.Y; double offsetX = _TicstartPoint.X - _TicendPoint.X; if (offsetY < 2 && offsetY > -2 && offsetX > -5 && offsetX < 5) { this.Img.Visibility = System.Windows.Visibility.Hidden; //this.GgifImage.Visibility = System.Windows.Visibility.Hidden; this.Img.Visibility = System.Windows.Visibility.Hidden; //this.GgifImage.Visibility = System.Windows.Visibility.Hidden; WindowAppChoose win = new WindowAppChoose(this.Top, this.Left + this.Width, this.Width); win.ShowDialog(); //Thread.Sleep(1000); //this.GgifImage.Visibility = System.Windows.Visibility.Visible; this.Img.Visibility = System.Windows.Visibility.Visible; //this.Left = SystemParameters.PrimaryScreenWidth - this.Width / 2 + 5; } } catch (Exception ex) { log.WriteLogFile(ex.ToString()); } } //[DllImport("user32.dll")] //public static extern bool ReleaseCapture(); //[DllImport("user32.dll")] //public static extern bool SetCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; [DllImport("user32.dll", SetLastError = true)] static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpWindowClass, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); const int GWL_HWNDPARENT = -8; [DllImport("user32.dll")] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); [DllImport("user32.dll", EntryPoint = "ShowWindow")] public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow); public static int SW_SHOW = 5; public static int SW_NORMAL = 1; public static int SW_MAX = 3; public static int SW_HIDE = 0; public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //窗体置顶 public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); //取消窗体置顶 public const uint SWP_NOMOVE = 0x0002; //不调整窗体位置 public const uint SWP_NOSIZE = 0x0001; //不调整窗体大小 private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {//拖动窗体 SendMessage(new WindowInteropHelper(this).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } #region 检测鼠标动作 //定义结构体 [StructLayout(LayoutKind.Sequential)] struct LASTINPUTINFO { // 设置结构体块容量 [MarshalAs(UnmanagedType.U4)] public int cbSize; // 捕获的时间 [MarshalAs(UnmanagedType.U4)] public uint dwTime; } // 定义定时器,结构体对象等 private DispatcherTimer mIdleTimer; private LASTINPUTINFO mLastInputInfo; [DllImport("user32.dll")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); /*初始化 mLastInputInfo = new LASTINPUTINFO(); mLastInputInfo.cbSize = Marshal.SizeOf(mLastInputInfo); mIdleTimer = new System.Windows.Threading.DispatcherTimer(); mIdleTimer.Tick += new EventHandler(IdleTime);//起个Timer一直获取当前时间 mIdleTimer.Interval = new TimeSpan(0, 0, 0, 1, 0); mIdleTimer.Start(); */ //实现 private void IdleTime(object sender, EventArgs e) { if (!GetLastInputInfo(ref mLastInputInfo)) { //MessageBox.Show("GetLastInputInfo Failed!"); //this.Top = SystemParameters.PrimaryScreenHeight - this.Height - 50; //this.Left = SystemParameters.PrimaryScreenWidth - this.Width - 10; } else { if ((Environment.TickCount - (long)mLastInputInfo.dwTime) / 1000 > 10) { //MessageBox.Show("no operation for 5 minutes."); //this.Top = SystemParameters.PrimaryScreenHeight - this.Height - 50; this.Left = app.ScreenWidth - this.Width / 2 + 5; } } } #endregion private void Win_MouseEnter(object sender, MouseEventArgs e) { //if (this.Left > SystemParameters.PrimaryScreenWidth - this.Width) //{ // this.Left = SystemParameters.PrimaryScreenWidth - this.Width; //} } private void Win_MouseLeave(object sender, MouseEventArgs e) { //if (this.Left >= SystemParameters.PrimaryScreenWidth - this.Width-2) //{ // this.Left = SystemParameters.PrimaryScreenWidth - this.Width / 2 + 5; //} } private void Btn_MouseDown(object sender, MouseButtonEventArgs e) { _TicstartPoint = e.GetPosition(App.Current.MainWindow); } private void GgifImage_MouseDown(object sender, MouseButtonEventArgs e) { //_TicstartPoint = PointToScreen(e.GetPosition(this)); _TicstartPoint = new Point(this.Left,this.Top); } private void GgifImage_MouseUp(object sender, MouseButtonEventArgs e) { try { _TicendPoint = new Point(this.Left, this.Top); //X轴滑动的距离 double offsetY = _TicstartPoint.Y - _TicendPoint.Y; double offsetX = _TicstartPoint.X - _TicendPoint.X; if (offsetY < 2 && offsetY > -2 && offsetX > -5 && offsetX < 5) { this.Img.Visibility = System.Windows.Visibility.Hidden; //this.GgifImage.Visibility = System.Windows.Visibility.Hidden; this.Img.Visibility = System.Windows.Visibility.Hidden; //this.GgifImage.Visibility = System.Windows.Visibility.Hidden; WindowAppChoose win = new WindowAppChoose(this.Top, this.Left + this.Width, this.Width); win.ShowDialog(); //Thread.Sleep(1000); //this.GgifImage.Visibility = System.Windows.Visibility.Visible; this.Img.Visibility = System.Windows.Visibility.Visible; //this.Left = SystemParameters.PrimaryScreenWidth - this.Width / 2 + 5; } } catch (Exception ex) { log.WriteLogFile(ex.ToString()); } } } }