关于Paint方法的问题

zhaomuzhi 2007-08-30 11:10:59
我在绘制TabPage时用了Paint方法如下:
void page1_Paint(object sender, PaintEventArgs e)

但我发现((TabPage)sender).CreateGraphics()并不等于e.Graphics,
而且e.Graphics比((TabPage)sender).CreateGraphics()更底层。
因为e.Graphics上的图象可以透过透明控件显示出来((TabPage)sender).CreateGraphics()却不行,所以我想在方法外也能得到e.Graphics,但不知应该怎样做,希望有能人来解答。
...全文
136 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaomuzhi 2007-09-04
  • 打赏
  • 举报
回复
这个方法不行,不信你可以试试
BearRui 2007-09-04
  • 打赏
  • 举报
回复
定个全局变量,把e.Graphics保存到这个变量,其他方法中不就可以使用了?
zhaomuzhi 2007-09-04
  • 打赏
  • 举报
回复
因为我想在其他的方法中也能使用e.Graphics
BearRui 2007-09-04
  • 打赏
  • 举报
回复
那为什么就不直接使用e.Graphics了
zhaomuzhi 2007-09-04
  • 打赏
  • 举报
回复
我用如下的方法试了一下:
void page1_Paint(object sender, PaintEventArgs e)
{
PAINTSTRUCT a = new PAINTSTRUCT();
Graphics g = Graphics.FromHdc(BeginPaint(((TabPage)sender).Handle, ref a));
g.DrawLine(new Pen(Color.Black), new Point(50, 50), new Point(100, 100));
}
但什么都显示不出来,不知道是不是我的方法不对,还希望纠正。
北京的雾霾天 2007-08-31
  • 打赏
  • 举报
回复
使用如下的API可以得到这个Graphics的Handle:
[DllImport("user32.dll", EntryPoint="BeginPaint", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern IntPtr BeginPaint(IntPtr hWnd, [In, Out] ref PAINTSTRUCT lpPaint);

[StructLayout(LayoutKind.Sequential)]
public struct PAINTSTRUCT
{
public IntPtr hdc;
public bool fErase;
public int rcPaint_left;
public int rcPaint_top;
public int rcPaint_right;
public int rcPaint_bottom;
public bool fRestore;
public bool fIncUpdate;
public int reserved1;
public int reserved2;
public int reserved3;
public int reserved4;
public int reserved5;
public int reserved6;
public int reserved7;
public int reserved8;
}
得到后能过Graphics.FromHdc得到Graphics
csharp_start 2007-08-30
  • 打赏
  • 举报
回复
mark

110,499

社区成员

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

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

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