实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; namespace DrawDragRect { public struct PaintStruct { public Graphics dc; public bool isPaint; }; public partial class MainForm : CDrawDragRect //Form { public int nDblClick; Image imgResize; Image img; public void LoadImage() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.Tiff)|*.BMP;*.JPG;*.GIF;*.Tiff "; if(DialogResult.OK == ofd.ShowDialog()) img = Image.FromFile(ofd.FileName); Invalidate(); nDblClick = -1; } public MainForm() { InitializeComponent(); Icon = new Icon(GetType(), "Icon1.ico"); Text = "DrawDragRect API for .NET"; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); //WindowState = FormWindowState.Minimized; WindowState = FormWindowState.Normal; LoadImage(); } public void DrawImage(PaintStruct ps) { if (img != null) { if (imgResize != null) imgResize.Dispose(); if (ClientRectangle.Width > 0 && ClientRectangle.Height > 0) { imgResize = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics dcTmp = Graphics.FromImage(imgResize); dcTmp.DrawImage(img, ClientRectangle); dcTmp.Dispose(); if (ps.isPaint) { Graphics dc = ps.dc; dc.DrawImage(imgResize, ClientRectangle); } else { Graphics dc = CreateGraphics(); dc.DrawImage(imgResize, ClientRectangle); ReflectedBLockText(dc, new Point(ClientRectangle.Width / 10, ClientRectangle.Height / 4)); dc.Dispose(); } } } } protected override void OnPaint(PaintEventArgs e) { PaintStruct ps; ps.dc = e.Graphics; ps.isPaint = true; DrawImage(ps); ReflectedBLockText(e.Graphics, new Point(ClientRectangle.Width/10, ClientRectangle.Height/4)); base.OnPaint(e); } protected override void OnMouseDoubleClick(MouseEventArgs e) { base.OnMouseDoubleClick(e); LoadImage(); } protected override void OnResize(EventArgs e) { PaintStruct ps; ps.dc = null; ps.isPaint = false; DrawImage(ps); base.OnResize(e); } protected override void OnMouseDown(MouseEventArgs e) { Invalidate(); nDblClick = 0; base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { if (rcNew.Width > 0 && rcNew.Height > 0); else { base.OnMouseUp(e); return; } if (nDblClick == -1) { base.OnMouseUp(e); return; } ChildFrm chFrm = new ChildFrm(); //Graphics dc = CreateGraphics(); chFrm.img = new Bitmap(rcNew.Width, rcNew.Height); //Image img = new Bitmap(rcNew.Width, rcNew.Height); Graphics dcMem = Graphics.FromImage(chFrm.img); //Graphics dcMem = Graphics.FromImage(img); dcMem.Clear(Color.Azure); dcMem.DrawImage(imgResize, new Rectangle(0, 0, rcNew.Width, rcNew.Height), rcNew, GraphicsUnit.Pixel); dcMem.Dispose(); chFrm.Size = new Size(chFrm.img.Width 4,chFrm.img.Height 34); chFrm.Show(); //dc.DrawImage(img, new Point(0, 0)); nDblClick = -1; base.OnMouseUp(e); } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Escape) { base.Close(); } return base.ProcessCmdKey(ref msg, keyData); } public void BlockText(Graphics dc, int a, int b) { //Font font = new Font("verdana", ClientRectangle.Width * 37 / 452, FontStyle.Bold); Font font = new Font("verdana", 12, FontStyle.Bold); int x = a; int y = b; string str = "With Left mouse button down\ndrag the mouse"; int j = 0; dc.SmoothingMode = SmoothingMode.HighQuality; dc.PixelOffsetMode = PixelOffsetMode.HighQuality; for (j = 0; j < 7; j = 1) { dc.DrawString(str, font, Brushes.Black, new PointF(x j, y j)); } SizeF sizef = dc.MeasureString(str, Font); sizef.Width /= 8; sizef.Height /= 8; PointF ptf = new PointF(x j, y j); RectangleF rectf = new RectangleF(ptf, sizef); LinearGradientBrush lgBrush = new LinearGradientBrush(rectf, Color.Yellow, Color.Red, LinearGradientMode.ForwardDiagonal); lgBrush.WrapMode = WrapMode.TileFlipXY; dc.DrawString(str, font, lgBrush, ptf); } public void ReflectedBLockText(Graphics dc, Point pt) { Point p = pt; p.Offset(0, ClientRectangle.Height * 100 / 351); BlockText(dc, p.X, p.Y); string str = "Resize or Double click"; //Font font = new Font("Tahoma", ClientRectangle.Width * 27 / 452, FontStyle.Bold); Font font = new Font("Tahoma", 15, FontStyle.Bold); SizeF sz = dc.MeasureString(str, font); StringFormat strFmt = StringFormat.GenericDefault; Rectangle rcTmp = new Rectangle(pt.X, pt.Y, (int)(sz.Width 1), (int)(sz.Height 1)); LinearGradientBrush brLinear = new LinearGradientBrush(new Point(0, rcTmp.Height / 4), new Point(0, rcTmp.Height / 8), Color.Red, Color.White); brLinear.WrapMode = WrapMode.TileFlipX; dc.TranslateTransform(pt.X, pt.Y); GraphicsState dcState = dc.Save(); int j = 0; for (j = 0; j < 5; j ) { dc.DrawString(str, font, Brushes.Yellow, new PointF(-j, j)); } dc.DrawString(str, font, brLinear, -j, j, strFmt); dc.ScaleTransform(1, -1); dc.DrawString(str, font, brLinear, -j, (float)-(1.59 * sz.Height), strFmt); dc.Restore(dcState); //dc.Dispose(); } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论