因?yàn)榻鼇?lái)想寫(xiě)個(gè)類(lèi)似于遠(yuǎn)程桌面監(jiān)控的程序,該程序中要用到屏幕捕捉.為實(shí)現(xiàn)該程序的一部分功能,做了個(gè)小demo.程序很簡(jiǎn)單,用到的技術(shù)也不多,只能實(shí)現(xiàn)類(lèi)似qq的截圖功能(方法雖然很笨)
程序流程如下:
1.截取整個(gè)屏幕并保存
2.新開(kāi)一個(gè)全屏窗口,將保存的屏幕作為背景
3.鼠標(biāo)拖動(dòng)改變截取范圍,右鍵取消
4.雙擊截取,保存在粘貼板,全屏窗口關(guān)閉
好了,下面的是代碼部分
首先新建一個(gè)項(xiàng)目screencutter(vs2005),將窗體名改為mainform,再新建一個(gè)窗體screenbody.
添加一個(gè)按鈕btncutter到screencutter并添加按鈕事件:
private void btncutter_click(object sender, eventargs e)
{
image img = new bitmap(screen.allscreens[0].bounds.width, screen.allscreens[0].bounds.height);
graphics g = graphics.fromimage(img);
g.copyfromscreen(new point(0, 0), new point(0, 0), screen.allscreens[0].bounds.size);
screenbody body = new screenbody();
body.backgroundimage = img;
body.show();
}screen.allscreens[0]是獲取當(dāng)前所有設(shè)備窗口的第一個(gè),我這里只有一個(gè)顯示器,當(dāng)然我就是第一個(gè).
利用graphics的copyfromscreen函數(shù)獲取當(dāng)前屏幕.
好了,現(xiàn)在按下按鈕全屏窗口就會(huì)出來(lái)了.
下面講全屏窗口screenbody,首先設(shè)置窗體的formborderstyle為none,然后聲明以下變量
private graphics mainpainter; //主畫(huà)筆
private pen pen; //就是筆咯
private bool isdowned; //判斷鼠標(biāo)是否按下
private bool rectready; //矩形是否繪制完成
private image baseimage; //基本圖形(原來(lái)的畫(huà)面)
private rectangle rect; //就是要保存的矩形
private point downpoint; //鼠標(biāo)按下的點(diǎn)
int tmpx;
int tmpy;
之后就是窗體的鼠標(biāo)函數(shù)了,里面很多代碼都沒(méi)有作出整理,看了一下,整理后的代碼應(yīng)該會(huì)更少更精簡(jiǎn)的
private void screenbody_doubleclick(object sender, eventargs e)
{
if (((mouseeventargs)e).button == mousebuttons.left &&rect.contains(((mouseeventargs)e).x, ((mouseeventargs)e).y))
{
//保存的時(shí)候有很多種方法的......我這里只用了這種
image memory = new bitmap(rect.width, rect.height);
graphics g = graphics.fromimage(memory);
g.copyfromscreen(rect.x + 1, rect.y + 1, 0, 0, rect.size);
clipboard.setimage(memory);
this.close();
}
}
private void screenbody_mousedown(object sender, mouseeventargs e)
{
if (e.button == mousebuttons.left)
{
isdowned = true;
if (rectready == false)
{
rect.x = e.x;
rect.y = e.y;
downpoint = new point(e.x, e.y);
}
if (rectready == true)
{
tmpx = e.x;
tmpy = e.y;
}
}
if (e.button == mousebuttons.right)
{
if (rectready != true)
{
this.close();
return;
}
mainpainter.drawimage(baseimage, 0, 0);
rectready = false;
}
}
private void screenbody_mouseup(object sender, mouseeventargs e)
{
if (e.button == mousebuttons.left)
{
isdowned = false;
rectready = true;
}
}
private void screenbody_mousemove(object sender, mouseeventargs e)
{
if (rectready == false)
{
if (isdowned == true)
{
image new = drawscreen((image)baseimage.clone(), e.x, e.y);
mainpainter.drawimage(new, 0, 0);
new.dispose();
}
}
if (rectready == true)
{
if (rect.contains(e.x, e.y))
{
//this.cursor = cursors.hand;
if (isdowned == true)
{
//和上一次的位置比較獲取偏移量
rect.x = rect.x + e.x - tmpx;
rect.y = rect.y + e.y - tmpy;
//記錄現(xiàn)在的位置
tmpx = e.x;
tmpy = e.y;
moverect((image)baseimage.clone(), rect);
}
}
}
}
private void screenbody_load(object sender, eventargs e)
{
this.windowstate = formwindowstate.maximized;
mainpainter = this.creategraphics();
pen = new pen(brushes.blue);
isdowned = false;
baseimage = this.backgroundimage;
rect = new rectangle();
rectready = false;
}
輔助函數(shù)
本來(lái)應(yīng)該寫(xiě)更多的輔助函數(shù)的,將窗體響應(yīng)函數(shù)里面的代碼放到里面來(lái),不過(guò)本人很懶,就這樣將就了.呵呵
private void drawrect(graphics painter, int mouse_x, int mouse_y)
{
int width = 0;
int heigth = 0;
if (mouse_y < rect.y)
{
rect.y = mouse_y;
heigth = downpoint.y - mouse_y;
}
else
{
heigth = mouse_y - downpoint.y;
}
if (mouse_x < rect.x)
{
rect.x = mouse_x;
width = downpoint.x - mouse_x;
}
else
{
width = mouse_x - downpoint.x;
}
rect.size = new size(width, heigth);
painter.drawrectangle(pen, rect);
}
private image drawscreen(image back, int mouse_x, int mouse_y)
{
graphics painter = graphics.fromimage(back);
drawrect(painter, mouse_x, mouse_y);
return back;
}
private void moverect(image image, rectangle rect)
{
graphics painter = graphics.fromimage(image);
painter.drawrectangle(pen, rect.x, rect.y, rect.width, rect.height);
drawrects(painter);
mainpainter.drawimage(image, 0, 0);
image.dispose();
}
到這里,代碼就算是寫(xiě)完了,運(yùn)行

截取結(jié)果,這里截取的邊界沒(méi)有控制好,所以還有邊界可以見(jiàn)到,稍微設(shè)置一下就可以了
好了,這個(gè)東東就這樣搞完了,接下來(lái)要做利用鉤子的了....希望能夠快點(diǎn)完成,累呀~~~~
新聞熱點(diǎn)
疑難解答
圖片精選