啊鑫
2024-07-09 0552fcc8cb73fc3021e2915129f55a42ed3f20e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#region
 
using System;
using System.Runtime.InteropServices;
using System.Text;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    /// <summary>
    ///     WinAPI函数库
    /// </summary>
    public class WinAPI
    {
        /// <summary>
        ///     枚举窗体的回调函数
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
 
        public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
 
        public const int SW_SHOWNORMAL = 1;
        public const int SW_SHOWMINIMIZED = 2;
        public const int SW_SHOWMAXIMIZED = 3;
        public const int SW_SHOWNOACTIVATE = 4; //用最近的大小和位置显示一个窗口,同时不改变活动窗口
        public const int SW_SHOW = 5; //用当前的大小和位置显示一个窗口,同时令其进入活动状态
        public const int SW_MINIMIZE = 6; //最小化窗口,活动状态给令一个窗口
        public const int SW_SHOWMINNOACTIVE = 7; //最小化一个窗口,同时不改变活动窗口
        public const int SW_SHOWNA = 8; //用当前的大小和位置显示一个窗口,不改变活动窗口
        public const int SW_RESTORE = 9; //与 SW_SHOWNORMAL   1 相同
        public const int SW_SHOWDEFAULT = 10;
        public const int SW_FORCEMINIMIZE = 11;
        public const int SW_MAX = 11;
        public const int WM_SYSCOMMAND = 0x112;
        public const int SC_MINIMIZE = 0xF020;
        public const int SC_MAXIMIZE = 0xF030;
 
        private const int SC_CLOSE = 0xF060;
        private const int MF_GRAYED = 0x1;
 
        public static void DisableCloseButton(IntPtr hwnd)
        {
            EnableMenuItem(GetSystemMenu(hwnd, false), SC_CLOSE, MF_GRAYED);
        }
 
        [DllImport("user32.dll")]
        private static extern int EnableMenuItem(IntPtr hMenu,
            int wIDEnableItem, int wEnable);
 
        [DllImport("user32.dll", EntryPoint = "EnumWindows",
            SetLastError = true)]
        public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc,
            uint lParam);
 
        [DllImport("user32")]
        public static extern int EnumWindows(EnumWindowsProc hWnd,
            IntPtr lParam);
        //使用:PostMessage(Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
 
        [DllImport("User32.dll ")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe,
            string strclass, string strname);
 
        [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
        public static extern IntPtr GetParent(IntPtr hWnd);
 
        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
 
        [DllImport("user32", SetLastError = true)]
        public static extern int GetWindowText(
            IntPtr hWnd, //窗口句柄 
            StringBuilder lpString, //标题 
            int nMaxCount //最大值 
        );
 
        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd,
            ref uint lpdwProcessId);
 
        [DllImport("user32.dll", EntryPoint = "IsWindow")]
        public static extern bool IsWindow(IntPtr hWnd);
 
        [DllImport("user32.dll", EntryPoint = "PostMessage")]
        public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam,
            int lParam);
 
        public static void RestoreWindow(IntPtr hwnd)
        {
            ShowWindow(hwnd, SW_RESTORE);
        }
 
        [DllImport("user32", EntryPoint = "SetActiveWindow")]
        public static extern int SetActiveWindow(int hwnd);
 
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool
            SetForegroundWindow(IntPtr hWnd); //WINAPI 设置当前活动窗体的句柄
 
        [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
        public static extern void SetLastError(uint dwErrCode);
        //使用:IntPtr p=FindWindowEx(System.IntPtr.Zero,System.IntPtr.Zero,null,"窗口标题"); 
 
        [DllImport("user32.dll", EntryPoint = "ShowWindow",
            CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
 
        [DllImport("User32.dll")]
        public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
 
        #region 窗体边框阴影效果变量申明
 
        public const int CS_DropSHADOW = 0x20000;
        public const int GCL_STYLE = -26;
 
        //声明Win32 API
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex,
            int dwNewLong);
 
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);
 
        #endregion
    }
}