プログラム



ウィンドウフォームアプリケーションでコンソールを使う

プロジェクトのプロパティ→アプリケーションタブ→出力の種類をコンソールアプリケーションにする、というのは無しね。
デバッグ用の表示のためにコンソールが使いたかったので、閉じたり開いたりする必要があった。
ここに、log4netのConsoleAppenderで表示すればGood!
ちなみに、フォームにTextBoxを使うのは、場合によっては不可です。別スレッドからの呼び出しでInvokeすると、平気で数ms喰われる。

public void 開く()
{
    // コンソールが開いていなければ、
    if (Win32.GetConsoleWindow() == IntPtr.Zero)
    {
        Win32.AllocConsole();
        StreamWriter stdout
         = new StreamWriter(Console.OpenStandardOutput(), Encoding.Default);
        stdout.AutoFlush = true;
        Console.SetOut(stdout);

        IntPtr hWnd = Win32.GetConsoleWindow();
        if (hWnd != IntPtr.Zero)
        {
            // [閉じる]ボタンの無効化
            IntPtr hMenu = GetSystemMenu(hWnd, 0);
            RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
        }
    }
}
public void 閉じる()
{   
    // コンソールが開いていれば、
    if (Win32.GetConsoleWindow() == IntPtr.Zero)
    {
        Console.SetOut(StreamWriter.Null);
        Win32.FreeConsole();
    }
} 
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

VBのアプリケーションフレームワークを使う

2重起動処理(禁止 or 許可:引数を渡しアクティブに)
終了処理(メインフォームが消えたら終了 or 全フォームが消えたら終了)
スプラッシュウィンドウ
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using Microsoft.VisualBasic.ApplicationServices;

namespace VBAppTest {
    internal class Program : WindowsFormsApplicationBase {
        [STAThread]
        static void Main(string[] args) {
            //↓不要:WindowsFormsApplicationBase.EnableVisualStylesで指定
            //Application.EnableVisualStyles();	
              Application.SetCompatibleTextRenderingDefault(false);
            Program app = new Program();
            app.Run(args);
        }
        public Program() : base(AuthenticationMode.Windows) {
            //↓Application.EnableVisualStyles(); と同じ意味??
              this.EnableVisualStyles = true;

            //Formのプロパティ→ApplicationSettings→PropertyBindingで設定しておく
              //Properties.Settings.Default.Save()が自動で呼ばれるはずなのだが、
              // C#のせいかうまくいかないので、Shutdownイベントに、
              //「Properties.Settings.Default.Save();」と書いておく必要がある
              this.SaveMySettingsOnExit = false;	// うまく動かない……

              this.ShutdownStyle = ShutdownMode.AfterAllFormsClose;  //メイン フォームが閉じた後に終了
                                        // AfterAllFormsClose    //最後のフォームが閉じた後に終了

             // 起動時のイベント
             this.Startup += Program_Startup;
           // 起動時(2つ目のインスタンス)のイベント
             this.IsSingleInstance = true;	// falseにすると、StartupNextInstanceは発生しない
             this.StartupNextInstance += Program_StartupNextInstance;
           // 終了時のイベント
             this.Shutdown += Program_Shutdown;
        }

        // 初期化処理の記述は、通常Startupイベントを使用するが、OnStartupをオーバーライドすることで、
         // アプリケーションを起動させない事ができる
         protected override bool OnStartup(StartupEventArgs eventArgs) {
            base.OnStartup(eventArgs);
            //eventArgs.CommandLineでコマンドライン引数が取得できる。
              return true;
        }
		
        // メインフォームはOnCreateMainFormで、スプラッシュウィンドウはOnCreateSplashScreenで生成しないと
         // メインフォームとスプラッシュウィンドウが同時に表示され、スプラッシュウィンドウが消えない
         protected override void OnCreateMainForm() {
            base.OnCreateMainForm();         // 念のためベースを呼んでおく
              this.MainForm = new formMain();
        }
        protected override void OnCreateSplashScreen() {
            base.OnCreateSplashScreen();     // 念のためベースを呼んでおく
              this.MinimumSplashScreenDisplayTime = 1000;
            this.SplashScreen = new formSalush();
        }
        // 初めてのインスタンス起動時のイベント
          void Program_Startup(object sender, StartupEventArgs e) {
            // e.CommandLineでコマンドライン引数を取得できる
         }
        // 2つ目のインスタンス起動時のイベント      
          void Program_StartupNextInstance(object sender, StartupNextInstanceEventArgs e) {
            string cmd = "";
            foreach (string str in e.CommandLine) {
                cmd += str + Environment.NewLine;  //e.CommandLineでコマンドライン引数を取得出来る
              }
            MessageBox.Show(cmd,"引数");
        }
        // アプリケーション終了時に発生するイベント
         void Program_Shutdown(object sender, EventArgs e) {
            Properties.Settings.Default.Save();
        }
    }
}


外部プログラム起動

using Syystem.Diagnostics;
// 標準ブラウザが起動する 
Process.Start("http://www.yahoo.co.jp");
// 標準メーラーが起動する 
Process.Start("mailto:xxxxx@yahoo.co.jp");
// ペイントが起動される(関連付けにより変わる)
Process.Start("sample.jpg");
// メモ帳が起動される(関連付けにより変わる)
Process.Start("log.txt");
最終更新:2009年08月20日 12:15