Click here to Skip to main content
15,886,077 members
Articles / Programming Languages / C#

Mars Mission (2) : Explore the Solar System

Rate me:
Please Sign up or sign in to vote.
4.97/5 (36 votes)
20 Nov 2010CPOL19 min read 50.3K   2.2K   45  
strategy/action game defending the solar system : interplanetary space
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BitmapRegion;

public class formNewGameMsgBox : Form
{
	private Button btnYes;
	private Label lblNewGame;
	private Button btnNo;

	private void InitializeComponent()
	{
		this.btnYes = new System.Windows.Forms.Button();
		this.btnNo = new System.Windows.Forms.Button();
		this.lblNewGame = new System.Windows.Forms.Label();
		this.SuspendLayout();
		// 
		// btnYes
		// 
		this.btnYes.BackgroundImage = global::Mars_Mission.Properties.Resources.thumbs_up_small;
		this.btnYes.Location = new System.Drawing.Point(157, 272);
		this.btnYes.Name = "btnYes";
		this.btnYes.Size = new System.Drawing.Size(70, 69);
		this.btnYes.TabIndex = 0;
		this.btnYes.UseVisualStyleBackColor = true;
		// 
		// btnNo
		// 
		this.btnNo.BackgroundImage = global::Mars_Mission.Properties.Resources.thumbs_down_small;
		this.btnNo.Location = new System.Drawing.Point(222, 273);
		this.btnNo.Name = "btnNo";
		this.btnNo.Size = new System.Drawing.Size(72, 72);
		this.btnNo.TabIndex = 1;
		this.btnNo.UseVisualStyleBackColor = true;
		// 
		// lblNewGame
		// 
		this.lblNewGame.AutoSize = true;
		this.lblNewGame.BackColor = System.Drawing.Color.Transparent;
		this.lblNewGame.Font = new System.Drawing.Font("Microsoft Sans Serif", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
		this.lblNewGame.Location = new System.Drawing.Point(115, 138);
		this.lblNewGame.Name = "lblNewGame";
		this.lblNewGame.Size = new System.Drawing.Size(209, 39);
		this.lblNewGame.TabIndex = 2;
		this.lblNewGame.Text = "New Game?";
		// 
		// formNewGameMsgBox
		// 
		this.BackgroundImage = global::Mars_Mission.Properties.Resources.pewter_frame;
		this.ClientSize = new System.Drawing.Size(448, 341);
		this.Controls.Add(this.lblNewGame);
		this.Controls.Add(this.btnNo);
		this.Controls.Add(this.btnYes);
		this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
		this.Name = "formNewGameMsgBox";
		this.ResumeLayout(false);
		this.PerformLayout();

	}
	public bool bolResponse;
	public formNewGameMsgBox()
	{
		InitializeComponent();

		BitmapRegion.BitmapRegion.CreateControlRegion(this, Mars_Mission.Properties.Resources.pewter_frame);
		BitmapRegion.BitmapRegion.CreateControlRegion(btnYes, Mars_Mission.Properties.Resources.thumbs_up_small);
		BitmapRegion.BitmapRegion.CreateControlRegion(btnNo, Mars_Mission.Properties.Resources.thumbs_down_small);

		btnYes.Click += new EventHandler(btnYes_Click);
		btnNo.Click += new EventHandler(btnNo_Click);
		btnYes.MouseEnter += new EventHandler(btnYes_MouseEnter);
		btnYes.MouseLeave += new EventHandler(btnYes_MouseLeave);
		btnNo.MouseEnter += new EventHandler(btnNo_MouseEnter);
		btnNo.MouseLeave += new EventHandler(btnNo_MouseLeave);
		VisibleChanged += new EventHandler(formNewGameMsgBox_VisibleChanged);
	}

	void btnNo_MouseLeave(object sender, EventArgs e)
	{
		BitmapRegion.BitmapRegion.CreateControlRegion(btnNo, Mars_Mission.Properties.Resources.thumbs_down_small);
	}

	void btnNo_MouseEnter(object sender, EventArgs e)
	{
		BitmapRegion.BitmapRegion.CreateControlRegion(btnNo, Mars_Mission.Properties.Resources.thumbs_down_small_hightlight);
	}

	void btnYes_MouseLeave(object sender, EventArgs e)
	{
		BitmapRegion.BitmapRegion.CreateControlRegion(btnYes, Mars_Mission.Properties.Resources.thumbs_up_small);		
	}

	void btnYes_MouseEnter(object sender, EventArgs e)
	{
		BitmapRegion.BitmapRegion.CreateControlRegion(btnYes, Mars_Mission.Properties.Resources.thumbs_up_small_highlight );
	}

	void formNewGameMsgBox_VisibleChanged(object sender, EventArgs e)
	{
		Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
		Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
	}

	void btnNo_Click(object sender, EventArgs e)
	{
		bolResponse = false;
		Hide();
	}

	void btnYes_Click(object sender, EventArgs e)
	{
		bolResponse = true;
		Hide();
	}
}

public class classMessageBox : Form
{
    string reply;
    int intButtonLeft;
    public System.Windows.Forms.Button[] btns = new System.Windows.Forms.Button[0];
    public System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();


    public classMessageBox(string strTitle, string strText, params string[] strButtons)
    {
        Controls.Add(lbl);
        lbl.Text = strText;
        lbl.Top = 5; lbl.Left = 5;
        lbl.AutoSize = true;
		FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

        foreach (string strButton in strButtons)
            addButton(strButton);

        resizeForm();
    }

    void addButton(string strThisButton)
    {
        System.Windows.Forms.Button btnNew = new System.Windows.Forms.Button();
        Controls.Add(btnNew);
        btnNew.Visible = true;
        btnNew.AutoSize = true; btnNew.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        btnNew.Text = strThisButton.Trim();

        btnNew.Click += new EventHandler(btn_Click);
        btnNew.FontChanged += new EventHandler(btnNew_FontChanged);

        Array.Resize<System.Windows.Forms.Button>(ref btns, btns.Length + 1);
        btns[btns.Length - 1] = btnNew;
    }

    void btn_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.Button btnThis = (System.Windows.Forms.Button)sender;
        reply = btnThis.Text;
        Close();
    }

    void btnNew_FontChanged(object sender, EventArgs e)
    { initResize(); }
    void lbl_FontChanged(object sender, EventArgs e) { initResize(); }

    void initResize()
    {
        System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
        tmr.Interval = 5;
        tmr.Tick += new EventHandler(tmr_Tick);
        tmr.Enabled = true;
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        System.Windows.Forms.Timer tmrMe = (System.Windows.Forms.Timer)sender;
        tmrMe.Enabled = false;
        resizeForm();
    }

    void resizeForm()
    {
        intButtonLeft = 5;
        foreach (System.Windows.Forms.Button btn in btns)
        {
            btn.Left = intButtonLeft;
            intButtonLeft += btn.Width + 5;
        }

        Width = btns[btns.Length - 1].Left + btns[btns.Length - 1].Width + 25;
        lbl.AutoSize = true;
        // resize label
        int intNumLines = (int)Math.Ceiling((double)lbl.Width / (double)Width) + 1;
        lbl.AutoSize = false;
        lbl.FontChanged += new EventHandler(lbl_FontChanged);
        lbl.Height = lbl.Font.Height * intNumLines;
        lbl.Width = Width - 20;

        int intTallestButtonHeight = 0;
        foreach (System.Windows.Forms.Button thisButton in btns)
        {
            thisButton.Top = lbl.Top + lbl.Height + 5;
            if (intTallestButtonHeight < thisButton.Height)
                intTallestButtonHeight = thisButton.Height;
        }

        Height = btns[0].Top + intTallestButtonHeight + 35;

        Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
        Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
    }

    public string Reply { get { return reply; } }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO unemployable
Canada Canada
Christ Kennedy grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University. He is unemployable and currently living in Moncton, N.B. writing his next novel.

Comments and Discussions