IT_Programming/WinForm (C#.NET)

다이얼로그 종류별 예제

JJun ™ 2008. 12. 21. 10:22

출처: Beginning C# & MSDN Library

 

[ 파일 열기 다이얼로그 - OpenFileDialog ]

 

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
     foreach(string s in openFileDialog1.FileName)  

    {
        // Insert code to read the stream here.


    }
}


 

[ 파일 저장 다이얼로그 - SaveFileDialog ]

Stream myStream;
SaveFileDialog saveFileDialog = new SaveFileDialog();

saveFileDialog.InitialDirectory = "c:\\";
saveFileDialog.Filter = "txt files (*.txt)|*.txt"; // 쓸 수 들일 수 있는 파일 제한
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;

 

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
      if ((myStream = saveFileDialog.OpenFile()) != null)

      {

            // Insert code to read the stream here.
            myStream.Close();

      }

}

 

 

[ 폰트 다이얼로그 - FontDialog ]

private void button1_Click(object sender, System.EventArgs e)
{
    fontDialog1.ShowColor = true;

    fontDialog1.Font = textBox1.Font;
    fontDialog1.Color = textBox1.ForeColor;

    if(fontDialog1.ShowDialog() != DialogResult.Cancel )
    {
       textBox1.Font = fontDialog1.Font ;
       textBox1.ForeColor = fontDialog1.Color;
    }
}

 

 

[ 색상 다이얼로그 - ColorDialog ]

private void button1_Click(object sender, System.EventArgs e)
{
    ColorDialog MyDialog = new ColorDialog();
    // Keeps the user from selecting a custom color.
    MyDialog.AllowFullOpen = false ;
    // Allows the user to get help. (The default is false.)
    MyDialog.ShowHelp = true ;
    // Sets the initial color select to the current text color.
    MyDialog.Color = textBox1.ForeColor ;
   
    // Update the text box color if the user clicks OK
    if (MyDialog.ShowDialog() == DialogResult.OK)
        textBox1.ForeColor =  MyDialog.Color;
}

 

 

[ 프린트 다이얼로그 - PrintDialog ]

// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint =
    new System.Drawing.Printing.PrintDocument();

// This method will set properties on the PrintDialog object and then display the dialog.
private void Button1_Click(System.Object sender, System.EventArgs e)
{

    // Allow the user to choose the page range he or she would like to print.
    PrintDialog1.AllowSomePages = true;

    // Show the help button.
    PrintDialog1.ShowHelp = true;

    // Set the Document property to the PrintDocument for which the PrintPage Event has been handled.

    // To display the dialog, either this property or the PrinterSettings property must be set
    PrintDialog1.Document = docToPrint;

    DialogResult result = PrintDialog1.ShowDialog();

    // If the result is OK then print the document.
    if (result==DialogResult.OK)
    {
        docToPrint.Print();
    }

}

// The PrintDialog will print the document by handling the document's PrintPage event.
private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

    // Insert code to render the page here.
    // This code will be called when the control is drawn.

    // The following code will render a simple
    // message on the printed document.
    string text = "In document_PrintPage method.";
    System.Drawing.Font printFont = new System.Drawing.Font ("Arial", 35,

                                                                 System.Drawing.FontStyle.Regular);

    // Draw the content.
    e.Graphics.DrawString(text, printFont,
        System.Drawing.Brushes.Black, 10, 10);
}

[ 페이지 설정 다이얼로그 - PageSetupDialog ]
// This method displays a PageSetupDialog object. If the user clicks OK in the dialog, selected results

// of the dialog are displayed in ListBox1.
private void Button1_Click(System.Object sender, System.EventArgs e)
{

    // Initialize the dialog's PrinterSettings property to hold user defined printer settings.
    PageSetupDialog1.PageSettings =
        new System.Drawing.Printing.PageSettings();

    // Initialize dialog's PrinterSettings property to hold user set printer settings.
    PageSetupDialog1.PrinterSettings =
        new System.Drawing.Printing.PrinterSettings();

    // Do not show the network in the printer dialog.
    PageSetupDialog1.ShowNetwork = false;

    // Show the dialog storing the result.
    DialogResult result = PageSetupDialog1.ShowDialog();

    // If the result is OK, display selected settings in ListBox1.

    // These values can be used when printing the document.
    if ( result == DialogResult.OK)
    {
        object[] results = new object[]

        {
            PageSetupDialog1.PageSettings.Margins,
            PageSetupDialog1.PageSettings.PaperSize,
            PageSetupDialog1.PageSettings.Landscape,
            PageSetupDialog1.PrinterSettings.PrinterName,
            PageSetupDialog1.PrinterSettings.PrintRange

        };
        ListBox1.Items.AddRange(results);
    }
}

 

 

[ 인쇄 미리 보기 다이얼로그 - PrintPreviewDialog ]

// Declare the dialog.
internal PrintPreviewDialog PrintPreviewDialog1;

// Declare a PrintDocument object named document.
private System.Drawing.Printing.PrintDocument document =
                                                                       new System.Drawing.Printing.PrintDocument();

// Initalize the dialog.
private void InitializePrintPreviewDialog()
{

    // Create a new PrintPreviewDialog using constructor.
    this.PrintPreviewDialog1 = new PrintPreviewDialog();

    // Set the size, location, and name.
    this.PrintPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
    this.PrintPreviewDialog1.Location =  new System.Drawing.Point(29, 29);
    this.PrintPreviewDialog1.Name = "PrintPreviewDialog1";
   
    // Associate the event-handling method with the document's PrintPage event.
    this.document.PrintPage += new System.Drawing.Printing.PrintPageEventHandler
                                                                                               (document_PrintPage);

    // Set the minimum size the dialog can be resized to.
    this.PrintPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);

    // Set the UseAntiAlias property to true, which will allow the
    // operating system to smooth fonts.
    this.PrintPreviewDialog1.UseAntiAlias = true;
}

private void Button1_Click(object sender, System.EventArgs e)
{

    if (TreeView1.SelectedNode != null)

        // Set the PrintDocument object's name to the selectedNode object's  tag,

        // which in this case contains the fully-qualified name of the document.

        // This value will show when the dialog reports progress.
    {
        document.DocumentName = TreeView1.SelectedNode.Tag.ToString();
    }

    // Set the PrintPreviewDialog.Document property to the PrintDocument object selected by the user.
    PrintPreviewDialog1.Document = document;

    // Call the ShowDialog method. This will trigger the document's PrintPage event.
    PrintPreviewDialog1.ShowDialog();
}

private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

    // Insert code to render the page here.
    // This code will be called when the PrintPreviewDialog.

    // Show method is called.

    // The following code will render a simple message on the document in the dialog.
    string text = "In document_PrintPage method.";
    System.Drawing.Font printFont =  new System.Drawing.Font("Arial", 35,        

                                                                                        System.Drawing.FontStyle.Regular);

    e.Graphics.DrawString(text, printFont,
        System.Drawing.Brushes.Black, 0, 0);

}