惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - 温温恭人

[导入]:创建单色画笔 (Visual C#) [导入]在运行时创建位图 (Visual C#) [导入]创建笔 (Visual C#) [导入]如何动态生成table(javascript) [导入]javascript分页 [导入]贡献一个新浪的幻灯片(javascript) [导入]解析XML并生成表格 [导入]css控制div居中 [导入]精典的一句 [导入]通过简易的前台代码实现无限二级域名转向(来自无忧 biyuan老矣) [导入]巧妙的让web来执行“计划任务”(转 biyuan老矣 ) [导入]由浅到深了解JavaScript类 - 温温恭人 [导入]DHTML中重要的属性方法 - 温温恭人 [导入](转贴)正则表达式学习心得体会(5) [导入](转贴)正则表达式学习心得体会(4) [导入]正则表达式学习心得体会(3)(转) [导入]正则表达式学习心得体会(2) [导入](转贴)正则表达式学习心得体会(1) - 温温恭人 [导入]使用透明叠加法美化文件上传界面 (http://www.script8.com/bbs/thread.asp?tid=6) [导入]用Javascript实现interface的类似功能 [导入]javascript总结 [导入]将程序集链接到 Word 或 Excel 文件 [导入]input高级限制级用法 [导入] js实现的动态多文件上传 (来自http://cms.bmw.net.cn) [导入]js用FileSystemObject 对象实现文件控制
[导入]。net图形操作(MSDN)
温温恭人 · 2007-06-19 · via 博客园 - 温温恭人

此示例说明如何在窗体上绘制实心矩形。

示例

System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0,0,200,300));
myBrush.Dispose();
formGraphics.Dispose();

编译代码

本示例需要:

  • 一个 Windows 应用程序项目。

代码需要处于 form 类的范围内。该窗体实例由 this 来表示。

可靠编程

应该始终对使用系统资源的任何对象(如 Brush Graphics 对象)调用 Dispose

此示例说明如何在窗体上绘制空心椭圆和矩形。

示例

private void DrawEllipse()
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawEllipse(myPen, new Rectangle(0,0,200,300));
myPen.Dispose();
formGraphics.Dispose();
}
private void DrawRectangle()
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawRectangle(myPen, new Rectangle(0,0,200,300));
myPen.Dispose();
formGraphics.Dispose();
}

编译代码

该示例需要:

  • 一个 Windows 应用程序项目。

代码需要处于 form 类的范围内。该窗体实例由 this 来表示。

可靠编程

应该始终对使用系统资源的任何对象(如 PenGraphics 对象)调用 Dispose

此示例说明如何在窗体上竖向绘制文本。

示例

private void DrawVerticalText()
{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
string drawString = "Sample Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150.0f;
float y = 50.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
}

编译代码

本示例需要:

  • 一个 Windows 应用程序项目。

代码需要处于 form 类的范围内。该窗体实例由 this 来表示。

可靠编程

应该始终对使用系统资源的任何对象(如 FontGraphics 对象)调用 Dispose

以下情况可能会导致异常:

  • 未安装 Arial 字体。

此示例说明如何对当前窗体进行打印预览的副本。

示例

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
CaptureScreen();
printPreviewDialog1.Show();
}

编译代码

本示例需要:

  • 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。
  • 名为 printPreviewDialog1 的 PrintPreviewDialog 组件,其 Document 属性设置为 printDocument1。
  • 名为 printButton 且包含 Click 事件处理程序的 Button 对象。

该示例代码替换现有的事件处理程序。单击 printButton 时会显示窗体的打印预览。

可靠编程

以下情况可能会导致异常:

  • 您没有访问该打印机的权限。
  • 您没有使用非托管代码的权限。
  • 没有安装打印机。
  • 该“打印预览”对话框以前被处置过。在关闭“打印预览”对话框后会出现该情况。

安全性

为了运行此示例,您必须具有执行非托管代码和访问打印机的权限。

此示例说明如何打印 DataGrid 控件。

示例

private void printGrid_Click(System.Object sender, System.EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size));
this.InvokePaint(dataGrid1, myPaintArgs);
}

编译代码

本示例需要:

  • 名为 printGrid 且含有 Click 事件处理程序的按钮控件。
  • 名为 dataGrid1 的 DataGrid 控件。
  • 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。

该示例代码替换现有的事件处理程序。

可靠编程

以下情况可能会导致异常:

  • 您没有访问该打印机的权限。
  • 没有安装打印机。

此示例说明如何打印文本文件。

示例

System.IO.StreamReader fileToPrint;
System.Drawing.Font printFont;
private void printButton_Click(object sender, EventArgs e)
{
string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
fileToPrint = new System.IO.StreamReader(printPath + @"\myFile.txt");
printFont = new System.Drawing.Font("Arial", 10);
printDocument1.Print();
fileToPrint.Close();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float yPos = 0f;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
while (count < linesPerPage)
{
line = fileToPrint.ReadLine();
if (line == null)
{
break;
}
yPos = topMargin + count * printFont.GetHeight(e.Graphics);
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
if (line != null)
{
e.HasMorePages = true;
}
}

编译代码

本示例需要:

  • 名为 printButton 且包含 Click 事件处理程序的按钮控件。
  • 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。

此示例还假定在您的桌面上有一个名为 myFile.txt 的文本文件。该示例代码替换现有的事件处理程序。

可靠编程

文件操作应被包括在适当的 try...catch...finally 块内。使用完它后,请始终调用 StreamReader.Close

以下情况可能会导致异常:

  • 您没有访问该打印机的权限。
  • 您没有访问文件系统的权限。
  • 没有安装打印机。
  • 未安装 Arial 字体。

安全性

为了运行此示例,您必须具有访问文件系统和打印机的权限。

此示例说明如何打印当前窗体的副本。

示例

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
CaptureScreen();
printDocument1.Print();
}

编译代码

本示例需要:

  • 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。
  • 名为 printButton 且包含 Click 事件处理程序的 Button 对象。

该示例代码替换现有的事件处理程序。单击 printButton 时,就会打印该窗体。

可靠编程

以下情况可能会导致异常:

  • 您没有访问该打印机的权限。
  • 您没有使用非托管代码的权限。
  • 没有安装打印机。

安全性

为了运行此示例,您必须具有执行非托管代码和访问打印机的权限。

此示例说明如何更改已有 Pen 对象的颜色。

示例

myPen.Color = System.Drawing.Color.PeachPuff;

编译代码

本示例需要:

  • 名为 myPenPen 对象。

可靠编程

您应该始终对使用系统资源的任何对象(如 Pen 对象)调用 Dispose