C# 每当按下“打印”按钮时,尝试显示此对话框

C# 每当按下“打印”按钮时,尝试显示此对话框,c#,.net,C#,.net,因此,基本上我试图显示这个对话框,让用户能够在任何应用程序中按下打印按钮时更改打印颜色,因此他们必须首先指定设置,然后文件才会打印 我已经创建了对话框,它正在工作,但在打印步骤时仍然无法进入显示 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.D

因此,基本上我试图显示这个对话框,让用户能够在任何应用程序中按下打印按钮时更改打印颜色,因此他们必须首先指定设置,然后文件才会打印

我已经创建了对话框,它正在工作,但在打印步骤时仍然无法进入显示

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
using System.Runtime.InteropServices;


namespace PrinterSettingsSample
{

public class Form1 : System.Windows.Forms.Form
{
    [DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true,
         ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,
        [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName,
        IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode);
    [DllImport("kernel32.dll")]
    static extern IntPtr GlobalLock(IntPtr hMem);
    [DllImport("kernel32.dll")]
    static extern bool GlobalUnlock(IntPtr hMem);
    [DllImport("kernel32.dll")]
    static extern bool GlobalFree(IntPtr hMem);

    private void OpenPrinterPropertiesDialog(PrinterSettings printerSettings)
    {
        IntPtr hDevMode = printerSettings.GetHdevmode(printerSettings.DefaultPageSettings);
        IntPtr pDevMode = GlobalLock(hDevMode);
        int sizeNeeded = DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, ref pDevMode, 0);
        IntPtr devModeData = Marshal.AllocHGlobal(sizeNeeded);
        DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, devModeData, ref pDevMode, 14);
        GlobalUnlock(hDevMode);
        printerSettings.SetHdevmode(devModeData);
        printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
        GlobalFree(hDevMode);
        Marshal.FreeHGlobal(devModeData);
    }

    private System.Windows.Forms.Button printSetting;
    private System.ComponentModel.Container components = null;

    public Form1()
    {

        InitializeComponent();


    }

    protected override void Dispose( bool disposing )
    {
        if( disposing )
        {
            if (components != null) 
            {
                components.Dispose();
            }
        }
        base.Dispose( disposing );
    }

    #region Windows Form 

    private void InitializeComponent()
    {
        this.printSetting = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // printSetting
        // 
        this.printSetting.Location = new System.Drawing.Point(24, 32);
        this.printSetting.Name = "printSetting";
        this.printSetting.Size = new System.Drawing.Size(328, 23);
        this.printSetting.TabIndex = 0;
        this.printSetting.Text = "Change Options";
        this.printSetting.Click += new System.EventHandler(this.button1_Click);
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
        this.ClientSize = new System.Drawing.Size(400, 200);
        this.Controls.Add(this.printSetting);
        this.Name = "Form1";
        this.Text = "Printing Options";
        this.ResumeLayout(false);

    }
    #endregion

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

    private void button1_Click(object sender, System.EventArgs e)
    {
        try
        {
            PrinterSettings ps = new PrinterSettings();

            OpenPrinterPropertiesDialog(ps);
        }
        catch(Exception ex)
        {
            MessageBox.Show("Printer settings are incorrect.","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
        
        }       
    }
}
}