using IOTContainer.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace IOTContainer.Common
{
public class WinApi
{
private const uint WM_SYSCOMMAND = 0x112; //系统消息
private const int SC_MONITORPOWER = 0xF020; //关闭显示器的系统命令
private const int SC_SCREENSAVE = 0xf140;
private const int MonitorPowerOff = 2; //2为PowerOff, 1为省电状态,-1为开机
private const int MonitorPowerOn = -1; //2为PowerOff, 1为省电状态,-1为开机
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);//广播消息,所有顶级窗体都会接收
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //窗体置顶
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); //取消窗体置顶
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1); //取消窗体置顶
public const uint SWP_NOMOVE = 0x0002; //不调整窗体位置
public const uint SWP_NOSIZE = 0x0001; //不调整窗体大小
public const int WM_CLOSE = 0x0010;//关闭窗体
public enum ShowWindowCommands : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_MAX = 10
}
//定义结构体
[StructLayout(LayoutKind.Sequential)]
public struct LASTINPUTINFO
{
// 设置结构体块容量
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
// 捕获的时间
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}
private LASTINPUTINFO mLastInputInfo;
[DllImport("user32.dll")]
public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, int data, UIntPtr extraInfo);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
[DllImport("Kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr ChildWin, IntPtr FatherWin);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uflags);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWindth, int nheight, bool brepaint);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
//关屏
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("shell32.dll")]
public extern static IntPtr ShellExecute(IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd
);
//锁屏
[DllImport("user32.dll")]
public static extern bool LockWorkStation();
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
public static void FindSetTop(string clasName, string winName)
{
IntPtr Win1 = FindWindow(clasName, winName);//设置置顶
if (Win1 != null && Win1 != IntPtr.Zero)
{
SetWindowPos(Win1, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
}
public static void Windows_Minimize(string winName)
{
IntPtr Win1 = FindWindow(null, winName);
if (Win1 != null && Win1 != IntPtr.Zero)
{
ShowWindow(Win1, (int)ShowWindowCommands.SW_MINIMIZE );
}
}
///
/// 获取鼠标位置
///
///
///
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out MyPoint point);
///
/// 获取置顶窗口名
///
///
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
public static string GetTopWindowName()
{
try
{
IntPtr hwnd = GetForegroundWindow();
Process[] pro = Process.GetProcesses();
foreach (var item in pro)
{
if (item.MainWindowHandle.ToInt32() == hwnd.ToInt32())
{
return item.ProcessName.ToLower();
}
}
return string.Empty;
}
catch (Exception ex)
{
Log.MyLog.WriteLogFile(ex.ToString(), "WinApi");
return string.Empty;
}
}
[DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
public static extern void ShowCursor(int status);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, IntPtr lpTimerName);
[DllImport("Kernel32.dll")]
public static extern UInt32 WaitForSingleObject(IntPtr pHandles, int dwMilliseconds);
}
}