C# 防止打印空白页

C# 防止打印空白页,c#,winforms,printing,print-preview,C#,Winforms,Printing,Print Preview,我只需要在打印预览后重置“边框”。我预览了我想要正确打印的页面,但当我打印时,它会显示空白页面,因为“边框”没有重置。我应该把“border=0”放在哪里(“border”是datagridview中的no.行) 最初将其设置为0,并在ppd.ShowDialog()之后重置 更新 看起来,PrintPreviewDialog并不像您(和许多其他人期望的那样)那样支持很多,这取决于用户(而不是程序员)。你可以试试这个小玩意儿: //code in your button5_Click ToolS

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


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

更新 看起来,
PrintPreviewDialog
并不像您(和许多其他人期望的那样)那样支持很多,这取决于用户(而不是程序员)。你可以试试这个小玩意儿:

//code in your button5_Click
ToolStripButton onePageButton = ((ToolStrip)ppd.Controls[1]).Items[3] as ToolStripButton;
BeginInvoke((Action)(() => onePageButton.PerformClick()));
ppd.ShowDialog();
更新 要截取
点击
打印按钮上的
点击
,您需要添加更多的代码。您必须在项目(打印按钮)上触发
单击之前检测单击,显示请求确认的消息框,并
重新单击项目(如果用户同意)。以下是您的代码:

//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);

在我关闭预览窗口之前,它不会掉到那个里。当我们按下打印按钮时,是否可以关闭预览窗口?你能检查我问题的底部吗?@emmett不确定是否有更优雅的解决方案,但在找到之前,请在我的更新中尝试该解决方案。我不认为这里有任何直接的
,我明白了。我还有一个问题。如何控制打印专用视图上的打印按钮?我的意思是,当你按“打印”按钮时,我想显示一个“是/否”对话框。@emmett查看我的更新,如果你有更多问题,请先接受答案,然后再问另一个问题,我们不会回答这样的问题。这需要进入BeginPrint事件处理程序。
ppd.ShowDialog();
border = 0;
//code in your button5_Click
ToolStripButton onePageButton = ((ToolStrip)ppd.Controls[1]).Items[3] as ToolStripButton;
BeginInvoke((Action)(() => onePageButton.PerformClick()));
ppd.ShowDialog();
//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);