怎么在控件上绘制图形,如在picturebox上写字

foxxiap 2003-02-20 04:55:03
Graphics g=pictureBox1.CreateGraphics();
g.DrawRectangle(new Pen(Color.AntiqueWhite,3),0,0,118,270);
//g.DrawString("sg.com.cn",new Font("Arial",10,FontStyle.Bold|FontStyle.Italic),SystemBrushes.ActiveCaptionText,new PointF(10,10));
g.SmoothingMode=SmoothingMode.HighQuality; g.DrawString("sg.com.cn",new Font("Arial",10,FontStyle.Bold|FontStyle.Italic),SystemBrushes.ActiveCaptionText,new PointF(10,10));


不成
...全文
475 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sandy2001 2003-02-21
  • 打赏
  • 举报
回复
你是不是把上面的代码写到Form_Load里面了?那样肯定先是不出来,如果那样你需要自己重写pictureBox控件
添加一个组建类 如(picture.cs)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
在那个类中添加以上名字空间
在重载 OnPaint事件
protected override void OnPaint(PaintEventArgs e)
{
Graphics g =e.Graphics;
Font fn =new Font("Ariel" , 9 , FontStyle.Bold );
SolidBrush brush=new SolidBrush(Color.Red);
g.DrawString("test",fn,brush,0,0);
base.OnPaint(e);
}

然后再你的那个Form的Form_Load中加入一下代码就OK了
private void Form1_Load(object sender, System.EventArgs e)
{
picture pic=new picture();
pic.BackColor=Color.Black;
pic.Visible=true;
pic.Location = new System.Drawing.Point(0, 0);

this.Controls.AddRange(new System.Windows.Forms.Control[] { pic });
}
foxxiap 2003-02-21
  • 打赏
  • 举报
回复
还是不成,不报错,就是显示不出来
Bob 2003-02-20
  • 打赏
  • 举报
回复
//记得using System.Drawing.Drawing2D;

private void button1_Click(object sender, EventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(new Pen(Color.AntiqueWhite,3),0,0,118,270);
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawString("sg.com.cn",
new Font("Arial", 10, FontStyle.Bold|FontStyle.Italic),
SystemBrushes.ActiveCaptionText,
new PointF(10,10));
}

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧