我也顶,我现在是预览,,我也像不要预览直接打。我知道有个调用api 函数直接往并口写数据打印矩形的。。功能太有限没有用它

解决方案 »

  1.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=4CA8773F-50D9-4CE1-B962-3C851A94B781
      

  2.   

    oh my god
    给我发个邮件,我把例子发给你
    [email protected]
      

  3.   

    可以使用抓取图片的方法,使用API BITBLT ,把GRID 的图形变成图形文件,
    然后利用PrintDocument
    控件的ON_Paint()事件里面把图片打印出来!。
    或者直接在ON_Paint事件里用绘制网格和文字这样做比较麻烦!
    附代码:
    图形抓取类:
    /// <summary>
    /// Summary description for CCaptueImage.
    /// </summary>
    public class CCaptureImage
    {   #region API DECLARE
       [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(
    IntPtr hdcDest, // handle to destination DC
    int nXDest,  // x-coord of destination upper-left corner
    int nYDest,  // y-coord of destination upper-left corner
    int nWidth,  // width of destination rectangle
    int nHeight, // height of destination rectangle
    IntPtr hdcSrc,  // handle to source DC
    int nXSrc,   // x-coordinate of source upper-left corner
    int nYSrc,   // y-coordinate of source upper-left corner
    System.Int32 dwRop  // raster operation code
    );
    #endregion
            
    public CCaptureImage()
    {
    //
    // TODO: Add constructor logic here
    //
    }
            
    #region My Property ,Field        private int mImgWidth=400;
    /// <summary>
    /// Capture image's width
    /// </summary>
    public int ImgWidth
    {
    set{mImgWidth=value;}
    get{return mImgWidth;}
    }
    private int mImgHeight=300;
    /// <summary>
    /// Capture image's height
    /// </summary>
    public int ImgHeight
    {
    set{mImgHeight=value;}
    get{return mImgHeight;}
    }
    public int mCapLeft=0;
    /// <summary>
    /// Capture image left position of screen coordinate
    /// </summary>
    public int CapLeft
    {
    set{mCapLeft=value;}
    get{return mCapLeft;}
    }
    public int mCapTop=0;
    /// <summary>
    /// Capture image top position of screen coordinate 
    /// </summary>
    public int CapTop
    {
    set{mCapTop=value;}
    get{return mCapTop;}
    }
            private Form mSelectForm;
    /// <summary>
    /// Capture image's form
    /// </summary>
    public Form SelectForm
    {
    set{mSelectForm=value;}
    get{return mSelectForm;}
    }

    #endregion #region Functions
            /// <summary>
            /// Capture image function
            /// </summary>
            /// <param name="filename">the path of the file</param>
            /// <returns>true is scuessfully,false is failure</returns>
    public bool SaveImage(string filename)
    {

    try
    {

    if (SelectForm==null) 
    {
    MessageBox.Show ("Not capture image form","No form",
                  MessageBoxButtons.OK ,
                  MessageBoxIcon.Information );
    return false;
    }
    Graphics g1 =SelectForm.CreateGraphics ();
    Image CapImg=new Bitmap (ImgWidth,ImgHeight,g1);
    Graphics g2=Graphics.FromImage (CapImg);
    IntPtr dc1=g1.GetHdc ();
    IntPtr dc2=g2.GetHdc ();
    BitBlt(dc2,0,0,ImgWidth,ImgHeight,dc1,CapLeft,CapTop,13369376);
    g1.ReleaseHdc(dc1);
    g2.ReleaseHdc (dc2);
    CapImg.Save (filename);

    return true;
    }
    catch
    {
    MessageBox.Show ("Capture image failure!","failure",
                  MessageBoxButtons.OK ,
                  MessageBoxIcon.Information);
    return false;
    }
                
    }
    #endregion }打印代码
    private void btnGraphPrint_Click(object sender, System.EventArgs e)
    {
    try
    {
    string imgfilename=Directory.GetCurrentDirectory ()+"\\temp.bmp";
    CCaptureImage ci=new CCaptureImage ();
    ci.SelectForm =this;
    ci.CapLeft =pnlGraphView.Left+ttChart2.Left;
    ci.CapTop =pnlGraphView.Top+ttChart2.Top;
    ci.ImgHeight =ttChart2.Height;
    ci.ImgWidth =ttChart2.Width;
    this.temI550N1.TimeDelayFunc(500); 
    ci.SaveImage (imgfilename);
                    
    if(MessageBox.Show("&Egrave;·&para;¨&Ograve;&ordf;&acute;ò&Oacute;&iexcl;&pound;&iquest;",
    "&acute;ò&Oacute;&iexcl;",
    MessageBoxButtons.YesNo  ,
    MessageBoxIcon.Information )
    ==DialogResult.Yes )
    {
    pntPreview.ShowDialog ();
    }
    }
    catch(Exception err)
    {
    MessageBox.Show (err.Message ,"&acute;í&Icirc;ó",
    MessageBoxButtons.OK ,
    MessageBoxIcon.Information );
    }
    }
    private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    try
    {
    //initialize print setting;
    int pntW=pntDoc.DefaultPageSettings.Bounds.Width ;
    int pntH=pntDoc.DefaultPageSettings.Bounds.Height;
    //draw Title
    int X=0;
    int Y=5;
                    
    Graphics g=e.Graphics ;
                
    // Image ImgTitle=Image.FromFile (Directory.GetCurrentDirectory() +
    // "//Parm//Bmp//Title.bmp");
    Rectangle DRect=new Rectangle (new Point (0,0),
    new Size (pntW,pntH));
    Rectangle SRect=new Rectangle (new Point (0,0),
    new Size (pntW,pntH));
                
    //g.DrawImage (ImgTitle,DRect,SRect,GraphicsUnit.Pixel);
    Font TitleFt=new Font ("Arial",16);
    //draw title
    g.DrawString ("Title- T6800 View",TitleFt,new SolidBrush (Color.Black ),new PointF (X,Y+90));
    //draw date
    g.DrawString ("Date -"+DateTime.Now.Date.ToShortDateString() ,TitleFt,
    new SolidBrush (Color.Black ),
    new PointF (X,Y+110));
    //draw copys
    g.DrawString ("Copys-"+pntDoc.PrinterSettings.Copies.ToString ("00"),TitleFt,
    new SolidBrush (Color.Black ),
    new PointF (X,Y+130));
    //draw a line
    g.DrawLine (new Pen (new SolidBrush (Color.Black ),1.0f),
    new Point (X,Y+160),
    new Point (pntW,Y+160));
    Image Img=Image.FromFile(Directory.GetCurrentDirectory () + 
    "\\temp.bmp");
    //draw image
    Point spt=new Point (0,0);
    Point dpt=new Point (pntW/2-Img.Width /2,200);
    Rectangle SrcRect=new Rectangle (spt,new Size (pntW,pntH));
    Rectangle DesRect=new Rectangle (dpt,new Size (pntW,pntH));
    g.DrawImage(Img,DesRect,SrcRect,GraphicsUnit.Pixel);
    }
    catch(Exception err)
    {
    MessageBox.Show (err.Message ,"&acute;í&Icirc;ó",
    MessageBoxButtons.OK ,
    MessageBoxIcon.Information );
    }
    }
      

  4.   

    前面打错了,不是ON_PAINT 事件 应该是printDoc_PrintPage()事件
      

  5.   

    不是在声明graphics 实例时,将设备打印机就可以了吗?
      

  6.   

    回复人: liduke(天下有雪) ( ) 信誉:100  2004-9-24 14:40:09  得分: 0  
     
     
       
    不是在声明graphics 实例时,将设备打印机就可以了吗?  
     
    兄弟有提供一下例子吗?
      

  7.   

    今天由于公司网络有问题,所以没能及时上来!wangjingjing390(晶晶),我已经给你发邮件了,在此先谢谢你!
      

  8.   

    不是有个打印控件吗?   直接  PIRNGcontrol.PRINT()哪里会有预览?printDoc_PrintPage()事件//所有的画图都画在里面,当然也可以先画在图上,然后,在此DRAWIMAGE
    {drawline...
    draw.....
    draw....
    }