如何在picturebox上写入文字

chenjiecao123 2014-06-20 03:29:52
大家好,以下是一段代码,可是没有实现,我的理解是,一开始运行程序后就会运行onpaint函数,我用messagebox测试是运行的,但是为什么并没有画出来,而我拖动窗口的时候却有出现文字,请大侠们帮忙解释下原理啊。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void Paintp();
public event Paintp p;
private void Form1_Load(object sender, EventArgs e)
{

}
protected override void OnPaint(PaintEventArgs e)//绿色
{

MessageBox.Show("OnPaint");
PointF pf = new PointF(200, 200);
using (Graphics g = pictureBox1.CreateGraphics())
{
Font f = new Font("Arial", 12);
g.DrawString("Hello!", f, Brushes.Green, pf);

}

MessageBox.Show("OnPaint2");
}
}
}
...全文
735 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
相思梦 2014-06-24
  • 打赏
  • 举报
回复
using (var _Graphics = this.pictureBox1.CreateGraphics()) { var _BSolid = new SolidBrush(Color.Black); _Graphics.DrawString("欲输出内容", this.Font, _BSolid ,ClientSize.Width - 60, 20); _BSolid.Dispose(); } // 做好释放优化工作,别想到现在电脑内存大了硬件配置高了 // 软件是要求越来越快,而不是搞得越来越慢,除非你愿意写垃圾代码
tanta 2014-06-23
  • 打赏
  • 举报
回复
仅供参考: onPaint的参数e中有graphic,e.Graphics 请用它来绘制。 不要忘记调用base.onPaint(e); 附:修改你的代码 protected override void OnPaint(PaintEventArgs e) { PointF pf = new PointF(200, 200); Font f = new Font("Arial", 12); e.Graphics.DrawString("Hello!", f, Brushes.Green, pf); base.OnPaint(e); }
Hassle 2014-06-21
  • 打赏
  • 举报
回复
protected override void OnPaint(PaintEventArgs e)//绿色 请注意override的是窗体的OnPaint!
kxyzjm62 2014-06-21
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) //用按钮在pictureBox1内写字 { Graphics g = pictureBox1.CreateGraphics(); Brush ft=new SolidBrush(Color.FromArgb(100, 200, 30)); Point p=new Point (0,100); g.DrawString("在这里写字!",new Font ("宋体",20),ft,p ); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Brush ft = new SolidBrush(Color.FromArgb(100, 200, 30)); Point p = new Point(45, 45); g.DrawString("这是pictureBox1_Paint写字!", new Font("宋体", 20), ft, p); } private void button2_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); Brush ft = new SolidBrush(Color.FromArgb(100, 200, 30)); Point p = new Point(0, 0); g.DrawString("这是在窗体上里写字!", new Font("宋体", 20), ft, p); } } }
sunny906 2014-06-20
  • 打赏
  • 举报
回复
引用 2 楼 chenjiecao123 的回复:
[quote=引用 1 楼 sunny906 的回复:] 代码没有问题,是坐标位置超出了pictureBox的范围

        public delegate void Paintp();
        public event Paintp p;
        protected override void OnPaint(PaintEventArgs e)//绿色
        {
            PointF pf = new PointF(0, 0);   //注意文字显示的坐标位置
            using (Graphics g = pictureBox1.CreateGraphics())
            {
                Font f = new Font("Arial", 12);
                g.DrawString("Hello!", f, Brushes.Green, pf);
            }
        }

我试了下,还是不行呢[/quote] 我这里测试是可以的,你调整一下显示的坐标,应该是坐标问题
by_封爱 版主 2014-06-20
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/390345508 看下这个 就是在 pic写字的..
chenjiecao123 2014-06-20
  • 打赏
  • 举报
回复
引用 1 楼 sunny906 的回复:
代码没有问题,是坐标位置超出了pictureBox的范围

        public delegate void Paintp();
        public event Paintp p;
        protected override void OnPaint(PaintEventArgs e)//绿色
        {
            PointF pf = new PointF(0, 0);   //注意文字显示的坐标位置
            using (Graphics g = pictureBox1.CreateGraphics())
            {
                Font f = new Font("Arial", 12);
                g.DrawString("Hello!", f, Brushes.Green, pf);
            }
        }

我试了下,还是不行呢
sunny906 2014-06-20
  • 打赏
  • 举报
回复
代码没有问题,是坐标位置超出了pictureBox的范围

        public delegate void Paintp();
        public event Paintp p;
        protected override void OnPaint(PaintEventArgs e)//绿色
        {
            PointF pf = new PointF(0, 0);   //注意文字显示的坐标位置
            using (Graphics g = pictureBox1.CreateGraphics())
            {
                Font f = new Font("Arial", 12);
                g.DrawString("Hello!", f, Brushes.Green, pf);
            }
        }

111,094

社区成员

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

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

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