我在一个winfrom(c#)窗体中画了一个表,要将这个表直接打印出来怎么作呢?

解决方案 »

  1.   

    使用system.drawing中的打印图片功能,把图片打印出来就是了。
      

  2.   

    “使用system.drawing中的打印图片功能,把图片打印出来就是了”   详细一点好吗?最好有代码。
      

  3.   

    看看这个吧,很不错的实现:
    http://www.codeproject.com/dotnet/PrintingFormReport.asp
    你也可以在codeproject搜索一下其他的例子,还有很多类似的
      

  4.   

    在vb中有很简单的方法,from.print 就可以了,c#中没有简单的方法吗?
      

  5.   

    参考:[长江支流]
    到http://www.webmis.com.cn/里找相关的打印资料,里面有直接对listview等控件的打印处理。
    如果连接打不开,稍后尝试。
      

  6.   

    private void PreviewMenu_Click(object sender, System.EventArgs e)
    {
    printPreviewDialog1.ShowDialog();
    } void DrawForm(Graphics g, int resX, int resY)
    {
    g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, this.Width, this.Height);
    float scale = resX/ScreenResolution; foreach (Control c in Controls)
    {
    string strType = c.GetType().ToString().Substring(c.GetType().ToString().LastIndexOf(".") + 1);
    switch (strType)
    {
    case "Button":
    Button b = (Button)c;
    ControlPaint.DrawButton(g, ((Button)c).Left, ((Button)c).Top, ((Button)c).Width, ((Button)c).Height, ButtonState.Normal);
    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":
    // ControlPaint.FillReversibleRectangle( ((TextBox)c).Bounds, c.BackColor);
    TextBox t = (TextBox)c; ControlPaint.DrawButton(g, t.Left, t.Top, t.Width, t.Height, ButtonState.Pushed );
    g.FillRectangle(new SolidBrush(t.BackColor), t.Left+1, t.Top + 1, t.Width+2, t.Height -2);
    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());


    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale) - 5, Border3DStyle.SunkenInner  , Border3DSide.Bottom);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenInner  , Border3DSide.Left);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale) - 5, (int)(t.Height*scale), Border3DStyle.SunkenInner  , Border3DSide.Right);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), (int)(t.Width*scale), 5, Border3DStyle.SunkenInner  , Border3DSide.Top);
    //
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale)+1 - 5, Border3DStyle.SunkenOuter, Border3DSide.Bottom);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale)-1, (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter  , Border3DSide.Left);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale)+1 - 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter  , Border3DSide.Right);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale)-1, (int)(t.Width*scale), 5, Border3DStyle.SunkenOuter  , Border3DSide.Top);// g.FillRectangle(new SolidBrush(t.BackColor)  , t.Left+1, t.Top + 1, t.Width - 2, t.Height - 2);
    break;
    case "CheckBox":
    // ControlPaint.FillReversibleRectangle( ((TextBox)c).Bounds, c.BackColor);
    CheckBox cb = (CheckBox)c; 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); 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());


    // float scale = resX/ScreenResolution;
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale) - 5, Border3DStyle.SunkenInner  , Border3DSide.Bottom);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenInner  , Border3DSide.Left);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale) - 5, (int)(t.Height*scale), Border3DStyle.SunkenInner  , Border3DSide.Right);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale), (int)(t.Width*scale), 5, Border3DStyle.SunkenInner  , Border3DSide.Top);
    //
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale) + 5, (int)(t.Width*scale), (int)(t.Height*scale)+1 - 5, Border3DStyle.SunkenOuter, Border3DSide.Bottom);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale)-1, (int)(t.Top*scale), 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter  , Border3DSide.Left);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale) + 5, (int)(t.Top*scale), (int)(t.Width*scale)+1 - 5, (int)(t.Height*scale), Border3DStyle.SunkenOuter  , Border3DSide.Right);
    // ControlPaint.DrawBorder3D(g, (int)(t.Left*scale), (int)(t.Top*scale)-1, (int)(t.Width*scale), 5, Border3DStyle.SunkenOuter  , Border3DSide.Top); // g.FillRectangle(new SolidBrush(t.BackColor)  , t.Left+1, t.Top + 1, t.Width - 2, t.Height - 2);
    break;
    } } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    PrinterResolution pr = e.PageSettings.PrinterResolution;
     
    DrawForm(e.Graphics, pr.X, pr.Y);
    } float ScreenResolution = 96.0f;
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    ScreenResolution = e.Graphics.DpiX;
    } private void PrintMenu_Click(object sender, System.EventArgs e)
    {
    printDocument1.Print();
    }
      

  7.   

    感谢napsoft(乡下人)提供的代码,可是运行后只显示一个打印对话框,没有内容
      

  8.   

    private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {
    System.Windows.Forms.PrintPreviewDialog ppdialog=new PrintPreviewDialog();
    ((System.Windows.Forms.Form) ppdialog).WindowState=
    System.Windows.Forms.FormWindowState.Maximized;
    ppdialog.Document=this.printDocument1;
    ppdialog.ShowDialog(this);
    ppdialog.Close();
    }private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        PaintEventArgs myPaintArgs = 
        new PaintEventArgs(e.Graphics, new Rectangle(new Point(0, 0), this.Size));
        this.InvokePaint(this.dataGrid1, myPaintArgs);
    }
      

  9.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    CaptureScreen();
    printDocument1.Print();
    }
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;
    private void CaptureScreen()
    {
    Graphics mygraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
    Graphics memoryGraphics = Graphics.FromImage(memoryImage);
    IntPtr dc1 = mygraphics.GetHdc();
    IntPtr dc2 = memoryGraphics.GetHdc();
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
    mygraphics.ReleaseHdc(dc1);
    memoryGraphics.ReleaseHdc(dc2);
    }
    private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    e.Graphics.DrawImage(memoryImage, 0, 0);
    }拖到窗体里一个Button1,在拖出一个printDocument控件,运行