MailNotifier.cs 3.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace TestWinForms
{
    public partial class MailNotifier : Form
    {
        private MessengerAPI.Messenger Messenger;
15
16
17
        private RegistryKey AppRegKey;

        private const int LIGHT_MAX = 0x1F;
18
19
20
21
22
23

        public MailNotifier()
        {
            InitializeComponent();

            Messenger = new MessengerAPI.Messenger();
24
            AppRegKey = Registry.CurrentUser.CreateSubKey("Software\\MailNotifier");
25
26
27

            for (int i = 1; i < 99; i++)
                cmbComPort.Items.Add("COM" + i.ToString());
28

29
30
31
32
33
34
35
36
37
38
            cmbComPort.SelectedIndex = System.Convert.ToInt32(AppRegKey.GetValue("Port", "1")) - 1;
            serSerialPort.PortName = cmbComPort.Text;

            nicoNotifyIcon.Icon = this.Icon;
            nicoNotifyIcon.MouseClick += new MouseEventHandler(TrayIconClick);
        }

        private void MailNotifier_Load(object sender, EventArgs e)
        {
            Messenger.OnUnreadEmailChange += new MessengerAPI.DMessengerEvents_OnUnreadEmailChangeEventHandler(NewEmail);
39
40
41

            bool UnreadMail = (Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0);
            NotifyLight((!UnreadMail ? LIGHT_MAX : 0), (UnreadMail ? LIGHT_MAX : 0), 0);
42
43
44
45
46
47
48
49
50
51
52
53
54

            Hide();
        }

        private void TrayIconClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void NewEmail(MessengerAPI.MUAFOLDER folder, int amount, ref bool enableDefault)
        {
            if (folder == MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX)
55
56
57
58
            {
                bool UnreadMail = (Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0);
                NotifyLight((!UnreadMail ? LIGHT_MAX : 0), (UnreadMail ? LIGHT_MAX : 0), 0);
            }
59
60
        }

61
        private void NotifyLight(int Red, int Green, int Blue)
62
        {
63
64
65
66
            byte[] buffer = new byte[3];
            buffer[0] = (byte)(0x80 | (Red & LIGHT_MAX));
            buffer[1] = (byte)(0x40 | (Green & LIGHT_MAX));
            buffer[2] = (byte)(0x20 | (Blue & LIGHT_MAX));
67
68
69
70

            try
            {
                serSerialPort.Open();
71
                serSerialPort.Write(buffer, 0, buffer.Length);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
                serSerialPort.Close();
            }
            catch (Exception e)
            {

            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cmbComPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            AppRegKey.SetValue("Port", cmbComPort.SelectedIndex + 1);
            serSerialPort.PortName = cmbComPort.Text;

90
91
92
93
94
95
96
            for (int i = 1; i < 10; i++)
            {
                NotifyLight((LIGHT_MAX / i), (LIGHT_MAX / (i * 10)), 0);
                System.Threading.Thread.Sleep(10);
            }

            for (int i = 10; i > 0; i--)
97
            {
98
99
                NotifyLight((LIGHT_MAX / i), (LIGHT_MAX / (i * 10)), 0);
                System.Threading.Thread.Sleep(10);
100
101
            }

102
103
            bool UnreadMail = (Messenger.get_UnreadEmailCount(MessengerAPI.MUAFOLDER.MUAFOLDER_INBOX) > 0);
            NotifyLight((!UnreadMail ? LIGHT_MAX : 0), (UnreadMail ? LIGHT_MAX : 0), 0);
104
105
106
107
108
109
110
111
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}