All pastes #66872 Raw Edit

C# music (comments)

public text v1 · immutable
#66872 ·published 2006-06-18 17:25 UTC
rendered paste body
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Text;

namespace WinAMP_Music {
    public class MainForm : Form {
        public Thread thread;
        public bool running;
        public string song;
        public MainForm() {
            InitializeComponent();
            // make a new thread
            thread = new Thread(new ThreadStart(loop));
            running = true;
            thread.Start();
        }
        private Label label1;
    
        private void InitializeComponent() {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(295, 22);
            this.label1.TabIndex = 0;
            this.label1.Text = "NP: <nothing>";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // MainForm
            // 
            this.ClientSize = new System.Drawing.Size(292, 21);
            this.Controls.Add(this.label1);
            this.Name = "MainForm";
            this.Text = "WinAMP Music";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
            this.ResumeLayout(false);

        }
        public void loop() {
            while (running) {
                // get the winamp window
                IntPtr winamp = Program.FindWindow("Winamp v1.x", null);
                // make a buffer for the window title
                StringBuilder buff = new StringBuilder(512);
                // get the title text
                Program.GetWindowText(winamp, buff, 512);
                // make a managed string of the buffer
                string str = buff.ToString();
                // remove the song number
                str = str.Split(".".ToCharArray())[1].Trim();
                // remove " - Winamp"
                str = str.Remove(str.Length - 9, 9);
                label1.Text = str;
                song = str;

                //sleep
                Thread.Sleep(100);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
            // cancel close
            e.Cancel = true;
            // end the thread
            running = false;
            // continue close
            e.Cancel = false;
        }
    }
    public class Program {
        public static void Main() {
            // multithread access
            Form.CheckForIllegalCrossThreadCalls = false;
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
        [DllImport("user32.dll")] // WinAPI FindWindow()
        public static extern IntPtr FindWindow(string cls, string window);
        [DllImport("user32.dll")] // WinAPI GetWindowText()
        public static extern int GetWindowText(IntPtr window, StringBuilder text, int len);
    }
}