国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

C#實(shí)現(xiàn)類(lèi)似qq的屏幕截圖程序

2024-07-21 02:26:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

因?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)完成,累呀~~~~


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 克什克腾旗| 色达县| 镇江市| 克东县| 长泰县| 牙克石市| 专栏| 临安市| 淮安市| 平南县| 聊城市| 天峻县| 郯城县| 高阳县| 乡宁县| 乌鲁木齐市| 会同县| 宝丰县| 鄂托克前旗| 绍兴市| 阿瓦提县| 鄂温| 宾阳县| 沈阳市| 昌都县| 临澧县| 密云县| 兴安县| 久治县| 平遥县| 绿春县| 台湾省| 河东区| 富宁县| 汉中市| 宁远县| 安塞县| 黔东| 盖州市| 巨野县| 渑池县|