اذهب إلى المحتوى
  • 0

عرض محتويات الفورم فى تقرير #C

Code Zero

السؤال

Recommended Posts

  • 0

متاح ذلك من خلال  PrintDocument Class لإنشاء تقرير يعكس محتوى الفورم، أو استخدام أدوات مثل Crystal Reports أو ReportViewer Control في Visual Studio لإنشاء تقارير مخصصة، وتصميم التقرير بحيث يعرض جميع العناصر التي ترغب في طباعتها بما في ذلك التكس بوكس والجروب بوكس والبيانات.

أو بالإعتماد على Console.WriteLine() لطباعة البيانات على الشاشة أو في ملف نصي.

مع تثبيت مكتبة طباعة خارجية مثل iTextSharp أو DevExpress  لإنشاء مستند PDF أو XPS يحتوي على محتوى الفورم.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

وعليكم السلام!

نعم، يمكنك عمل طباعة للفورم في C# باستخدام مكتبة PrintDocument . فيما يلي مثال بسيط يوضح كيفية إعداد الطباعة لفورم:

 

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public class PrintExample : Form
{
    private PrintDocument printDocument1 = new PrintDocument();
    private Button printButton = new Button();
    private TextBox textBox = new TextBox();
    private GroupBox groupBox = new GroupBox();

    public PrintExample()
    {
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);

        printButton.Text = "Print Form";
        printButton.Click += new EventHandler(printButton_Click);

        textBox.Text = "Text to be printed.";
        textBox.Location = new Point(10, 10);

        groupBox.Text = "Group Box Content";
        groupBox.Location = new Point(10, 50);
        groupBox.Size = new Size(200, 100);

        Controls.Add(printButton);
        Controls.Add(textBox);
        Controls.Add(groupBox);
    }

    private void printButton_Click(object sender, EventArgs e)
    {
        PrintDialog printDialog = new PrintDialog();
        printDialog.Document = printDocument1;

        if (printDialog.ShowDialog() == DialogResult.OK)
        {
            printDocument1.Print();
        }
    }

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        // Draw the text from the TextBox
        e.Graphics.DrawString(textBox.Text, textBox.Font, Brushes.Black, new PointF(100, 100));

        // Draw the contents of the GroupBox
        DrawGroupBox(groupBox, e.Graphics, new PointF(100, 150));
    }

    private void DrawGroupBox(GroupBox groupBox, Graphics g, PointF point)
    {
        // Draw the GroupBox border
        g.DrawRectangle(Pens.Black, point.X, point.Y, groupBox.Width, groupBox.Height);
        
        // Draw the GroupBox text
        g.DrawString(groupBox.Text, groupBox.Font, Brushes.Black, point.X + 5, point.Y - groupBox.Font.Height);

        // Offset the point for controls within the GroupBox
        point.X += 10;
        point.Y += 20;

        // Draw the controls inside the GroupBox
        foreach (Control control in groupBox.Controls)
        {
            control.DrawToBitmap(new Bitmap(control.Width, control.Height), new Rectangle(0, 0, control.Width, control.Height));
            g.DrawImage(control.BackgroundImage, point);
            point.Y += control.Height + 5;
        }
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new PrintExample());
    }
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

نعم، يمكنك طباعة الفورم ككل بما في ذلك التكس بوكس والجروب بوكس وجميع المحتويات باستخدام الأدوات المتاحة في C#، فمثلا يمكنك استخدام PrintDocument Class أو أدوات التقارير المتاحة في Visual Studio أو حتى استخدام ()Console.WriteLine لطباعة البيانات على الشاشة أو في ملف نصي. كما يمكن استخدام مكتبات الطباعة الخارجية مثل iTextSharp أو DevExpress لإنشاء ملفات PDF أو XPS تحتوي على محتوى الفورم.

وكمثال فقط للشرح سأستخدم PrintDocument Class لشرح كيفية طباعة الفورم ككل. هذا المثال سيشمل إنشاء فورم ببعض العناصر مثل التكس بوكس والجروب بوكس، وسنقوم بطباعة محتويات الفورم على ورقة.

سيكون بهذا الشكل:

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public class PrintFormExample : Form
{
    private TextBox textBox;
    private GroupBox groupBox;
    private Button printButton;
    private PrintDocument printDocument;

    public PrintFormExample()
    {
        textBox = new TextBox();
        textBox.Text = "This is a TextBox";

        groupBox = new GroupBox();
        groupBox.Text = "This is a GroupBox";
        groupBox.Controls.Add(new Label { Text = "Label inside GroupBox", Location = new Point(10, 20) });

        printButton = new Button();
        printButton.Text = "Print Form";
        printButton.Click += new EventHandler(PrintButton_Click);

        printDocument = new PrintDocument();
        printDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);

        Controls.Add(textBox);
        Controls.Add(groupBox);
        Controls.Add(printButton);
    }

    private void PrintButton_Click(object sender, EventArgs e)
    {
        PrintDialog printDialog = new PrintDialog();
        printDialog.Document = printDocument;

        if (printDialog.ShowDialog() == DialogResult.OK)
        {
            printDocument.Print();
        }
    }

    private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        Bitmap bitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
        DrawToBitmap(bitmap, new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
        e.Graphics.DrawImage(bitmap, 0, 0);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new PrintFormExample());
    }
}

هنا قد قمت بإنشاء فورم بسيط يحتوي على TextBox و GroupBox وزر Print Button. عند النقر على زر "Print Form"، يتم فتح مربع حوار الطباعة لاختيار الطابعة، ثم يتم طباعة محتويات الفورم على الورقة.

داخل PrintDocument_PrintPage event handler، نقوم برسم محتويات الفورم على Graphics كما لو كانت على الشاشة ويتم هذا الأمر باستخدام DrawToBitmap لتحويل محتويات الفورم إلى Bitmap ثم رسمها على ورقة الطباعة المحددة.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...