Windows窗体不显示 [英] Windows Form Not Showing

查看:82
本文介绍了Windows窗体不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口表单,在某一点需要运行但从不显示表单但现在我需要显示表单,我不知道如何再次显示表单。我认为它与以下代码有关。这是我的program.cs代码



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Windows.Forms;


命名空间 test_app
{
静态 class 程序
{
/// < 摘要 >
/// 应用程序的主要入口点。
/// < / summary >
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form1());
}
}
}





这是我的代码来自form1.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Linq;
使用 System.Text;
使用 System.Drawing;
使用 System.Drawing.Printing;
使用 System.Windows.Forms;
使用 System.Runtime.InteropServices;
使用 System.IO;
使用 System.Windows;
使用 System.Security.Permissions;
使用 System.Threading;
使用 System.ServiceProcess;

// 工作但不显示表格
命名空间 test_app
{
public partial class Form1:表格
{
public Form1()
{
InitializeComponent();
// this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this .ShowInTaskbar = false ;
this .Load + = new EventHandler(Form1_Load);

}

public static bool SendStringToPrinter( string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// 字符串中有多少个字符?
dwCount = szString。长度;
// 假设打印机需要ANSI文本,然后将字符串转换为ANSI文本。
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// 将转换后的ANSI字符串发送到打印机。
SendBytesToPrinter(szPrinterName ,pBytes,dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true ;
}

}

private void Form1_Load( object sender,System.EventArgs e) // 这部分工作!!
{
ServiceController serviceController = new ServiceController( 后台处理程序);
serviceController.Stop();
Thread.Sleep( 7000 );
serviceController.Start();
Thread.Sleep( 3000 );
Thread.Sleep( 3000 );
foreach 字符串路径 in Directory.GetFiles( C:\\Windows\\System32 \\\\\\\\ \\ PRINTERS))
File.Delete(path);
string szString = ~JR< /跨度>;
PrintDialog printDialog = new PrintDialog();
printDialog.PrinterSettings = new PrinterSettings();
printDialog.PrinterSettings.PrinterName = Label Printer;
Form1.RawPrinterHelper.SendStringToPrinter(printDialog.PrinterSettings.PrinterName,szString);
Environment.Exit( 1 );

}

private void button1_Click(< span class =code-keyword> object
sender,EventArgs e) // 与form_load相同
{

// 停止打印后台处理程序服务然后等待3秒
ServiceController controller = new ServiceController( 假脱机程序);
controller.Stop();



// 启动Print Spooler Service然后等待3秒
controller.Start();
// Thread.Sleep(3000);


// 删除打印作业然后等待3秒
// Thread.Sleep(3000);
string [] filePaths = Directory.GetFiles( @ C:\ Windows \ System32 \\\\\\\\\\\\\ /跨度>);
foreach string filePath in filePaths)
File.Delete(filePath);


string s = 〜JR; // 设备相关的字符串,需要FormFeed?

// 允许用户选择打印机。
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
pd.PrinterSettings.PrinterName = Label Printer;
// if(DialogResult.OK == pd.ShowDialog(this))
// {
// 将特定于打印机的信息发送给打印机。
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName,s);



// Environment.Exit(1);
}
}
}





我确定这很简单,我只是想不出来!有人可以帮忙吗?

解决方案

我建​​议不要在Load处理程序中调用Environment.Exit!



艾伦。

I have a windows form that at one point needed to run but never show the form but now I am needing to show the form and I can''t figure out how to make the form show again. I think it has something to do with the following code. Here is my code for program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace test_app
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}



And here is my code from form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows;
using System.Security.Permissions;
using System.Threading;
using System.ServiceProcess;

//working but not showing form
namespace test_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.ShowInTaskbar = false;
            this.Load += new EventHandler(Form1_Load);
            
        }

                   public static bool SendStringToPrinter(string szPrinterName, string szString)
            {
                IntPtr pBytes;
                Int32 dwCount;
                //How many characters are in the string?
                dwCount = szString.Length;
                //Assume that the printer is expecting ANSI text, and then convert the string to ANSI text.
                pBytes = Marshal.StringToCoTaskMemAnsi(szString);
                //Send the converted ANSI string to the printer.
                SendBytesToPrinter(szPrinterName, pBytes, dwCount);
                Marshal.FreeCoTaskMem(pBytes);
                return true;
            }

        }

        private void Form1_Load(object sender, System.EventArgs e)//This part works!!
        {
            ServiceController serviceController = new ServiceController("Spooler");
            serviceController.Stop();
            Thread.Sleep(7000);
            serviceController.Start();
            Thread.Sleep(3000);
            Thread.Sleep(3000);
            foreach (string path in Directory.GetFiles("C:\\Windows\\System32\\spool\\PRINTERS"))
                File.Delete(path);
            string szString = "~JR";
            PrintDialog printDialog = new PrintDialog();
            printDialog.PrinterSettings = new PrinterSettings();
            printDialog.PrinterSettings.PrinterName = "Label Printer";
            Form1.RawPrinterHelper.SendStringToPrinter(printDialog.PrinterSettings.PrinterName, szString);
            Environment.Exit(1);

        }

        private void button1_Click(object sender, EventArgs e)//does the same as form_load
        {

            //Stop Print Spooler Service then wait 3 seconds
            ServiceController controller = new ServiceController("Spooler");
            controller.Stop();



            //Start Print Spooler Service then wait 3 seconds
            controller.Start();
            //Thread.Sleep(3000);


            //Delete Print Jobs then wait 3 seconds
            //Thread.Sleep(3000);
            string[] filePaths = Directory.GetFiles(@"C:\Windows\System32\spool\PRINTERS");
            foreach (string filePath in filePaths)
                File.Delete(filePath);


            string s = "~JR"; // device-dependent string, need a FormFeed?

            // Allow the user to select a printer.
            PrintDialog pd = new PrintDialog();
            pd.PrinterSettings = new PrinterSettings();
            pd.PrinterSettings.PrinterName = "Label Printer";
            // if (DialogResult.OK == pd.ShowDialog(this))
            //{
            // Send a printer-specific to the printer.
            RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s);



            //Environment.Exit(1);
        }
    }
}



I''m sure this is something simple, I just can''t figure it out! Could someone help?

解决方案

I would recommend not calling Environment.Exit in the Load handler!

Alan.


这篇关于Windows窗体不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆