关于c#打印一个容器内的所有控件的问题,高手的挑战(100分)

titanicliu 2005-01-17 04:08:27
请各位高手赐教:
我想打印一个容器内的所有控件,比如,打印一个panel中的所有控件(包括textbox,picturebox,panel等),不用下列两种方法:
1。抓图;
2。把每个控件都画到printdocument中(如textbox用drawstring(),picturebox用drawimage())

有没有另外的特别简单的方法
...全文
151 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
haoco 2005-03-21
  • 打赏
  • 举报
回复
up
flyfisher001 2005-03-21
  • 打赏
  • 举报
回复
Method in ControlPaint
Description

DrawButton Draws a Button in the Graphics area. Note this command will not draw any text or image on the button
DrawCheckBox Draws a Checkbox in the Graphics area
DrawBorder3D This will allow you to draw any 3D Border Side.

Table 1 - Methods of ControlPaint used in this article

Listing 1 - Draw the Form and controls to the printer Graphics object

void DrawForm(Graphics g, int resX, int resY)
{
g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
float scale = resX/ScreenResolution;
// Cycle through each control on the form and paint it to the printe
foreach (Control c in Controls)
{

// Get the time of the next control so we can unbox it
string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);
switch (strType)
{
case "Button":
Button b = (Button)c;

// Use the ControlPaint method DrawButton in order to draw the button of the form
ControlPaint.DrawButton(g, ((Button)c).Left, ((Button)c).Top, ((Button)c).Width, ((Button)c).Height, ButtonState.Normal);

// We also need to draw the text
g.DrawString(b.Text, b.Font, new SolidBrush(b.ForeColor),
b.Left + b.Width/2 - g.MeasureString(b.Text,
b.Font).Width/2, b.Top + b.Height/2 - g.MeasureString("a", b.Font).Height/2,
new StringFormat());

break;

case "TextBox":

TextBox t = (TextBox)c;

// Draw a text box by drawing a pushed in button and filling the rectangle with the background color and the text
// of the TextBox control

// First the sunken border
ControlPaint.DrawButton(g, t.Left, t.Top, t.Width, t.Height, ButtonState.Pushed );

// Then fill it with the background of the textbox
g.FillRectangle(new SolidBrush(t.BackColor), t.Left+1, t.Top + 1, t.Width+2, t.Height -2);

// Finally draw the string inside
g.DrawString(t.Text, t.Font, new SolidBrush(t.ForeColor), t.Left + 2,
t.Top + t.Height/2 - g.MeasureString("a", t.Font).Height/2, new StringFormat());

break;

case "CheckBox":

// We have a checkbox to paint, unbox it

CheckBox cb = (CheckBox)c;

// Use the DrawCheckBox command to draw a checkbox and pass the button state to paint it checked or unchecked
if (cb.Checked)
ControlPaint.DrawCheckBox(g, cb.Left, cb.Top, cb.Height/2, cb.Height/2, ButtonState.Checked);
else
ControlPaint.DrawCheckBox(g, cb.Left, cb.Top, cb.Height/2, cb.Height/2, ButtonState.Normal);

// Don't forget the checkbox text
g.DrawString(cb.Text, cb.Font, new SolidBrush(cb.ForeColor), cb.Right -cb.Height - g.MeasureString(cb.Text, cb.Font).Width ,
cb.Top, new StringFormat());

break;

}
}

}

liulxmooo 2005-01-18
  • 打赏
  • 举报
回复
up
titanicliu 2005-01-18
  • 打赏
  • 举报
回复
跟没说一样,我就是要做第三方控件,如果要导入到excel里面的话,我就不提这个问题了,就是不想用excel
nga96 2005-01-17
  • 打赏
  • 举报
回复
还有一种,使用第三方控件也行的
sean168 2005-01-17
  • 打赏
  • 举报
回复
导到Excel里面,然后打印

110,533

社区成员

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

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

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