|
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;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Configuration;
namespace TrafficLights
{
public partial class TrafficLights : Form
{
#region DLLImports
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll")]
static extern bool GetKeyboardState(byte[] lpKeyState);
#endregion DLLImports
#region Constants
const int VK_NUMLOCK = 0x90;
const int VK_SCROLL = 0x91;
const int VK_CAPITAL = 0x14;
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
#endregion
#region Enums
enum LEDType
{
NUMLOCK = 0x90,
SCROLL = 0x91,
CAPSLOCK = 0x14,
}
#endregion
#region Variables
LEDType m_sendLED = LEDType.NUMLOCK;
LEDType m_receiveLED = LEDType.NUMLOCK;
string m_adapter = "";
PerformanceCounterCategory m_performanceCounterCategory;
PerformanceCounter m_performanceCounterSent;
PerformanceCounter m_performanceCounterReceived;
Thread m_backGroundThread;
//EventWaitHandle m_wh = new AutoResetEvent(false);
#endregion
public TrafficLights()
{
InitializeComponent();
notifyIcon1.ShowBalloonTip(1);
}
private void TrafficLights_Load(object sender, EventArgs e)
{
this.Opacity = 0;
m_sendLED = (LEDType)Enum.Parse(typeof(LEDType), Properties.Settings.Default.SendLED.ToString());
m_receiveLED = (LEDType)Enum.Parse(typeof(LEDType), Properties.Settings.Default.ReceiveLED.ToString());
m_adapter = Properties.Settings.Default.Adapter.ToString();
m_performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
comboBoxAdapter.Items.AddRange(m_performanceCounterCategory.GetInstanceNames());
comboBoxSendLED.Items.AddRange(Enum.GetNames(typeof(LEDType)));
comboBoxSendLED.Text = m_sendLED.ToString();
comboBoxReceiveLED.Items.AddRange(Enum.GetNames(typeof(LEDType)));
comboBoxReceiveLED.Text = m_receiveLED.ToString();
comboBoxAdapter.Text = m_adapter;
Init();
}
void Init()
{
string instance;
if (m_adapter == "")
instance = m_performanceCounterCategory.GetInstanceNames()[0]; // 1st NIC !
else
instance = m_adapter;
if (m_performanceCounterSent != null)
m_performanceCounterSent.Dispose();
if (m_performanceCounterReceived != null)
m_performanceCounterReceived.Dispose();
m_performanceCounterSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
m_performanceCounterReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
m_backGroundThread = new Thread(new ThreadStart(DoWork));
m_backGroundThread.Start();
}
public void DoWork()
{
while (true)
{
float fSentValue = m_performanceCounterSent.NextValue() / 1024;
float fReceivedValue = m_performanceCounterReceived.NextValue() / 1024;
if (fSentValue > 0)
{
keybd_event((byte)m_sendLED, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event((byte)m_sendLED, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
}
if (fReceivedValue > 0)
{
keybd_event((byte)m_receiveLED, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event((byte)m_receiveLED, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
}
//UpdateIcon(text);
Thread.Sleep(50);
}
}
void UpdateIcon(string text)
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(TrafficLights));
Bitmap iconBmp = ((System.Drawing.Icon)(resources.GetObject
("notifyIcon1.Icon"))).ToBitmap();
// Create an ImageGraphics Graphics object from bitmap Image
System.Drawing.Graphics ImageGraphics = System.Drawing.Graphics.FromImage(iconBmp);
// Draw random code within Image
System.Drawing.Font drawFont = new System.Drawing.Font("Arial Narrow", 20, FontStyle.Regular);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
ImageGraphics.DrawString(text, drawFont, drawBrush, -2, 1, drawFormat);
// Dispose used Objects
drawFont.Dispose();
drawBrush.Dispose();
ImageGraphics.Dispose();
//
notifyIcon1.Icon = Icon.FromHandle(iconBmp.GetHicon());
notifyIcon1.Text = text;
}
private void TrafficLights_FormClosed(object sender, FormClosedEventArgs e)
{
m_backGroundThread.Abort();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void buttonHide_Click(object sender, EventArgs e)
{
//m_wh.Reset();
m_sendLED = (LEDType)Enum.Parse(typeof(LEDType), comboBoxSendLED.Text);
m_receiveLED = (LEDType)Enum.Parse(typeof(LEDType), comboBoxReceiveLED.Text);
m_adapter = comboBoxAdapter.Text;
Properties.Settings.Default.SendLED = (byte)m_sendLED;
Properties.Settings.Default.ReceiveLED = (byte)m_receiveLED;
Properties.Settings.Default.Adapter = m_adapter;
//m_wh.Set();
Properties.Settings.Default.Save();
Init();
this.Opacity = 0;
}
private void configToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Opacity = 100;
}
private void comboBoxSendLED_SelectedIndexChanged(object sender, EventArgs e)
{
//m_wh.Reset();
//m_sendLED = (LEDType)Enum.Parse(typeof(LEDType), comboBoxSendLED.Text);
//Properties.Settings.Default.SendLED = (byte)m_sendLED;
//m_wh.Set();
//Properties.Settings.Default.Save();
}
private void comboBoxReceiveLED_SelectedIndexChanged(object sender, EventArgs e)
{
//m_wh.Reset();
//m_receiveLED = (LEDType)Enum.Parse(typeof(LEDType), comboBoxReceiveLED.Text);
//Properties.Settings.Default.ReceiveLED = (byte)m_receiveLED;
//m_wh.Set();
//Properties.Settings.Default.Save();
}
private void comboBoxAdapter_SelectedIndexChanged(object sender, EventArgs e)
{
//m_wh.Reset();
//m_adapter = comboBoxAdapter.Text;
//Properties.Settings.Default.Adapter = m_adapter;
//m_wh.Set();
//Properties.Settings.Default.Save();
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.