Some C# Basics – Buttons, Transparency, and On Top

email me

The form design itself

What we have here are three basic buttons and a transparency slider. The idea was to create a power panel that demonstrated buttons, transparency, and set on top; I also added the background, the Invader Zim as a separate image placeholder, added a title into the title bar, controlled the size of the panel, and a notify icon with messages and events.

 

My Form1.cs, containing my buttons

// MrNetTek
// eddiejackson.net
// 8/8/2016
// free for public use 
// free to claim as your own

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private System.Windows.Forms.TrackBar trackBar1 = new System.Windows.Forms.TrackBar();

public int OpacityValue
{
get { return (int)(this.Opacity * 100); }
set { this.Opacity = (double)value / 100.0; }
}
public Form1()
{
InitializeComponent();
Init();
this.OpacityValue = 100;
this.trackBar1.DataBindings.Add("Value", this, "OpacityValue", false, DataSourceUpdateMode.OnPropertyChanged);
this.notifyIcon1.BalloonTipTitle = "Balloon Tip Title";
this.notifyIcon1.BalloonTipText = "Balloon Tip Text.";
this.notifyIcon1.ShowBalloonTip(5000);
}
private void Init()
{
this.trackBar1.Location = new System.Drawing.Point(130, 70);
this.trackBar1.Maximum = 100;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(345, 15);
this.trackBar1.TabIndex = 0;
this.Controls.Add(this.trackBar1);
this.trackBar1.BackColor = System.Drawing.Color.Black;
//this.trackBar1.BackColor = SystemColors.Control;
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe", "");
}

private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("calc.exe", "");
}

private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("mspaint.exe", "");
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void MouseDownHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)

{
//MessageBox.Show("Left");
this.WindowState = FormWindowState.Normal;
}

if (e.Button == MouseButtons.Right)

{
//MessageBox.Show("Right");
this.WindowState = FormWindowState.Minimized;
}
}
}

}

 

My Form1.Designer.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
partial class Form1
{
/// 
<summary>
/// Required designer variable.
/// </summary>

private System.ComponentModel.IContainer components = null;

/// 
<summary>
/// Clean up any resources being used.
/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// 
<summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("Book Antiqua", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(24, 22);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(98, 47);
this.button1.TabIndex = 0;
this.button1.Text = "Notepad";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("Book Antiqua", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button2.Location = new System.Drawing.Point(137, 22);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(98, 47);
this.button2.TabIndex = 1;
this.button2.Text = "Calc";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("Book Antiqua", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button3.Location = new System.Drawing.Point(250, 22);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(98, 47);
this.button3.TabIndex = 3;
this.button3.Text = "MSPaint";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(697, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(81, 99);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 4;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// notifyIcon1
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "Click";
this.notifyIcon1.Visible = true;
notifyIcon1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MouseDownHandler);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(790, 123);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Power Panel";
this.TopMost = true;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
}

internal class MouseEventHandler
{
private Action<object, MouseEventArgs> notifyIcon1_MouseClick;

public MouseEventHandler(Action<object, MouseEventArgs> notifyIcon1_MouseClick)
{
this.notifyIcon1_MouseClick = notifyIcon1_MouseClick;
}
}
}