1. 程式人生 > 其它 >自動擷取桌面並按照所截圖的小圖找到桌面圖且點選

自動擷取桌面並按照所截圖的小圖找到桌面圖且點選

簡單設計介面

點選picture下方的按鈕進入桌面截圖

其中截圖需要定義的方法和屬性:

private ScreenForm sf = new ScreenForm();
private Thread thread;
private Bitmap curBitmap;
private System.Threading.Timer _timer;
private int cur;
public Form2()
{
InitializeComponent();
}
[Flags]
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
}
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[DllImport("user32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver,
string lpszDevice,
string lpszzouput,
IntPtr lpdate
);
[DllImport("user32.dll")]
public static extern bool BitBlt(
int hdcDest,
int x,
int y,
int widht,
int htght,
IntPtr hdcsrc,
int xsrc,
int ysrc,
System.Int32 dw
);

[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 // 光柵的處理數值
);

private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized; //窗體狀態預設大小
this.Activate();
_timer = new System.Threading.Timer(c => DisDataPlay(), null, 0, 1000);

//var therd = new Thread(new ThreadStart(DisDataPlay));
//thread = new Thread(new ThreadStart(DisDataPlay));
//thread.Name = "武功第一打手";
//thread.Start();


}

public void DisDataPlay()
{
if (checkBox1.Checked)
{
var val=Convert.ToInt32(textBox1.Text);
textBox1.Text = (val - 1).ToString();
if (val<=0)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "cmd.exe";//呼叫cmd.exe程式
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;//重定向標準輸入
process.StartInfo.RedirectStandardOutput = true;//重定向標準輸出
process.StartInfo.RedirectStandardError = true;//重定向標準出錯
process.StartInfo.CreateNoWindow = true;//不顯示黑視窗
process.Start();//開始呼叫執行
process.StandardInput.WriteLine("shutdown -s -f -t 0&exit");
//process.StandardInput.WriteLine(str + "&exit");//標準輸入str + "&exit",相等於在cmd黑視窗輸入str + "&exit"
process.StandardInput.AutoFlush = true;//重新整理緩衝流,執行緩衝區的命令,相當於輸入命令之後回車執行
process.WaitForExit();//等待退出
process.Close();//關閉程序
}
}

}

Application.DoEvents();
int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width; //螢幕寬度
int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height; //螢幕高度

Bitmap bmSave = new Bitmap(screenWidth, screenHeight);
Graphics g = Graphics.FromImage(bmSave);

g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy);
g.Dispose();
var url = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
//var dd = File.Exists(url);
//if (File.Exists(url))
//{
// File.Delete(url);
//}
try
{
//bmSave.Save(url, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception)
{


}

//var xiaotu = Application.StartupPath + "\\112233.png";
//var data = (Bitmap)this.ptb_start.Image;
//var pingmu = SaveImage();
//Bitmap xiaotubit = Image.FromFile(xiaotu) as Bitmap;
//Bitmap pingmubit = Image.FromFile(url) as Bitmap; ;

//var data = new FindSmallImgInBig().GetImageContains(xiaotubit, pingmubit, 0);
//MessageBox.Show(data.ToString());
//var rec = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\開始.png", url, 0);
if ((Bitmap)this.ptb_start.Image != null)
{
var rec = new FindSmallImgInBig().FindPicture((Bitmap)this.ptb_start.Image, bmSave, 0);
if (rec.Count > 0)
{
Cursor.Position = rec[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}

if ((Bitmap)this.ptb_center.Image != null)
{
//var rec1 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\結束.png", url, 0);
var rec1 = new FindSmallImgInBig().FindPicture((Bitmap)this.ptb_center.Image, bmSave, 0);
if (rec1.Count > 0)
{
Cursor.Position = rec1[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}

if ((Bitmap)this.ptb_stop.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最後.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.ptb_stop.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox1.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最後.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox1.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox2.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最後.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox2.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox3.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最後.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox3.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox4.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最後.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox4.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox5.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最後.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox5.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //這裡是移動滑鼠的程式碼
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}

g.Dispose();
bmSave.Dispose();
}

private void Form2_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
sf.ScreenShotOk += new EventHandler(ScreenShotOk_Click);
}

private void ScreenShotOk_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(sf.End.X - sf.Start.X, sf.End.Y - sf.Start.Y);
using (Graphics g = Graphics.FromImage(bmp))
{
int w = sf.End.X - sf.Start.X;
int h = sf.End.Y - sf.Start.Y;
Rectangle destRect = new Rectangle(0, 0, w + 1, h + 1);//在畫布上要顯示的區域(記得畫素加1)
Rectangle srcRect = new Rectangle(sf.Start.X, sf.Start.Y - 15, w + 1, h + 1);//影象上要擷取的區域
g.DrawImage(curBitmap, destRect, srcRect, GraphicsUnit.Pixel);//加影象繪製到畫布上
}
if(cur==1)
this.ptb_start.Image = bmp;
if (cur == 2)
this.ptb_center.Image = bmp;
if (cur == 3)
this.ptb_stop.Image = bmp;
if (cur == 4)
this.pictureBox1.Image = bmp;
if (cur == 5)
this.pictureBox2.Image = bmp;
if (cur == 6)
this.pictureBox3.Image = bmp;
if (cur == 7)
this.pictureBox4.Image = bmp;
if (cur == 8)
this.pictureBox5.Image = bmp;
this.Show();
}

private void btn_start_Click(object sender, EventArgs e)
{
cur = 1;
this.Hide();//隱藏當前
//this.ptb_start.LineHistory.Clear();//清除繪製的歷史線條
//this.ptb_start.RectHistory.Clear();
this.curBitmap = GetScreen();
sf.BackgroundImage = this.curBitmap;
sf.StartPosition = FormStartPosition.Manual;//起始位置
sf.ShowDialog();
}

public Bitmap GetScreen()
{
//獲取整個螢幕影象,不包括工作列
Rectangle ScreenArea = Screen.GetWorkingArea(this);
Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(0, 0, 0, 0, new Size(ScreenArea.Width, ScreenArea.Height));
}

return bmp;
}

private void Form2_KeyDown(object sender, KeyEventArgs e)
{
// Initialize the flag to false.


if (e.KeyCode == Keys.F5)
{
this.WindowState = FormWindowState.Minimized; //窗體狀態預設大小
this.Activate();
_timer = new System.Threading.Timer(c => DisDataPlay(), null, 0, 2000);

}
if (e.KeyCode == Keys.F10)
{
_timer.Change(-1, 10);
}

}