using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace remoteClass
{
///
/// 远程监控操作类
///
public class remoteClass:System.MarshalByRefObject
{
public remoteClass()
{
}
#region get、delete 、run process
///
/// 获取当前进程名数组
///
/// 进程名的数组
public string[] getProcessName()
{
//获取执行本组件的本机所以进程数组
Process[] myProcesses=Process.GetProcesses(".");
string[] processNames=new string[myProcesses.Length];
for(int i=0;i
{
processNames[i]=myProcesses[i].ProcessName;
}
return processNames;
}
///
/// 结束指定的进程
///
/// 要结束的进程名称
/// 操作是否成功
public int endProcess(string processName)
{
//在本机中获取指定名称的进程
Process[] myProcess=Process.GetProcessesByName(processName,".");
myProcess[0].CloseMainWindow();
myProcess[0].Kill();
return 1;
}
///
/// 运行指定位置的程序
///
/// 程序所在的路径
public void exeProcess(string path)
{
Process process1=new Process();
process1.StartInfo.FileName=path;
process1.Start();
}
#endregion
#region send message
///
/// 显示消息
///
/// 要显示的消息
public void showMessage(string message)
{
this.message=message;
//多线程显示消息
Thread t=new Thread(new ThreadStart(m));
t.Start();
}
private string message;
private void m()
{
MessageBox.Show(message,"信使",MessageBoxButtons.OKCancel);
}
#endregion
#region get file or dir's info
///
/// 获取指定文件夹的文件信息列表
///
/// 指定的文件夹路径
/// 文件信息列表
public FileInfo[] getFileInfo(string path)
{
DirectoryInfo d=new DirectoryInfo(path);
return d.GetFiles();
}
///
/// 获取指定文件夹的子文件夹列表
///
/// 指定的文件夹路径
/// 文件夹信息列表
public DirectoryInfo[] getDir(string path)
{
DirectoryInfo d=new DirectoryInfo(path);
return d.GetDirectories();
}
#endregion
#region delete file or dir
///
/// 删除指定的文件
///
/// 要删除的文件路径
public void DelFile(string path)
{
File.Delete(path);
}
///
/// 删除指定的文件夹,包含子文件夹和其中的所有文件
///
/// 要删除的文件夹的路径
public void DelDir(string path)
{
Directory.Delete(path,true);
}
#endregion
#region keybroad event
[DllImport("User32.dll")]
public static extern void keybd_event(int d,byte i,System.Int32 q,System.IntPtr s);//模拟键盘事件
///
/// 模拟按下一个按键
///
/// 要摹拟的按键的键值
public void keyDown(int keyValue)
{
keybd_event(keyValue,0,0,(IntPtr)0);
}
///
/// 模拟释放一个按键
///
/// 要摹拟的按键的键值
public void keyPress(int keyValue)
{
keybd_event(keyValue,0,2,(IntPtr)0);
}
///
/// 模拟一次完整的按键过程
///
/// 要摹拟的按键的键值
public void key(int keyValue)
{
keybd_event(keyValue,0,0,(IntPtr)0);
keybd_event(keyValue,0,2,(IntPtr)0);
}
///
/// 模拟一次完整的按键过程
///
/// 要摹拟的按键的键值
/// 要处理的窗口句柄
public void key(int keyValue,IntPtr ptr)
{
keybd_event(keyValue,0,0,ptr);
keybd_event(keyValue,0,2,ptr);
}
#endregion
#region cut screen
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , // 目标 DC的句柄
int nXDest ,
int nYDest ,
int nWidth ,
int nHeight ,
IntPtr hdcSrc , // 源DC的句柄
int nXSrc ,
int nYSrc ,
System.Int32 dwRop // 光栅的处理数值
) ;
///
/// 截屏
///
///
public Stream GetImage()
{
//得到一个最大化没有标题栏的窗体,为了截取整个屏幕
Form f=new Form();
f.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;
f.WindowState=System.Windows.Forms.FormWindowState.Maximized;
//获得当前屏幕的大小
Rectangle rect = new Rectangle () ;
rect = Screen.GetBounds (f) ;
//创建一个以当前屏幕为模板的图象
Graphics g1 = f.CreateGraphics ( ) ;
//创建以屏幕大小为标准的位图
Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;
Graphics g2 = Graphics.FromImage ( MyImage ) ;
//得到屏幕的DC
IntPtr dc1 = g1.GetHdc ( ) ;
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc ( ) ;
//调用此API函数,实现屏幕捕获
BitBlt( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
//释放掉屏幕的DC
g1.ReleaseHdc ( dc1 ) ;
//释放掉Bitmap的DC
g2.ReleaseHdc ( dc2 ) ;
f.Dispose();
Stream s=new System.IO.MemoryStream();
//以JPG文件格式来保存
MyImage.Save(s,ImageFormat.Jpeg);
MyImage.Dispose();
return s;
}
#endregion
#region lock mouse
[StructLayout(LayoutKind.Explicit)]
public struct Rect
{
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
}
[DllImport("User32.dll")]
public static extern void ClipCursor(ref Rect rect);
///
/// 锁定鼠标在特定的矩形区域
///
/// top
/// left
/// right
/// buttom
public void lockMouse(int top,int left,int right,int buttom)
{
Rect r;
r.bottom=buttom;
r.left=left;
r.top=top;
r.right=right;
ClipCursor(ref r);
}
#endregion
#region mouse click
//鼠标单击事件
//mouse_event API中的鼠标模拟方法
[DllImport("User32.dll")]
public static extern void mouse_event(int dwFlags,int dx,int dy,uint dwData,ulong dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN=2;
private const int MOUSEEVENTF_LEFTUP=4;
private const int MOUSEEVENTF_RIGHTDOWN=8;
private const int MOUSEEVENTF_RIGHTUP=16;
[DllImport("User32.dll")]
public static extern bool SetCursorPos(int x,int y); //定位鼠标的位置
///
/// 模拟鼠标左键单击
///
/// x的相对屏幕的相对位置
/// y的相对屏幕的相对位置
public void mouse_Click(float x,float y)
{
//计算相对于屏幕的绝对位置
int screenX=(int)(x*Screen.PrimaryScreen.Bounds.Width);
int screenY=(int)(y*Screen.PrimaryScreen.Bounds.Height);
//在屏幕上定位鼠标事件的位置
SetCursorPos(screenX,screenY);
mouse_event(MOUSEEVENTF_LEFTDOWN,screenX,screenY,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,screenX,screenY,0,0);
}
///
/// 模拟鼠标左键双击
///
/// x的相对屏幕的相对位置
/// y的相对屏幕的相对位置
public void mouse_DoubleClick(float x,float y)
{
int screenX=(int)(x*Screen.PrimaryScreen.Bounds.Width);
int screenY=(int)(y*Screen.PrimaryScreen.Bounds.Height);
SetCursorPos(screenX,screenY);
mouse_event(MOUSEEVENTF_LEFTDOWN,screenX,screenY,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,screenX,screenY,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN,screenX,screenY,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,screenX,screenY,0,0);
}
///
/// 模拟鼠标右键单击
///
/// x的相对屏幕的相对位置
/// y的相对屏幕的相对位置
public void mouse_rightClick(float x,float y)
{
int screenX=(int)(x*Screen.PrimaryScreen.Bounds.Width);
int screenY=(int)(y*Screen.PrimaryScreen.Bounds.Height);
SetCursorPos(screenX,screenY);
mouse_event(MOUSEEVENTF_RIGHTDOWN,screenX,screenY,0,0);
mouse_event(MOUSEEVENTF_RIGHTUP,screenX,screenY,0,0);
}
#endregion
#region cdrom event
[DllImport("winmm.dll")]
public static extern long mciSendStringA (string lpstrCommand,string lpstrReturnString,long uReturnLength,long hwndCallback);
// lpstrCommand 这是控制命令参数
// lpstrReturnString 这是返回值
// uReturnLength 返回值长度参数
///
/// 打开光驱
///
public void OpenCDRom()
{
mciSendStringA("set CDAudio door open", "", 0, 0);
}
///
/// 关闭光驱
///
public void CloseCDRom()
{
mciSendStringA("set CDAudio door closed", "", 0, 0);
}
#endregion
}
}