簡體   English   中英

C#如何打印沒有窗體邊界的窗體

[英]c# how to print window form without window form borders

我有一個窗口表格。 我想在沒有窗口外觀的情況下打印表單的內容。 我的意思是我想像收據一樣打印,沒有窗口邊框。 我該怎么做呢?

您可以使用有關如何打印到Windows窗體的MSDN示例,將要打印的表面從窗體更改為面板控件,這將使您可以無邊界打印。 您的內容將必須添加到面板中而不是表單中,但是它將起作用。 這是MSDN示例的修改后的示例。

public class Form1 : Form
{
    private Panel printPanel = new Panel();
    private Button printButton = new Button();
    private PrintDocument printDocument1 = new PrintDocument();

    public Form1()
    {
        printPanel.Size = this.ClientSize;
        this.Controls.Add(printPanel);
        printButton.Text = "Print Form";
        printButton.Click += new EventHandler(printButton_Click);
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        printPanel.Controls.Add(printButton);

    }

    void printButton_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
    }


    Bitmap memoryImage;

    private void CaptureScreen()
    {
        Graphics myGraphics = printPanel.CreateGraphics();
        Size s = printPanel.Size; 
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        Point screenLoc = PointToScreen(printPanel.Location); // Get the location of the Panel in Screen Coordinates
        memoryGraphics.CopyFromScreen(screenLoc.X, screenLoc.Y, 0, 0, s);
    }

    private void printDocument1_PrintPage(System.Object sender,
           System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }



    public static void Main()
    {
        Application.Run(new Form1());
    }
}

您可以通過執行以下操作來獲得空白屏幕

this.FormBorderStyle = System.Windows.Forms.FormsBorderStyle.None;
this.ControlBox = false;
this.Text = String.Empty;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM