• ベストアンサー

Visual C# 2008 Express Editionで実行するとDOS窓が出る

私はC#でフォームプログラミングを勉強しようと思い、「Visual C# 2008 Express Edition」をダウンロードして、取り合えずウィンドウを作るだけの簡単なプログラムを実行してみたのですが、ウィンドウと同時に黒い窓(DOS窓というのでしょうか)が現れてしまいます。 作成したアプリケーションデータを実行しても、やはり黒い画面が出てしまい困っています。 本に掲載されていたコードをウィンドウタイトルを変えて、そのままビルドしただけですし、本に付いてきたCD-ROMの中の同じサンプルプログラムのアプリケーションデータを実行しても、黒い画面は現れません。何か方法に誤りがあるのでしょうか、分かる方がいたら教えて頂きたいのですが、下がコードです。 using System; using System.Drawing; using System.Windows.Forms; public class Test : Form { protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); } static void Main() { Form form = new Test(); form.Text = "ウィンドウタイトル"; form.ClientSize = new Size(600, 400); form.BackColor = Color.FromArgb(0xff, 0xff, 0xff); Application.Run(form); } } 参照設定には、 System System.Drawing System.Windows.Forms を含めています。

noname#67169
noname#67169

質問者が選んだベストアンサー

  • ベストアンサー
  • koko_u_
  • ベストアンサー率18% (459/2509)
回答No.2

プロジェクトを右クリックして、プロパティの「出力の種類」が Windows アプリケーションになっていますか?

noname#67169
質問者

お礼

回答有難う御座います。 仰るとおりで「コンソールアプリケーション」になっていました。ウィンドウ自体はちゃんと作れるので、Windowsアプリケーションだと勘違いしていました。設定し直すと黒い画面は出なくなりました。有難う御座いました。

その他の回答 (2)

回答No.3

コマンドプロンプトからコンパイルをされているのでしたら、 csc /t:winexe test.cs のように、/t:winexe をつけてみてください。 (windows アプリケーション) ディフォルトでは確かに、/t:exe (コンソールアプリケーション)になっています。

noname#67169
質問者

お礼

回答有難う御座います。 コマンドプロンプトではありませんでした。問題は解決しました。助言有難う御座いました。

回答No.1

プロジェクトがコンソールアプリケーションになっているとかだったら Windows Applicationにすれば治る。 #Windows以外は出来るだけOSSを使うように努力しようとして #SharpDevelopを使うようにしているので #やり方は知らん。

noname#67169
質問者

お礼

回答有難う御座いました。 その通りだった様で問題は無事解決しました。

関連するQ&A

  • C#で派生クラスから描画処理を行う

    C#を勉強しているのですが、GUIを作り初めて描画処理で分からない所があり、質問させて頂きます。 基本クラスの方で「Hello, world!」という文字列をDrawStringで表示させる事は出来たのですが、それを基本クラスを継承した派生クラスのメソッドで行うと何も表示されないんです。 以下が試したコードです。 //基本クラスSample1 using System; using System.Drawing; using System.Windows.Forms; public class Sample1 : Form {   protected Bitmap image; protected Graphics g; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Sample2 s = new Sample2(); s.helloworld(); e.Graphics.DrawImage(image, 0, 0); } public Sample1() { SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); image = new Bitmap(600, 400); g = Graphics.FromImage(image); } static void Main() { Form form = new Sample1(); form.Text = "sample"; form.ClientSize = new Size(600, 400); form.BackColor = Color.FromArgb(0xff, 0xff, 0xff); Application.Run(form); } } //派生クラスSample2 using System; using System.Drawing; using System.Windows.Forms; public class Sample2 : Sample1 { Brush brush = new SolidBrush(Color.Black); public void helloworld() { g.DrawString("Hello, world!", this.Font, brush, 10, 10); } } なぜ表示されないのか分かる方いらしたら、ご教授願えないでしょうか。是非お願いします。

  • Graphicsプロパティ

    例えば using System; using System.Drawing; using System.Windows.Forms; class SeparateMain { public static void Main() { Application.Run(new AnotherHelloWorld()); } } class AnotherHelloWorld : Form { public AnotherHelloWorld() { Text = "Another Hello World"; BackColor = Color.White; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; grfx.DrawString("Hello, windows Forms!", Font, Brushes.Black, 0, 0); } } の中の Graphics grfx = pea.Graphics; という部分なのですが、この部分はなぜ必要で、何をしてるのでしょう。 GDI+というのでしょうか System.Drawing.Graphicsというクラスのインスタンス、grfxを定義して、 PaintEventArgsのインスタンスpeaのGraphicsプロパティを代入 この処理が必要な理由がわかりません。お願いします。

  • Visual c# スレッド

    プログラム初心者です。 Visual c#にてスレッド関数を使ったプログラムを見よう見まねで 作りました。 スタートボタンを押すとピクチャーボックスの位置が 左から右へ移動し、ストップボタンを押すと止まる。 ピクチャーボックスの位置をテキストボックスに表示する。 というプログラムのつもりです。 デバックの状態でスタートボタンを押すとエラーメッセージが出て テキストボックスに位置を書き込むの関数のところが緑色にハイライトされます。エラーの内容は「有効でないスレッド間の操作」とありますが、どうしたら良いのか分かりません。 エラーの直し方を教えてください。 プログラムコードは以下です。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace bit_map_08._1._ { public partial class Form1 : Form { private Thread thread; static private int Position = 0; static private int i; static bool BtnOnFig; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { thread = new System.Threading.Thread(newSystem.Threading.ThreadStart(MainThread)); thread.Start(); } private void PointMove() { if (i < 300) { i = i + 1; Position = Position + i; textBox1.Text = Convert.ToString(Position); pictureBox1.Left = (Position); } if (i == 300) { i = 0; } } private void MainThread() { while( true ) { Thread.Sleep(100); if (BtnOnFig == true) { PointMove(); } } } private void button1_Click(object sender, EventArgs e) { BtnOnFig = true; } private void button2_Click(object sender, EventArgs e) { BtnOnFig = false; } } }

  • C# フォームを閉じてもプログラムが終了しない

    前略 ・C#の初心者です。 ・下記のようなプログラムを作りたいと思っています。プログラムは<作りたいプログラム>の仕様どうりに動作していますが、Form1のFormClosingイベントに Application.Exit()を追加しないと フォーム1で "X"(閉じる)をクリックしても(フォームは非表示になりますが)プログラムが終了しません。プログラムでどこかおかしな部分があると思っています。Application.Exit()を追加しないでもプログラムを終了する方法を教えてください。 <作りたいプログラム> (1)Form1 のbutton1をクリックすると新しいForm2が作成され表示される。Form2が表示されるとForm1は非表示となる。 (2)Form2 のbutton1をクリックするとForm2が非表示となりForm1が表示される。 (3)Form1 の "X"(閉じる)をクリックしてプログラムを終了する。 //Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace formClose { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.Show(); //フォーム2を表示 this.Hide(); //フォーム1を非表示 } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); //アプリケーション終了 } } } //Form2.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace formClose { public partial class Form2 : Form { Form1 form1 = new Form1(); public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.Close(); //フォーム2を閉じる } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { form1.Show(); //フォーム1を表示する } } } 以上

  • フォームを閉じる方法

    VC#2008ExpressEditionを使用して仕事で使う在庫管理ソフトを作成しています。 まず、ウェルカムページが開いて、その後認証ページが開くように作っていますが、認証ページが開いたのと同時にウェルカムページを閉じたいのですが、閉じることが出来ません。どなたか教えて下さい。 ウェルカムページのコーディング using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace zaiko { public partial class welcomeForm : Form { public welcomeForm() { InitializeComponent(); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Form mainForm = new mainForm(); mainForm.Show(); } } } 認証ページのコーディング using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace zaiko { public partial class mainForm : Form { public mainForm() { InitializeComponent(); } private void mainForm_Load(object sender, EventArgs e) { Form welcomeForm = new Form(); welcomeForm.Close(); } } }

  • C# 計算処理中に実行中ウィンドウを表示させたい。

    アドバイスをお願いいたします。 やりたいこと: (1)メインウィンドウで「計算始めるボタン」を押す。 (2)すると計算中には「実行中」の文字が別ウィンドウで立ち上がる。 (3)計算が終わると別ウィンドウが閉じる。 下記プログラムの不具合: 別ウィンドウで「実行中」の文字を出させたいが、文字部分が描画されない。 注意:  下記プログラムはAthlon3000+で1秒ほどかかります。 CPUが遅いマシンで試す場合は「//計算処理」部分のfor重みを軽減させて実行して下さい。 using System; using System.Drawing; using System.Windows.Forms; //メインウィンドウ public class MainForm : Form { private Button button; private Label label; public MainForm() { button = new Button(); button.Text = "計算GO!!"; button.Click += new System.EventHandler(button_click); label = new Label(); label.Location = new Point(50,50); label.Text = "メインウィンドウ!"; Controls.Add(button); Controls.Add(label); Text = "MainForm"; BackColor = Color.Blue; } static void Main() { Application.Run(new MainForm()); } //計算GOボタンをクリックした時の処理 private void button_click(object sender, System.EventArgs e) { double z; SubForm Sub = new SubForm(); Sub.Show(); //計算処理 for(int i=0; i<=500000000; i++) z = double.MaxValue * double.MinValue; Sub.Close(); MessageBox.Show("計算終了"); } } //計算中を通知するウィンドウ public class SubForm : Form { private Label label; public SubForm(){ label = new Label(); label.Text = "実行中!"; label.Location = new Point(50,50); Text = "計算中通知ウィンドウ"; BackColor = Color.Red; Controls.Add(label); } }

  • なぜForm型にキャストするのでしょう?

    using System; using System.Drawing; using System.Windows.Forms; class PaintHello { public static void Main() { Form form = new Form(); form.Text = "Paint Hello"; form.BackColor = Color.White; form.Paint += new PaintEventHandler(MyPaintHandler); Application.Run(form); } static void MyPaintHandler(object objSender,PaintEventArgs pea) { Form form = (Form)objSender;//←この部分が分かりません。 Graphics grfx = pea.Graphics; grfx.DrawString("Hello,world!", form.Font, Brushes.Black, 0, 0); } } C#の勉強をしているのですが、イベントを発生させるためにデリゲートに自分で作ったメソッドを 引数として送るというのは理解できるのですが、 このMyPaintHandlerのobject objSender という部分が、書籍の説明だと「objSenderはForm型のオブジェクトなのでキャストはうまくいく」 と書かれています。 object型というのを調べるとSystem名前空間にObjectというクラスがあるのですが、これはすべてのクラスのルートであり、、、とMSDNに書かれていて、なぜForm型と言えるのかわかりません。 そもそもこのキャストはなぜ必要なのでしょう。お願いします。

  • Visual C#で、usingが出来ないクラスがある?

    最近、Visual C# 2005 Express EditionでC#の勉強を始めた者です。 いくつかテストプログラムを実行しようと思ったのですが、JavaのEclipse等と操作感覚の違いに戸惑っています。 質問なのは、usingできるクラスについてです。 新しいプロジェクトを作成する時、「Windowsアプリケーション」を選択すると、次のusingが使えます。 > using System.Windows.Forms; しかし「コンソールアプリケーション」を選択した場合、上記のusingは使用できないのです。 ただ、プロジェクトの「参照設定」で.Netタブの「System.Windows.Forms」を追加すれば、一応使えるようになります。 考えてみたのですが、もしかしてプロジェクトを作成する時に選択する「Windowsアプリケーション」や「コンソールアプリケーション」は、参照設定に追加する内容が違うという事なのでしょうか? 一応使う事が出来るので、今の所は問題ではないのですが、付属のマニュアルを読んでも詳細が解りにくく困っています。 私の認識は間違っているのでしょうか?

  • 別スレッドからメインスレッドのテキストボックスに文字を表示させたい

    Visual C++ 2005 Express Editionを使用している初心者です。 タイトルの方法がどうしてもわからないので教えてください。 うまく説明できないので聞きたいところのコードを載せます。 //SAMPLE.cpp #include "stdafx.h" #include "Form1.h" using namespace SAMPLE; int main(array<System::String ^> ^args) { Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); Application::Run(gcnew Form1()); } //Form1.h #include <process.h> #include <vcclr.h> namespace SAMPLE { unsigned __stdcall counter(void *arg); int thread_id1; unsigned dummy; using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public:Form1(void) { InitializeComponent(); //別スレッド起動 thread_id1=_beginthreadex(NULL,0,counter,(void *)1,0,&dummy); } private: System::Windows::Forms::TextBox^ textBox1; private: System::ComponentModel::Container ^components; void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); //省略(コンポーネント初期化) } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; unsigned __stdcall counter(void *arg){ //別スレッド //ここで文字列を記述して上のtextBox1に表示したい return 0; } } 初歩的なことかもしれませんが、 よろしくお願いします。

  • キー入力が反応しません。

    VisualStudio2005のC#になります。 ボタンを押す ラベルに文章 ボタン消える Enterキー押す ラベルの文章が変わる という処理を行いたいのですが、”Enterキー押す”以下が実行されません。なぜなのでしょうか?どうすれば実行されるのでしょう。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace 消す { public partial class Form1 : Form { private bool EnterKeyDown; public Form1() { InitializeComponent(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (EnterKeyDown == false) { EnterKeyDown = true; label1.Text = "押したよ"; } else { EnterKeyDown = false; label1.Text = "押したよ2"; } } private void button1_Click(object sender, EventArgs e) { button1.Visible = false; } } }

専門家に質問してみよう