using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System;
using System.Collections.Generic;
using System.EnterpriseServices;
namespace FengCreateCLRDll {
namespace Win32API {
/*
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_FORCEMINIMIZE 11
SW_MAX 11
*/
/// <summary>
/// 使用托管枚举定义
/// WinUser.h的宏,用来显示窗体命令参数,如被ShowWindow函数使用
/// 使用时,应当转换成int类型,如(int)Cmd_SHOWWINDOWS.SW_NORMAL;
/// </summary>
public enum Cmd_SHOWWINDOWS : int {
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_FORCEMINIMIZE = 11,
SW_MAX = 11
};
//****************************ShellExecuteEx
/// <summary>
/// 该类实现ShellExecuteEx函数的一些功能,例如以默认的方式打开文件、打开文件的属性页
/// </summary>
public static class ShellExecuteEx_Fun {
#region 可在外部手工实现
/// <summary>
/// ShellExeInfo结构实现win32API的SHELLEXECUTEINFO结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct ShellExeInfo {
public int size;
public uint mask;
public IntPtr hwnd;
public string verb;
public string file;
public string parameters;
public string directory;
public int show;
public IntPtr hInsApp;
public IntPtr IDList;
public string lpClass;
public IntPtr hkeyClass;
public uint dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
};
/*BOOL ShellExecuteEx(
LPSHELLEXECUTEINFO lpExecInfo
);*/
/// <summary>
/// ShellExecuteEx实现win32API的ShellExecuteEx函数
/// </summary>
[DllImport("shell32.dll")]
public static extern bool ShellExecuteEx(ref ShellExeInfo fInfo);
#endregion 可在外部手工实现
/// <summary>
/// SHELLEXECUTEINFO的verb成员可由此类的静态只读字段获取
/// </summary>
public class Verb {
private Verb() { }
public static readonly string edit = "edit";
/// <summary>
/// 在explore中打开指定的文件夹
/// </summary>
public static readonly string explore = "explore";
public static readonly string find = "find";
public static readonly string open = "open";
public static readonly string print = "print";
/// <summary>
/// 用来打开文件的属性页
/// </summary>
public static readonly string properties = "properties";
};
//如果打开的是图片文件,并且是连续用次函数打开的,就可以连续显示№●○▲■◆◇□〓ㄟ
/// <summary>
/// 该方法实现以指定的动作操作一个文件
/// </summary>
/// <param name="path">执行的文件路径</param>
/// <param name="verb">指定的动作,如edit,explore,find,open,
/// print,properties(用来打开文件的属性页),可通过Verb类的静态只读字段获取
///
/// </param>
/// <returns>如果操作成功返回true,否则为false</returns>
public static bool GetfileORfolder_Operation(string path, string verb) {
ShellExeInfo fileInfo = new ShellExeInfo();
const int SEE_MASK_INVOKEIDLIST = 0x0000000c;
const int SW_SHOW = 5;
fileInfo.size = Marshal.SizeOf(fileInfo);
fileInfo.mask = SEE_MASK_INVOKEIDLIST;
fileInfo.hwnd = IntPtr.Zero;
fileInfo.verb = verb;
fileInfo.parameters = null;
fileInfo.directory = null;
fileInfo.show = SW_SHOW;
fileInfo.hInsApp = IntPtr.Zero;
fileInfo.IDList = IntPtr.Zero;
fileInfo.lpClass = null;
fileInfo.hkeyClass = IntPtr.Zero;
fileInfo.dwHotKey = 0;
fileInfo.hIcon = IntPtr.Zero;
fileInfo.hProcess = IntPtr.Zero;
fileInfo.file = path + '/0';
if(ShellExecuteEx(ref fileInfo))
return true;
return false;
}
};
//*************************SHFileOperation
/// <summary>
/// 该类实现SHFileOperation函数的一些功能,如:删除文件到回收站
/// </summary>
public static class SHFileOPeration_Fun {
/* internal static string Description() {
return "如果是移动或者复制文件,第二个参数给出目的路径就行了,不必加上文件名和扩展名"
+ "否则会创建新的文件/n 使用带有不定参数的重载函数,可以用来操作指定的文件"
+ "这些函数重载均可以通配符,使用不定参数为参数的函数,所使用通配符,可以指定多个不同的通配符"
+ "如:*.txt,*.doc";
}
*/
/*typedef struct _SHFILEOPSTRUCT {
HWND hwnd;
UINT wFunc;
LPCTSTR pFrom;
LPCTSTR pTo;
FILEOP_FLAGS fFlags;
BOOL fAnyOperationsAborted;
LPVOID hNameMappings;
LPCTSTR lpszProgressTitle;
} SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT;
*/
/// <summary>
/// 一个托管结构实现win32API的SHFILEOPSTRUCT结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEOPSTRUCT {
public int hwnd;
public int wFunc;
public string pFrom;
public string pTo;
public ushort flags;
public bool aborted;
public IntPtr hNameMappings;
public string ProgressTitle;
};
/*int SHFileOperation( LPSHFILEOPSTRUCT lpFileOp
);
*/
/// <summary>
/// 指定SHFILEOPSTRUCT结构的wFunc的值,使用时转换成int
/// </summary>
public enum FileOpeArg : int {
FO_MOVE = 0x0001,
FO_COPY = 0x0002,
FO_DELETE = 0x0003,
FO_RENAME = 0x0004,
};
/// <summary>
/// 托管静态方法实现win32API的SHFileOperation函数
/// </summary>
/// <param name="lpFileOp"></param>
/// <returns></returns>
[DllImport("shell32.dll", SetLastError = true)]
public static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);
/// <summary>
/// 将指定一个路径的文件删除到回收站
/// </summary>
/// <param name="IsNotify">是否弹出对话框提示确定操作,true为提示,否则为false</param>
/// <param name="filepath">指定的文件路径。允许使用通配符,如*.txt</param>
/// <returns>返回0成功,否则为其他值</returns>
public static int DeletetoRecycleBin(bool IsNotify, string filepath) {
SHFILEOPSTRUCT shfileOP = new SHFILEOPSTRUCT();
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x0010;
shfileOP.hwnd = 0;
shfileOP.wFunc = 0x0003;
shfileOP.pFrom = filepath + '/0';//必须加终止符,否则会被认为存在多个文件……&×%¥#!@#ナヌネナヌネスヒセソ
フヘヌネノナタソセスャナノヒニタャネノハニヒハヱ
shfileOP.pTo = null;
if(IsNotify)//当为true时,需要通知用户
shfileOP.flags = FOF_ALLOWUNDO;
else
shfileOP.flags = FOF_NOCONFIRMATION;
shfileOP.aborted = true;
shfileOP.hNameMappings = IntPtr.Zero;
shfileOP.ProgressTitle = "";
return SHFileOperation(ref shfileOP);
}
/// <summary>
/// 指定多个路径的文件删除到回收站
/// </summary>
/// <param name="IsNotify">是否弹出对话框提示确定操作,true为提示,否则为false</param>
/// <param name="filepathArray">一个string[],指定多个文件的路径,每个文件为filepathArray
/// 的一个元素。允许使用通配符,如*.txt,*.exe</param>
/// <returns>返回0成功,否则为其他值</returns>
public static int DeletetoRecycleBin(bool IsNotify, params string[] filepathArray) {
StringBuilder fileList = new StringBuilder();
for(int i = 0; i < filepathArray.Length; i++)
fileList.Append(filepathArray[i] + '/0');
SHFILEOPSTRUCT shfileOP = new SHFILEOPSTRUCT();
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x0010;
shfileOP.hwnd = 0;
shfileOP.wFunc = 0x0003;
shfileOP.pFrom = fileList.ToString();
shfileOP.pTo = null;
if(IsNotify)
shfileOP.flags = FOF_ALLOWUNDO;
else
shfileOP.flags = FOF_NOCONFIRMATION;
shfileOP.aborted = true;
shfileOP.hNameMappings = IntPtr.Zero;
shfileOP.ProgressTitle = "";
return SHFileOperation(ref shfileOP);
}
//------------------------------LOOK----------------------------
//被拷贝的或者被移动到目的路径,即可以是目录也是文件
/// <summary>
/// 指定一个新的目录路径,对原有的文件进行拷贝
/// </summary>
/// <param name="newPath">目的目录的路径,可以是磁盘上存在的或者不存在的路径
/// 如果这个参数指定的路径不存在,将提示是否创建一个新的路径。
/// </param>
/// <param name="filePath">指定一个路径的文件。可以是通配符</param>
/// <returns>返回0成功,否则为其他值</returns>
public static int CopyFile(string newPath, string filePath) {
SHFILEOPSTRUCT shfileOP = new SHFILEOPSTRUCT();
shfileOP.hwnd = 0;
shfileOP.pFrom = filePath + '/0';
shfileOP.pTo = newPath + '/0';
shfileOP.wFunc = (int)FileOpeArg.FO_COPY;
shfileOP.aborted = true;
shfileOP.flags = 0x0008;
shfileOP.hNameMappings = IntPtr.Zero;
shfileOP.ProgressTitle = "";
return SHFileOperation(ref shfileOP);
}
/// <summary>
/// 指定一个新的目录路径,将多个文件拷贝到其中
/// </summary>
/// <param name="newPath">目的目录的路径,可以是磁盘上存在的或者不存在的路径
/// 如果这个参数指定的路径不存在,将提示是否创建一个新的路径。</param>
/// <param name="filePathArray">指定多个文件的路径。可以是通配符</param>
/// <returns>返回0成功,否则为其他值</returns>
public static int CopyFile(string newPath, params string[] filePathArray) {
StringBuilder fileList = new StringBuilder();
foreach(string s in filePathArray)
fileList.Append(s + '/0');
SHFILEOPSTRUCT shfileOP = new SHFILEOPSTRUCT();
shfileOP.hwnd = 0;
shfileOP.pFrom = fileList.ToString();
shfileOP.pTo = newPath + '/0';
shfileOP.wFunc = (int)FileOpeArg.FO_COPY;
shfileOP.aborted = true;
shfileOP.flags = 0x0008;
shfileOP.hNameMappings = IntPtr.Zero;
shfileOP.ProgressTitle = null;
return SHFileOperation(ref shfileOP);
}
//FOF_MULTIDESTFILES多个目的文件用于拷贝可移动
/// <summary>
/// 指定一个新的目录路径,对原有的文件进行移动
/// </summary>
/// <param name="newPath">目的目录的路径,可以是磁盘上存在的或者不存在的路径
/// 如果这个参数指定的路径不存在,将提示是否创建一个新的路径</param>
/// <param name="filePath">指定一个路径的文件。可以是通配符</param>
/// <returns>返回0成功,否则为其他值</returns>
public static int MoveFile(string newPath, string filePath) {
SHFILEOPSTRUCT shfileOP = new SHFILEOPSTRUCT();
shfileOP.hwnd = 0;
shfileOP.pFrom = filePath + '/0';
shfileOP.pTo = newPath + '/0';
shfileOP.wFunc = (int)FileOpeArg.FO_MOVE;
shfileOP.aborted = true;
shfileOP.flags = 0x0008;
shfileOP.hNameMappings = IntPtr.Zero;
shfileOP.ProgressTitle = null;
return SHFileOperation(ref shfileOP);
}
/// <summary>
/// 指定一个新的目录路径,将多个文件移动到其中
/// </summary>
/// <param name="newPath">目的目录的路径,可以是磁盘上存在的或者不存在的路径
/// 如果这个参数指定的路径不存在,将提示是否创建一个新的路径</param>
/// <param name="filePathArray">指定多个文件的路径。可以是通配符</param>
/// <returns>返回0成功,否则为其他值</returns>
public static int MoveFile(string newPath, params string[] filePathArray) {
StringBuilder fileList = new StringBuilder();
foreach(string s in filePathArray)
fileList.Append(s + '/0');
SHFILEOPSTRUCT shfileOP = new SHFILEOPSTRUCT();
shfileOP.hwnd = 0;
shfileOP.pFrom = fileList.ToString();
shfileOP.pTo = newPath + '/0';
shfileOP.wFunc = (int)FileOpeArg.FO_MOVE;
shfileOP.aborted = true;
shfileOP.flags = 0x0008;
shfileOP.hNameMappings = IntPtr.Zero;
shfileOP.ProgressTitle = null;
return SHFileOperation(ref shfileOP);
}
};
//*************************SHGetFileInfo Function
/// <summary>
/// 该类用来获取文件的图标
/// </summary>
/// <remarks></remarks>
public class GetFileIcon : IDisposable {
/*
typedef struct _SHFILEINFO {
HICON hIcon;
int iIcon;
DWORD dwAttributes;
TCHAR szDisplayName[MAX_PATH];
TCHAR szTypeName[80];
} SHFILEINFO;
*/
/// <summary>
/// win32API的SHFILEINFO结构的托管类型
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO {
public IntPtr hIcon;
public int iIcon;
public uint attributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
/*
DWORD_PTR SHGetFileInfo(
LPCTSTR pszPath,
DWORD dwFileAttributes,
SHFILEINFO* psfi,
UINT cbFileInfo,
UINT uFlags
);
* */
/// <summary>
/// win32API的SHGetFileInfo函数的托管声明
/// </summary>
[DllImport("shell32.dll", SetLastError = true)]
public static extern int SHGetFileInfo(
string path,
uint fileAttributes,
out SHFILEINFO psfi,
int fileInfo,
uint flags
);
/* BOOL DestroyIcon( HICON hIcon
);*/
/// <summary>
/// win32API函数DestroyIcon的托管声明
/// 释放该图标对象以及使用它的任何内存
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
public static extern int DestroyIcon(IntPtr hIcon);
/* /// <summary>
/// 获取一个文件的小图标,返回System.Drawing.Icon
/// </summary>
/// <param name="filePath">指定文件的路径</param>
/// <returns>以System.Drawing.Icon类表示该文件的小图标</returns>
public static System.Drawing.Icon GetFileSmallIcon(string filePath) {
const int SHGFI_ICON = 0x000000100;
const int SHGFI_SMALLICON = 0x000000001;
SHFILEINFO fileinfo = new SHFILEINFO();
SHGetFileInfo(filePath + '/0', 0, out fileinfo, Marshal.SizeOf(fileinfo),
SHGFI_ICON | SHGFI_SMALLICON);
return System.Drawing.Icon.FromHandle(fileinfo.hIcon);
}*/
/*/// <summary>
/// 获取一个文件大图标,返回System.Drawing.Icon
/// </summary>
/// <param name="filePath">指定文件的路径</param>
/// <returns>以System.Drawing.Icon类表示该文件的大图标</returns>*/
/* public static void GetFileLargeIcon(string filePath,
System.Drawing.Icon fileIcon ) {
const int SHGFI_ICON = 0x000000100;
const int SHGFI_LARGEICON = 0x000000000;
SHFILEINFO fileinfo = new SHFILEINFO();
SHGetFileInfo(filePath+'/0', 0, out fileinfo, Marshal.SizeOf(fileinfo),
SHGFI_ICON | SHGFI_LARGEICON);
fileIcon= System.Drawing.Icon.FromHandle(fileinfo.hIcon);
DestroyIcon(fileinfo.hIcon);
}*/
const int SHGFI_ICON = 0x000000100;
const int SHGFI_LARGEICON = 0x000000000;
const int SHGFI_SMALLICON = 0x000000001;
/// <summary>
/// 获取用于表示指定文件图标的句柄,改变文件的路径将使该句柄发生变化
/// 或者调用GetFile_SmallIcon或者GetFile_LargeIcon这两个重载其中的一个之后,
/// 又调用另外一个方法,都会令句柄发生改变
/// </summary>
public IntPtr HIcon {
get {
return this.fileInfo.hIcon;
}
}
SHFILEINFO fileInfo = new SHFILEINFO();
/// <summary>
/// 初始化一个GetFileIcon_Fun对象,未指定文件的路径,
/// 必须在FilePath属性中指定,或者调用带有string参数
/// 的GetFile_SmallIcon和GetFile_LargeIcon方法,否则抛出异常
///
/// </summary>
public GetFileIcon() {
}
/// <summary>
/// 初始化一个GetFileIcon_Fun对象,并指定要获取图标的文件路径
/// 可通过FilePath属性改变文件的路径
/// </summary>
/// <param name="filepath">指定的要获取文件图标的路径</param>
public GetFileIcon(string filepath) {
this.filePath = filepath;
}
string filePath=null;
/// <summary>
/// 获取或设置要获取图标的文件的路径
/// </summary>
public string FilePath {
set { this.filePath = value; }
get { return this.filePath; }
}
/// <summary>
/// 获取在构造函数函数指定的路径,或者在FilePath属性设置路径的文件
/// 的小图标
/// </summary>
/// <returns>返回一个System.Drawing.Icon,表示指定文件的小图标</returns>
public System.Drawing.Icon GetFile_SmallIcon() {
this.SmallIcon();
return System.Drawing.Icon.FromHandle(this.fileInfo.hIcon);
}
/// <summary>
/// 获取指定路径的文件的小图标
/// </summary>
/// <param name="filepath">要获取小图标的文件的路径</param>
/// <returns>返回一个System.Drawing.Icon,表示指定文件的小图标</returns>
public System.Drawing.Icon GetFile_SmallIcon(string filepath) {
this.filePath = filepath;
this.SmallIcon();
return System.Drawing.Icon.FromHandle(this.fileInfo.hIcon);
}
/// <summary>
/// 获取在构造函数函数指定的路径,或者在FilePath属性设置路径的文件
/// </summary>
/// <returns>返回一个System.Drawing.Icon,表示指定文件的大图标</returns>
public System.Drawing.Icon GetFile_LargeIcon() {
this.LargeIcon();
return System.Drawing.Icon.FromHandle(this.fileInfo.hIcon);
}
/// <summary>
/// 获取指定路径的文件的大图标
/// </summary>
/// <param name="filepath">要获取大图标的文件的路径</param>
/// <returns>返回一个System.Drawing.Icon,表示指定文件的大图标</returns>
public System.Drawing.Icon GetFile_LargeIcon(string filepath) {
this.filePath = filepath;
this.LargeIcon();
return System.Drawing.Icon.FromHandle(this.fileInfo.hIcon);
}
void SmallIcon() {
SHGetFileInfo(this.filePath + '/0', 0, out this.fileInfo,
Marshal.SizeOf(this.fileInfo), SHGFI_ICON | SHGFI_SMALLICON);
}
void LargeIcon() {
SHGetFileInfo(this.filePath + '/0', 0, out this.fileInfo,
Marshal.SizeOf(this.fileInfo), SHGFI_ICON | SHGFI_LARGEICON);
}
/// <summary>
/// 显示释放GetFileIcon_Fun使用过的内存,释放图标的句柄
/// </summary>
public void Dispose() {
if(this.HIcon!= IntPtr.Zero) {
GC.KeepAlive(this.fileInfo);
DestroyIcon(this.fileInfo.hIcon);
this.fileInfo.hIcon = IntPtr.Zero;
}
if(this.filePath != null) {
GC.KeepAlive(this.filePath);
this.filePath = null;
}
}
~GetFileIcon() {
Dispose();
}
};
/// <summary>
/// 包含win32API的GetLastError和SetLastError函数
/// </summary>
public class winAPI_LastError {
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int GetLastError();
[DllImport("kernel32.dll", SetLastError = true)]
public static extern void SetLastError(int dwErrorCode);
};
/// <summary>
/// 表示一个矩形的位置和大小
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
};
public class CloseHandleForwin32API {
/// <summary>
/// 关闭之前打开的句柄
/// </summary>
/// <param name="hobject">之前打开的句柄</param>
/// <returns>成功返回true,否则为false</returns>
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hobject);
/// <summary>
/// INVALID_HANDLE_VALUE值,表示句柄无效
/// </summary>
public static readonly IntPtr INVALID_HANDLE_VALUE =new IntPtr( -1);//无效句柄
/// <summary>
/// ERROR_INVALID_HANDLE值,表示错误的的句柄
/// </summary>
public static readonly IntPtr ERROR_INVALID_HANDLE = new IntPtr ( 6);
};
/// <summary>
/// 包含部分WIN32API窗体的定义
/// </summary>
public static class LibWarp_API {
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto,EntryPoint="GetWindowLong")]
public static extern IntPtr GetWindowLongPtr(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
/// <summary>
/// SetWindowLong或GetWindowLong的第二个参数,用于获取或者设置指定窗体的扩展样式
/// </summary>
public static readonly int GWL_STYLE = -16;
/// <summary>
/// SetWindowLong或GetWindowLong的第二个参数,用于获取或者设置指定窗体的扩展样式
/// </summary>
public static readonly int GWL_EXSTYLE = -20;
/// <summary>
/// 字符长度的最长限度
/// </summary>
public static readonly int MAX_PATH = 260;
/// <summary>
/// SetWindowLong或GetWindowLong的第二个参数,用于获取或设置指定窗体的应用程序实例句柄的值,不是指针
/// ,如果要获取指针用get/get windowlongptr,注意vista不能调用get/setwindowlongptr
/// </summary>
public static readonly int GWL_HINSTANCE = -6;
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowText(IntPtr hwnd, StringBuilder text, int Max_Path);
/// <summary>
/// 获取窗体的类名
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetClassName(IntPtr hwnd, StringBuilder className, int Max_Path);
[DllImport("user32.dll", SetLastError = true)]
public
static extern IntPtr FindWindow(string ClassName, string windowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string className, string widnowText);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetClassInfoEx(IntPtr hinst, string classname, IntPtr wcx);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint DesiredAccess, bool bInheritHandle, uint id);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hProcess);
[DllImport("Psapi.dll", SetLastError = true)]
public static extern uint GetModuleFileNameEx(IntPtr hprocess, IntPtr hmodule, StringBuilder Filename,
uint size);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hwnd, ref uint processId);
/// <summary>
/// 进程的最大权限
/// </summary>
internal static uint PROCESS_ALL_ACCESS = 0xFFF | 0x000F0000 | 0x00100000;
[DllImport("user32.dll", SetLastError = true)]
public static extern bool ShowWindow(IntPtr hwnd, Cmd_SHOWWINDOWS cmdshow);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool ShowWindowAsync(IntPtr hwnd, Cmd_SHOWWINDOWS cmdshow);
};
/// <summary>
/// 获取窗体的信息
/// </summary>
public class WindowInfo:IDisposable {
/// <summary>
/// 公共构造函数,通过窗体句柄获取窗体信息
/// </summary>
/// <param name="hwnd">要获取信息的窗体的句柄</param>
public WindowInfo(IntPtr hwnd) {
WINDOWINFO wi = new WINDOWINFO();
wi.structSize = Marshal.SizeOf(wi);
if(hwnd == IntPtr.Zero)
throw new ArgumentException("无效句柄");
GetWindowInfo(hwnd, ref wi);
this.atomtype = wi.AtomWindowType;
this.cRect = wi.ClientRect;
this.w = wi.width;
this.h = wi.height;
this.style = wi.Style;
this.exstyle = wi.ExStyle;
this.cversion = wi.CreatorVersion;
this.winStatus = wi.WindowStatus;
this.wRect = wi.windowRect;
StringBuilder t = new StringBuilder(260);
LibWarp_API.GetWindowText(hwnd, t, LibWarp_API.MAX_PATH);
this.text = t.ToString();
LibWarp_API.GetClassName(hwnd, t, 260);
this.classname = t.ToString();
this.ins_add = LibWarp_API.GetWindowLongPtr(hwnd, -6);
uint id = 0;
LibWarp_API.GetWindowThreadProcessId(hwnd, ref id
); this.parentProcessId =(int) id;
this.phandle = LibWarp_API.OpenProcess(
LibWarp_API.PROCESS_ALL_ACCESS, true, id);
this.fileIcon = new GetFileIcon(this.Path);
this.sicon = this.fileIcon.GetFile_SmallIcon();
this.licon = this.fileIcon.GetFile_LargeIcon();
}
/// <summary>
/// 获取指定窗体的是否拥有指定的样式,如果有,返回true,如果没有,返回false
/// </summary>
/// <param name="hwnd">指定的窗体的句柄</param>
/// <param name="winStyle">窗体的样式值,可从WindowStyles静态成员中获取</param>
/// <returns>如果窗体包含参数指定的样式,返回true,如果不包含,返回false</returns>
public static bool QueryWindowStyle(IntPtr hwnd, WindowStyles winStyle) {
if(
(LibWarp_API.GetWindowLong(hwnd,LibWarp_API. GWL_STYLE) & (uint)winStyle) == (uint)winStyle)
return true;;
return false;
}
/// <summary>
/// 获取指定窗体的是否拥有指定的扩展样式,如果有,返回true,如果没有,返回false
/// </summary>
/// <param name="hwnd">指定窗体句柄</param>
/// <param name="winExStyle">指定的窗体扩展样式</param>
/// <returns>如果窗体包含参数指定的扩展样式,返回true,如果不包含,返回false</returns>
public static bool QueryWindowExStyle(IntPtr hwnd, WindowExStyles winExStyle) {
if(
(LibWarp_API.GetWindowLong(hwnd, LibWarp_API.GWL_EXSTYLE) & (uint)winExStyle) == (uint)winExStyle)
return true;
return false;
}
/// <summary>
/// 获取窗体信息函数
/// </summary>
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
/// <summary>
/// 该结构包含窗体的信息
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct WINDOWINFO {
public int structSize;
public RECT windowRect;
public RECT ClientRect;
public uint Style;
public uint ExStyle;
public uint WindowStatus;
public uint width;
public uint height;
public ushort AtomWindowType;
public ushort CreatorVersion;
};
#region 字段
RECT wRect;
RECT cRect;
uint style;
uint exstyle;
uint winStatus;
uint w;
uint h;
ushort atomtype;
ushort cversion;
string text;
string classname;
IntPtr ins_add;
int parentProcessId;
IntPtr phandle;
GetFileIcon fileIcon;
System.Drawing.Icon sicon;
System.Drawing.Icon licon;
#endregion 字段
#region 公共属性
/// <summary>
/// 获取窗体的矩形区域
/// </summary>
public RECT windowRect {
get {
return this.wRect;
}
}
/// <summary>
/// 获取客户端的矩形区域
/// </summary>
public RECT ClientRect {
get { return this.cRect; }
}
/// <summary>
/// 获取窗体的样式
/// </summary>
public uint Sytle {
get { return this.style; }
}
/// <summary>
/// 获取窗体的扩展样式
/// </summary>
public uint ExStyle {
get { return this.exstyle; }
}
/// <summary>
/// 获取窗体的状态,如果窗体是活动的,值为WS_ACTIVECAPTION,否则为0
/// </summary>
public uint WindowStatus {
get { return this.winStatus; }
}
/// <summary>
/// 获取窗体的边缘宽度,以像素为单位
/// </summary>
public uint Width {
get { return this.w; }
}
/// <summary>
/// 获取窗体的边缘高度,以像素为单位
/// </summary>
public uint High{
get { return this.h; }
}
/// <summary>
/// 获取窗体的类的Atom值
/// </summary>
public ushort AtomWindowType {
get { return this.atomtype; }
}
/// <summary>
/// 创建窗体应用程序的windows版本值
/// </summary>
public ushort CreatorVersion {
get {
return this.cversion;
}
}
/// <summary>
/// 获取窗体的标题
/// </summary>
public string Text {
get {
return this.text;
}
}
/// <summary>
/// 获取窗体的类名
/// </summary>
public string ClassName {
get {
return this.classname;
}
}
/// <summary>
/// 获取应用程序实例句柄
/// </summary>
public IntPtr Instance {
get {
//由于vista无法调用GetWindowLongPtr函数,因而无法得到实例的指针
//上面调用的仅是GetWindowLong,但返回intptr,这样做的道理也是得到一个指针(因为原来的GetWindowLong
//返回一个int),但这个指针不是真实的指针,仅仅只是一个指针指向一个int变量-即GetWindowLong的返回值。
//就是说,这一个伪地址,无效的内存地址,不能参与操作,仅作只读用的
return this.ins_add;
}
}
/// <summary>
/// 获取窗体所属进程的ID
/// </summary>
public int ParentProcessId {
get {
return this.parentProcessId;
}
}
/// <summary>
/// 以最大权限打开所属进程的句柄
/// </summary>
public IntPtr ParentProcessHandle {
get {
return this.phandle;
}
}
/// <summary>
/// 获取应用程序的路径
/// </summary>
public string Path {
get {
StringBuilder path = new StringBuilder(260);
LibWarp_API.GetModuleFileNameEx(this.ParentProcessHandle, IntPtr.Zero, path, 260);
return path.ToString();
}
}
/// <summary>
/// 获取该窗体的小图标
/// </summary>
public System.Drawing.Icon SmallIcon {
get {
return this.sicon;
}
}
/// <summary>
/// 获取该窗体的大图标
/// </summary>
public System.Drawing.Icon LargeIcon {
get {
return this.licon;
}
}
#endregion 公共属性
/// <summary>
/// 活动窗体状态的值
/// </summary>
public static readonly uint WS_ACTIVECAPTION = 0x0001;
#region IDisposable 成员
/// <summary>
/// 释放内存
/// </summary>
public void Dispose() {
this.Disposing();
}
#endregion
private void Disposing() {
if((int)this.phandle != -6) {
if(this.phandle != IntPtr.Zero) {
LibWarp_API.CloseHandle(this.phandle);
this.phandle = IntPtr.Zero;
}
}
this.fileIcon.Dispose();//释放图标
}
~WindowInfo(){
this.Disposing();
}
};
/// <summary>
/// 窗体样式
/// </summary>
public enum WindowStyles:uint {
WS_OVERLAPPED = 0x00000000,
WS_POPUP = 0x80000000,
WS_CHILD = 0x40000000,
WS_MINIMIZE = 0x20000000,
WS_VISIBLE = 0x10000000,
WS_DISABLED = 0x08000000,
WS_CLIPSIBLINGS = 0x04000000,
WS_CLIPCHILDREN = 0x02000000,
WS_MAXIMIZE = 0x01000000,
WS_CAPTION = 0x00C00000 ,
WS_BORDER = 0x00800000,
WS_DLGFRAME = 0x00400000,
WS_VSCROLL = 0x00200000,
WS_HSCROLL = 0x00100000,
WS_SYSMENU = 0x00080000,
WS_THICKFRAME = 0x00040000,
WS_GROUP = 0x00020000,
WS_TABSTOP = 0x00010000,
WS_MINIMIZEBOX = 0x00020000,
WS_MAXIMIZEBOX = 0x00010000,
WS_TILED = WS_OVERLAPPED,
WS_ICONIC = WS_MINIMIZE,
WS_SIZEBOX = WS_THICKFRAME,
WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW,
WS_OVERLAPPEDWINDOW = WS_OVERLAPPED | WS_CAPTION |
WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
WS_POPUPWINDOW =WS_POPUP|WS_BORDER|WS_SYSMENU,
WS_CHILDWINDOW = WS_CHILD,
};
/// <summary>
/// 窗体扩展样式
/// </summary>
public enum WindowExStyles : uint {
WS_EX_DLGMODALFRAME = 0x00000001,
WS_EX_NOPARENTNOTIFY = 0x00000004,
WS_EX_TOPMOST = 0x00000008,
WS_EX_ACCEPTFILES = 0x00000010,
WS_EX_TRANSPARENT = 0x00000020,
//xp以上
WS_EX_MDICHILD =0x00000040,
//xp以上
WS_EX_TOOLWINDOW = 0x00000080,
WS_EX_WINDOWEDGE = 0x00000100,
WS_EX_CLIENTEDGE = 0x00000200,
WS_EX_CONTEXTHELP = 0x00000400,
WS_EX_RIGHT = 0x00001000,
WS_EX_LEFT = 0x00000000,
WS_EX_RTLREADING = 0x00002000,
WS_EX_LTRREADING = 0x00000000,
WS_EX_LEFTSCROLLBAR = 0x00004000,
WS_EX_RIGHTSCROLLBAR = 0x00000000,
WS_EX_CONTROLPARENT = 0x00010000,
WS_EX_STATICEDGE = 0x00020000,
WS_EX_APPWINDOW = 0x00040000,
WS_EX_OVERLAPPEDWINDOW = WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
WS_EX_PALETTEWINDOW = WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW|WS_EX_TOPMOST,
WS_EX_LAYERED = 0x00080000,
WS_EX_NOINHERITLAYOUT = 0x00100000,
WS_EX_LAYOUTRTL = 0x00400000,
WS_EX_COMPOSITED = 0x02000000,
WS_EX_NOACTIVATE = 0x08000000
};
}//Win32API
}//FengCreateCrtDll
#region
//**************************************ShutDown_Fun
/* static class ShutDown_Fun {
/*typedef struct _LUID_AND_ATTRIBUTES { LUID Luid; DWORD Attributes;
} LUID_AND_ATTRIBUTES, *PLUID_AND_ATTRIBUTES;*/
/*
[StructLayout(LayoutKind.Sequential)]
private struct LUID_AND_ATTRIBUTES {
public long Luid;
public uint Attributes;
};
/*typedef struct _TOKEN_PRIVILEGES { DWORD PrivilegeCount;
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];
} TOKEN_PRIVILEGES, *PTOKEN_PRIVILEGES;*/
/*
[StructLayout(LayoutKind.Sequential)]
private struct TOKEN_PRIVILEGES {
public int count;
public LUID_AND_ATTRIBUTES[] Privileges;
};
/*BOOL OpenProcessToken(
HANDLE ProcessHandle,
DWORD DesiredAccess,
PHANDLE TokenHandle
);
*/
/*
//HANDLE GetCurrentProcess(void);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetCurrentProcess();
//进程有访问的标记
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool OpenProcessToken(
IntPtr ProcessH,
int access,
out IntPtr TokenHandle
);
/*BOOL LookupPrivilegeValue(
LPCTSTR lpSystemName,
LPCTSTR lpName,
PLUID lpLuid
);
*/
/*
//在本地系统里拥有唯一的标识符
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LookupPrivilegeValue(
string systemname,
string name,
out long Luid
);
/*BOOL AdjustTokenPrivileges(
HANDLE TokenHandle,
BOOL DisableAllPrivileges,
PTOKEN_PRIVILEGES NewState,
DWORD BufferLength,
PTOKEN_PRIVILEGES PreviousState,
PDWORD ReturnLength
);
*/
/*
//是否拥有asscee token特权
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool AdjustTokenPrivileges(
IntPtr TokenHandle,
bool DisableAllPrivileges,
TOKEN_PRIVILEGES NewState,
int BufferLenth,
out TOKEN_PRIVILEGES PreviousState,
out int ReturnLength
);
*/
/*
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
private static extern bool ExitWindowsEx(int DoFlag, int rea);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct TokPriv1Luid {
public int Count;
public long Luid;
public int Attr;
};
[Flags]
enum ShutdownFlags : int {
EWX_LOGOFF = 0x00000000,
EWX_SHUTDOWN = 0x00000001,
EWX_REBOOT = 0x00000002,
EWX_FORCE = 0x00000004,
EWX_POWEROFF = 0x00000008,
EWX_FORCEIFHUNG = 0x00000010,
};
static void ExitWinOPeration(ShutdownFlags flags) {
const int SE_PRIVILEGE_ENABLED = 0x00000002;
const int TOKEN_QUERY = 0x00000008;
const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, out
tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ExitWindowsEx((int)flags, 0);
}
internal static void ShutDown(bool Isforce) {
if(Isforce)
ExitWinOPeration(ShutdownFlags.EWX_FORCE | ShutdownFlags.EWX_SHUTDOWN);
else
ExitWinOPeration(ShutdownFlags.EWX_FORCEIFHUNG | ShutdownFlags.EWX_SHUTDOWN);
}
internal static void ReBoot(bool Isforce) {
if(Isforce)
ExitWinOPeration(ShutdownFlags.EWX_FORCE | ShutdownFlags.EWX_REBOOT);
else
ExitWinOPeration(ShutdownFlags.EWX_FORCEIFHUNG | ShutdownFlags.EWX_REBOOT);
}
internal static void LogOff(bool Isforce) {
if(Isforce)
ExitWinOPeration(ShutdownFlags.EWX_FORCE | ShutdownFlags.EWX_LOGOFF);
else
ExitWinOPeration(ShutdownFlags.EWX_FORCEIFHUNG | ShutdownFlags.EWX_LOGOFF);
}
internal static void PowerOff(bool Isforce) {
if(Isforce)
ExitWinOPeration(ShutdownFlags.EWX_FORCE | ShutdownFlags.EWX_POWEROFF);
else
ExitWinOPeration(ShutdownFlags.EWX_FORCEIFHUNG | ShutdownFlags.EWX_POWEROFF);
}
}
};*/
#endregion