版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、高仿QQ截圖程序C#源碼using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;namespace _SCREEN_CAPTURE public partial class FrmCapture : Form public FrmCapture() InitializeComponent();
2、 this.FormBorderStyle = FormBorderStyle.None; this.Location = new Point(0, 0); this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); this.TopMost = true; this.ShowInTaskbar = false; m_MHook = new MouseHook(); this.FormClosing += (s, e) = m_MHook.UnLoadHook(); t
3、his.DelResource(); ; imageProcessBox1.MouseLeave += (s, e) = this.Cursor = Cursors.Default; /后期一些操作歷史記錄圖層 m_layer = new List(); private void DelResource() if (m_bmpLayerCurrent != null) m_bmpLayerCurrent.Dispose(); if (m_bmpLayerShow != null) m_bmpLayerShow.Dispose(); m_layer.Clear(); imageProcessBo
4、x1.DeleResource(); GC.Collect(); #region Properties private bool isCaptureCursor; / / 獲取或設(shè)置是否捕獲鼠標(biāo) / public bool IsCaptureCursor get return isCaptureCursor; set isCaptureCursor = value; / / 獲取或設(shè)置是否顯示圖像信息 / public bool ImgProcessBoxIsShowInfo get return imageProcessBox1.IsShowInfo; set imageProcessBox
5、1.IsShowInfo = value; / / 獲取或設(shè)置操作框點(diǎn)的顏色 / public Color ImgProcessBoxDotColor get return imageProcessBox1.DotColor; set imageProcessBox1.DotColor = value; / / 獲取或設(shè)置操作框邊框顏色 / public Color ImgProcessBoxLineColor get return imageProcessBox1.LineColor; set imageProcessBox1.LineColor = value; / / 獲取或設(shè)置放大圖形
6、的原始尺寸 / public Size ImgProcessBoxMagnifySize get return imageProcessBox1.MagnifySize; set imageProcessBox1.MagnifySize = value; / / 獲取或設(shè)置放大圖像的倍數(shù) / public int ImgProcessBoxMagnifyTimes get return imageProcessBox1.MagnifyTimes; set imageProcessBox1.MagnifyTimes = value; #endregion /初始化參數(shù) private void
7、InitMember() panel1.Visible = false; panel2.Visible = false; panel1.BackColor = Color.White; panel2.BackColor = Color.White; panel1.Height = tBtn_Finish.Bottom + 3; panel1.Width = tBtn_Finish.Right + 3; panel2.Height = colorBox1.Height; panel1.Paint += (s, e) = e.Graphics.DrawRectangle(Pens.SteelBlu
8、e, 0, 0, panel1.Width - 1, panel1.Height - 1); panel2.Paint += (s, e) = e.Graphics.DrawRectangle(Pens.SteelBlue, 0, 0, panel2.Width - 1, panel2.Height - 1); tBtn_Rect.Click += new EventHandler(selectToolButton_Click); tBtn_Ellipse.Click += new EventHandler(selectToolButton_Click); tBtn_Arrow.Click +
9、= new EventHandler(selectToolButton_Click); tBtn_Brush.Click += new EventHandler(selectToolButton_Click); tBtn_Text.Click += new EventHandler(selectToolButton_Click); tBtn_Close.Click += (s, e) = this.Close(); textBox1.BorderStyle = BorderStyle.None; textBox1.Visible = false; textBox1.ForeColor = Co
10、lor.Red; colorBox1.ColorChanged += (s, e) = textBox1.ForeColor = e.Color; private MouseHook m_MHook; private List m_layer; /記錄歷史圖層 private bool m_isStartDraw; private Point m_ptOriginal; private Point m_ptCurrent; private Bitmap m_bmpLayerCurrent; private Bitmap m_bmpLayerShow; private void FrmCaptu
11、re_Load(object sender, EventArgs e) this.InitMember(); imageProcessBox1.BaseImage = this.GetScreen(); m_MHook.SetHook(); m_MHook.MHookEvent += new MouseHook.MHookEventHandler(m_MHook_MHookEvent); imageProcessBox1.IsDrawOperationDot = false; this.BeginInvoke(new MethodInvoker() = this.Enabled = false
12、); timer1.Interval = 500; timer1.Enabled = true; private void m_MHook_MHookEvent(object sender, MHookEventArgs e) /如果窗體禁用 調(diào)用控件的方法設(shè)置信息顯示位置 if (!this.Enabled) /貌似Hook不能精確坐標(biāo)(Hook最先執(zhí)行執(zhí) 執(zhí)行完后的坐標(biāo)可能與執(zhí)行時傳入的坐標(biāo)發(fā)生了變化 猜測是這樣) 所以放置了一個timer檢測 imageProcessBox1.SetInfoPoint(MousePosition.X, MousePosition.Y); /鼠標(biāo)點(diǎn)下恢復(fù)窗
13、體禁用 if (e.MButton = ButtonStatus.LeftDown | e.MButton = ButtonStatus.RightDown) this.Enabled = true; imageProcessBox1.IsDrawOperationDot = true; #region 在imageProcessBox_MouseUp中完成了 /if (e.MButton = ButtonStatus.LeftUp) / /if (imageProcessBox1.SelectedRectangle.Width 5 / / | imageProcessBox1.Selecte
14、dRectangle.Height 5) / / /如果選取區(qū)域不符要求 向控件模擬鼠標(biāo)抬起 然后禁用窗體 / / /(Hook事件先于控件事件 禁用控件時模擬一個鼠標(biāo)抬起) / / Win32.SendMessage(imageProcessBox1.Handle, Win32.WM_LBUTTONUP, / / IntPtr.Zero, (IntPtr)(MousePosition.Y this.Close(); /有繪制的情況 情況繼續(xù)禁用窗體 this.Enabled = false; imageProcessBox1.ClearDraw(); imageProcessBox1.Can
15、Reset = true; imageProcessBox1.IsDrawOperationDot = false; m_layer.Clear(); /清空歷史記錄 m_bmpLayerCurrent = null; m_bmpLayerShow = null; ClearToolBarBtnSelected(); panel1.Visible = false; panel2.Visible = false; #endregion #region 找尋窗體 if (!this.Enabled) this.FoundAndDrawWindowRect(); #endregion /工具條前五個
16、按鈕綁定的公共事件 private void selectToolButton_Click(object sender, EventArgs e) panel2.Visible = (ToolButton)sender).IsSelected; if (panel2.Visible) imageProcessBox1.CanReset = false; else imageProcessBox1.CanReset = m_layer.Count = 0; this.SetToolBarLocation(); #region 截圖后的一些后期繪制 private void imageProces
17、sBox1_MouseDown(object sender, MouseEventArgs e) if (imageProcessBox1.Cursor != Cursors.SizeAll & imageProcessBox1.Cursor != Cursors.Default) panel1.Visible = false; /表示改變選取大小 隱藏工具條 /若果在選取類點(diǎn)擊 并且有選擇工具 if (e.Button = MouseButtons.Left & imageProcessBox1.IsDrawed & HaveSelectedToolButton() if (imagePro
18、cessBox1.SelectedRectangle.Contains(e.Location) if (tBtn_Text.IsSelected) /如果選擇的是繪制文本 彈出文本框 textBox1.Location = e.Location; textBox1.Visible = true; textBox1.Focus(); return; m_isStartDraw = true; Cursor.Clip = imageProcessBox1.SelectedRectangle; m_ptOriginal = e.Location; private void imageProcessB
19、ox1_MouseMove(object sender, MouseEventArgs e) m_ptCurrent = e.Location; /根據(jù)是否選擇有工具決定 鼠標(biāo)指針樣式 if (imageProcessBox1.SelectedRectangle.Contains(e.Location) & HaveSelectedToolButton() & imageProcessBox1.IsDrawed) this.Cursor = Cursors.Cross; else if (!imageProcessBox1.SelectedRectangle.Contains(e.Locati
20、on) this.Cursor = Cursors.Default; if (imageProcessBox1.IsStartDraw & panel1.Visible) /在重置選取的時候 重置工具條位置(成立于移動選取的時候) this.SetToolBarLocation(); if (m_isStartDraw & m_bmpLayerShow != null) /如果在區(qū)域內(nèi)點(diǎn)下那么繪制相應(yīng)圖形 using (Graphics g = Graphics.FromImage(m_bmpLayerShow) int tempWidth = 1; if (toolButton2.IsSel
21、ected) tempWidth = 3; if (toolButton3.IsSelected) tempWidth = 5; Pen p = new Pen(colorBox1.SelectedColor, tempWidth); #region 繪制矩形 if (tBtn_Rect.IsSelected) int tempX = e.X - m_ptOriginal.X 0 ? m_ptOriginal.X : e.X; int tempY = e.Y - m_ptOriginal.Y 0 ? m_ptOriginal.Y : e.Y; g.Clear(Color.Transparent
22、); g.DrawRectangle(p, tempX - imageProcessBox1.SelectedRectangle.Left, tempY - imageProcessBox1.SelectedRectangle.Top, Math.Abs(e.X - m_ptOriginal.X), Math.Abs(e.Y - m_ptOriginal.Y); imageProcessBox1.Invalidate(); #endregion #region 繪制圓形 if (tBtn_Ellipse.IsSelected) g.DrawLine(Pens.Red, 0, 0, 200, 2
23、00); g.Clear(Color.Transparent); g.DrawEllipse(p, m_ptOriginal.X - imageProcessBox1.SelectedRectangle.Left, m_ptOriginal.Y - imageProcessBox1.SelectedRectangle.Top, e.X - m_ptOriginal.X, e.Y - m_ptOriginal.Y); imageProcessBox1.Invalidate(); #endregion #region 繪制箭頭 if (tBtn_Arrow.IsSelected) g.Clear(
24、Color.Transparent); System.Drawing.Drawing2D.AdjustableArrowCap lineArrow = new System.Drawing.Drawing2D.AdjustableArrowCap(4, 4, true); p.CustomEndCap = lineArrow; g.DrawLine(p, (Point)(Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location), (Point)(Size)m_ptCurrent - (Size)imagePro
25、cessBox1.SelectedRectangle.Location); imageProcessBox1.Invalidate(); #endregion #region 繪制線條 if (tBtn_Brush.IsSelected) Point ptTemp = (Point)(Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location); p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round; g.DrawLine(p, ptTemp, (Point)(S
26、ize)e.Location - (Size)imageProcessBox1.SelectedRectangle.Location); m_ptOriginal = e.Location; imageProcessBox1.Invalidate(); #endregion p.Dispose(); private void imageProcessBox1_MouseUp(object sender, MouseEventArgs e) if (!imageProcessBox1.IsDrawed) /如果沒有成功繪制選取 繼續(xù)禁用窗體 this.Enabled = false; image
27、ProcessBox1.IsDrawOperationDot = false; else if (!panel1.Visible) /否則顯示工具條 this.SetToolBarLocation(); /重置工具條位置 panel1.Visible = true; m_bmpLayerCurrent = imageProcessBox1.GetResultBmp(); /獲取選取圖形 m_bmpLayerShow = new Bitmap(m_bmpLayerCurrent.Width, m_bmpLayerCurrent.Height); /如果移動了選取位置 重新獲取選取的圖形 if (
28、imageProcessBox1.Cursor = Cursors.SizeAll & m_ptOriginal != e.Location) m_bmpLayerCurrent = imageProcessBox1.GetResultBmp(); if (!m_isStartDraw) return; Cursor.Clip = Rectangle.Empty; m_isStartDraw = false; if (e.Location = m_ptOriginal & !tBtn_Brush.IsSelected) return; this.SetLayer(); /將繪制的圖形繪制到歷史
29、圖層中 /繪制后期操作 private void imageProcessBox1_Paint(object sender, PaintEventArgs e) Graphics g = e.Graphics; if (m_layer.Count 0) /繪制保存的歷史記錄的最后一張圖 g.DrawImage(m_layerm_layer.Count - 1, imageProcessBox1.SelectedRectangle.Location); if (m_bmpLayerShow != null) /繪制當(dāng)前正在拖動繪制的圖形(即鼠標(biāo)點(diǎn)下還沒有抬起確認(rèn)的圖形) g.DrawImage(
30、m_bmpLayerShow, imageProcessBox1.SelectedRectangle.Location); #endregion /文本改變時重置文本框大小 private void textBox1_TextChanged(object sender, EventArgs e) Size se = TextRenderer.MeasureText(textBox1.Text, textBox1.Font); textBox1.Size = se.IsEmpty ? new Size(50, textBox1.Font.Height) : se; /文本框失去焦點(diǎn)時 繪制文本
31、private void textBox1_Validating(object sender, CancelEventArgs e) textBox1.Visible = false; if (string.IsNullOrEmpty(textBox1.Text.Trim() textBox1.Text = ; return; using (Graphics g = Graphics.FromImage(m_bmpLayerCurrent) SolidBrush sb = new SolidBrush(colorBox1.SelectedColor); g.DrawString(textBox
32、1.Text, textBox1.Font, sb, textBox1.Left - imageProcessBox1.SelectedRectangle.Left, textBox1.Top - imageProcessBox1.SelectedRectangle.Top); sb.Dispose(); textBox1.Text = ; this.SetLayer(); /將文本繪制到當(dāng)前圖層并存入歷史記錄 imageProcessBox1.Invalidate(); /窗體大小改變時重置字體 從控件中獲取字體大小 private void textBox1_Resize(object s
33、ender, EventArgs e) /在三個大小選擇的按鈕點(diǎn)擊中設(shè)置字體大小太麻煩 所以Resize中獲取設(shè)置 int se = 10; if (toolButton2.IsSelected) se = 12; if (toolButton3.IsSelected) se = 14; if (this.textBox1.Font.Height = se) return; textBox1.Font = new Font(this.Font.FontFamily, se); /撤銷 private void tBtn_Cancel_Click(object sender, EventArgs
34、 e) using (Graphics g = Graphics.FromImage(m_bmpLayerShow) g.Clear(Color.Transparent); /情況當(dāng)前臨時顯示的圖像 if (m_layer.Count 0) /刪除最后一層 m_layer.RemoveAt(m_layer.Count - 1); if (m_layer.Count 0) m_bmpLayerCurrent = m_layerm_layer.Count - 1.Clone(new Rectangle(0, 0, m_bmpLayerCurrent.Width, m_bmpLayerCurrent
35、.Height), m_bmpLayerCurrent.PixelFormat); else m_bmpLayerCurrent = imageProcessBox1.GetResultBmp(); imageProcessBox1.Invalidate(); imageProcessBox1.CanReset = m_layer.Count = 0 & !HaveSelectedToolButton(); else /如果沒有歷史記錄則取消本次截圖 this.Enabled = false; imageProcessBox1.ClearDraw(); imageProcessBox1.IsD
36、rawOperationDot = false; panel1.Visible = false; panel2.Visible = false; private void tBtn_Save_Click(object sender, EventArgs e) SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = 位圖(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg; saveDlg.FilterIndex = 1; saveDlg.FileName = CAPTURE_ + GetTimeString();
37、 if (saveDlg.ShowDialog() = DialogResult.OK) switch (saveDlg.FilterIndex) case 1: m_bmpLayerCurrent.Clone(new Rectangle(0, 0, m_bmpLayerCurrent.Width, m_bmpLayerCurrent.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb).Save(saveDlg.FileName, System.Drawing.Imaging.ImageFormat.Bmp); this.Cl
38、ose(); break; case 2: m_bmpLayerCurrent.Save(saveDlg.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); this.Close(); break; /將圖像保存到剪貼板 private void tBtn_Finish_Click(object sender, EventArgs e) Clipboard.SetImage(m_bmpLayerCurrent); this.Close(); private void imageProcessBox1_DoubleClick(object se
39、nder, EventArgs e) Clipboard.SetImage(m_bmpLayerCurrent); this.Close(); private void timer1_Tick(object sender, EventArgs e) if (!this.Enabled) imageProcessBox1.SetInfoPoint(MousePosition.X, MousePosition.Y); /根據(jù)鼠標(biāo)位置找尋窗體平繪制邊框 private void FoundAndDrawWindowRect() Win32.LPPOINT pt = new Win32.LPPOINT(); pt.X = MousePosition.X; pt.Y = MousePosition.Y; IntPtr hWnd = Win
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年辦公樓消防安全知識試題庫及答案
- 舉一反三系列高考高中數(shù)學(xué)同步及復(fù)習(xí)資料人教A版必修1專題3.5 冪函數(shù)-重難點(diǎn)題型精講(含答案及解析)
- 2024-2025學(xué)年九年級物理全冊第二十章電與磁第四節(jié)電動機(jī)習(xí)題無答案新版新人教版
- 2025屆高考?xì)v史一輪復(fù)習(xí)模塊1政治文明歷程第5單元當(dāng)今世界政治格局的多極化趨勢第17講兩極世界的形成課時跟蹤含解析新人教版
- 電工理論考試過關(guān)自測題帶答案
- 二年級數(shù)學(xué)100以內(nèi)加減法豎式計算題綜合考核訓(xùn)練題帶答案
- 江西省上饒市示范名校2025屆生物高一上期末調(diào)研試題含解析
- 河南省重點(diǎn)高中2025屆高二上數(shù)學(xué)期末調(diào)研模擬試題含解析
- 2025屆黑龍江省哈爾濱市第三十二中學(xué)生物高一上期末聯(lián)考模擬試題含解析
- 2025屆湖南省衡陽八中生物高一第一學(xué)期期末復(fù)習(xí)檢測模擬試題含解析
- 收費(fèi)管理員績效考核細(xì)則
- 小學(xué)數(shù)學(xué)西南師大五年級下冊二分?jǐn)?shù)《分?jǐn)?shù)與小數(shù)》教案
- 養(yǎng)老院機(jī)構(gòu)組織架構(gòu)圖
- PCB.A焊錫作業(yè)標(biāo)準(zhǔn)及通用檢驗標(biāo)準(zhǔn)
- 色粉-MSDS物質(zhì)安全技術(shù)資料
- 交通疏解施工安全交底
- 無塵車間潔凈度標(biāo)準(zhǔn)
- 伽瑪萊士說明書
- 中國傳統(tǒng)文化-點(diǎn)茶課件-高中主題班會
- 8.2《小二黑結(jié)婚》課件37張-統(tǒng)編版高中語文選擇性必修中冊
- DB32-T 3916-2020建筑地基基礎(chǔ)檢測規(guī)程-(高清現(xiàn)行)
評論
0/150
提交評論