private void pbSecurityCode_Paint(object sender, PaintEventArgs e)
{
//scd = new SecurityCodeDrawer(
// securityCode, pbSecurityCode.Width, pbSecurityCode.Height, securityCode);
//scd.drawSecurityCode(e.Graphics);
//Graphics.FromHwnd(pbSecurityCode.Handle)
Graphics gs;
gs = pbSecurityCode.CreateGraphics();
gs.DrawLine(
new Pen(Color.BlueViolet, 4.0f), 0, 0, pbSecurityCode.Width, pbSecurityCode.Height);
}
我这样画,完全没有效果,只有用e.graphics才行,这是为什么?
把那句改为gs = e.Graphics
就立刻管用了
这是为什么
但为什么我在另一个button的click事件中写这段代码,就生效了呢?
private void btnlog_Click(object sender, EventArgs e)
{
Graphics gs;
gs = pbSecurityCode.CreateGraphics();
gs.DrawLine(
new Pen(Color.BlueViolet, 4.0f), 0, 0, pbSecurityCode.Width, pbSecurityCode.Height);
}
因为你并没有重写button的click事件,只是button的click事件触发了其中的代码运行。并且button的click事件并不操作pictureBox对象。
你可以单独写一个事件,然后赋给pictureBox的 paint事件中。这样就可以pictureBox的 paint事件中使用createGraphics了
还是不太明白啊~,学长,能+我Q么?690038817
在button事件里,gs = pbSecurityCode.CreateGraphics();
无疑是指向pbSecurityCode(pictureBox)的graphics对象,
在paint事件里,gs = pbSecurityCode.CreateGraphics();
所指向的到底向的是谁呢?为什么就不指向他了呢?
各种晕啊~~~!
其实,我是想在load里画,但不知道在load里该如何获得graphics
我想在load里先画一下,在paint里直接用bitmap盖上去
如果想在load里画,那该怎么办呢?是不是非得在个birmap里画,然后再paint事件里,用e.graphics.drawImage()覆盖上去?
能直接在load里画么?
你要去了解一下win32api里面windows消息的传递和窗体的生命周期,你在load里面画画是很不合适的行为,因为load不是用来画画的,而是初始化窗体的。给你一个比喻,load事件是娃在妈肚子里的时候,这时候你却想要他找个姑娘做点欢乐的事情,你觉得合适么?
本回答被提问者采纳