繁体   English   中英

防止打印空白页

[英]Prevent printing blank pages

我只需要在打印预览后重设“边框”即可。 我预览了我想正确打印的页面,但是当我进行打印时,它会显示空白页面,因为“边框”未重置。 我应该在哪里放置“ border = 0”?(“ border”是datagridview中的行号)

  private void button5_Click(object sender, EventArgs e)
    {
       PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);           
        PrintPreviewDialog ppd = new PrintPreviewDialog();
        ppd.Document = pd;
        ppd.ShowDialog();

    }
  private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
  prntt(sender, e);
     }
   public void prntt(object sender, PrintPageEventArgs e)
    {
           for (; border < ViewA.RowCount; border++)
        {
            if (ustsin + yuk > e.MarginBounds.Bottom - 400f)
            {

                e.HasMorePages = true;                  

                return;
            }



            texts = ViewA.Rows[border].Cells["Persons"].Value.ToString();
            ...
            graphics.DrawString(texts, font, Brushes.Black, new RectangleF(e.MarginBounds.Left, ustsin, 115f, 90f));               
            ...

            float hoho = (float)e.Graphics.MeasureString(texts, font, 115, StringFormat.GenericTypographic).Height;
            ...
            var mesele = new float[] { hoho, koko, moko };
            float kapa = mesele.OrderByDescending(s => s).First();

            ustsin += kapa + yuk;             

        }            

        e.HasMorePages = false;
     }

如果在打印预览中按“打印”按钮时可以关闭,是否可以在关闭事件中重置?

编辑:我这样做,它似乎可以工作,但当我将其发送到xps时,它在屏幕上显示2页。 像这样http://i.imgur.com/a9KnkA0.png 如何显示此1页?

    private void printDocument1_EndPrint(object sender, PrintEventArgs e)
    {


        border = 0;
    }

最初将其设置为0 ,然后在ppd.ShowDialog();之后将其重置ppd.ShowDialog();

ppd.ShowDialog();
border = 0;

更新

看起来PrintPreviewDialog对您的支持不如您(和许多其他人所期望的那样),取决于用户(而不是程序员)。 您可以尝试以下技巧:

//code in your button5_Click
ToolStripButton onePageButton = ((ToolStrip)ppd.Controls[1]).Items[3] as ToolStripButton;
BeginInvoke((Action)(() => onePageButton.PerformClick()));
ppd.ShowDialog();

更新

要拦截ClickingPrint button ,您必须添加更多代码。 你必须检测点击之前Click上的项目(打印按钮)发射,显示消息框,询问确认,并re-click ,如果用户同意的项目。 这是给您的代码:

//Use this class to add message interceptor into your ToolStrip message loop
public class NativeToolStrip : NativeWindow {
    ToolStrip ts;
    bool letClicked;
    protected override void OnHandleChange() {
        base.OnHandleChange();
        Control c = Control.FromHandle(Handle);
        ts = c as ToolStrip;
    }
    protected override void WndProc(ref Message m) {                
      if (m.Msg == 0x202&&!letClicked) {//WM_LBUTTONUP = 0x202
           int x = m.LParam.ToInt32() & 0x00ff;
           int y = m.LParam.ToInt32() >> 16;
           ToolStripItem item = ts.GetItemAt(new Point(x, y));                    
           //check if the first item (the Print Button) is clicked
           if (item != null && ts.Items.IndexOf(item) == 0) {
             if (MessageBox.Show("Do you want to print?", "Print confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                 return;//discard message
             else {
                    letClicked = true;
                    item.PerformClick();
              }
            }
       }
       base.WndProc(ref m);
       if (letClicked) letClicked = false;
    }
}
//This code should be done somewhere like in your form constructor
//BUT your PrintPreviewDialog should also be declared once in the form scope
//You can also place this in your button5_Click BUT it's not recommended
ToolStrip ts = (ToolStrip)ppd.Controls[1];
new NativeToolStrip().AssignHandle(ts.Handle);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM