www.pudn.com > baoyouzhuomian.zip > Form1.cs, change:2011-08-06,size:20968b


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.Runtime.InteropServices; 
using 我的工程; 
using MyClassKU; 
using System.Diagnostics; 
using System.Data.OleDb; 
using Microsoft.Win32; 
using System.IO; 
 
namespace 宝友桌面 
{ 
    public partial class Form1 : Form 
    { 
        public const Int32 AW_HOR_POSITIVE = 0x00000001; 
        public const Int32 AW_HOR_NEGATIVE = 0x00000002; 
        public const Int32 AW_VER_POSITIVE = 0x00000004; 
        public const Int32 AW_VER_NEGATIVE = 0x00000008; 
        public const Int32 AW_CENTER = 0x00000010; 
        public const Int32 AW_HIDE = 0x00010000; 
        public const Int32 AW_ACTIVATE = 0x00020000; 
        public const Int32 AW_SLIDE = 0x00040000; 
        public const Int32 AW_BLEND = 0x00080000;  
        public Form1() 
        { 
            InitializeComponent(); 
            AnimateWindow(this.Handle, 500, AW_SLIDE + AW_VER_NEGATIVE); 
        } 
 
        public class GetSystemIcon 
        { 
            public Icon GetIconByFileName(string fileName) 
            { 
                if (fileName == null || fileName.Equals(string.Empty)) return null; 
                if (!File.Exists(fileName)) return null; 
 
                SHFILEINFO shinfo = new SHFILEINFO(); 
                //Use this to get the small Icon 
                Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON); 
                //The icon is returned in the hIcon member of the shinfo struct 
                Icon myIcon = Icon.FromHandle(shinfo.hIcon); 
                return myIcon; 
            } 
        } 
        [StructLayout(LayoutKind.Sequential)] 
        public struct SHFILEINFO 
        { 
            public IntPtr hIcon; 
            public IntPtr iIcon; 
            public uint dwAttributes; 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
            public string szDisplayName; 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 
            public string szTypeName; 
        } 
        class Win32 
        { 
            public const uint SHGFI_ICON = 0x100; 
            public const uint SHGFI_LARGEICON = 0x0; // 'Large icon 
            public const uint SHGFI_SMALLICON = 0x1; // 'Small icon 
 
            [DllImport("shell32.dll")] 
            public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); 
            [DllImport("shell32.dll")] 
            public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons); 
 
            [DllImport("User32.dll")] 
            public static extern int MessageBox(int h, string m, string c, int type); 
 
        } 
        string b;  
        StringChange sChange = new StringChange(); 
        string strPath = Application.StartupPath + "\\buttons.mdb"; 
        private void Form1_Load(object sender, EventArgs e) 
        { 
             
            this.AllowDrop = true; 
            timer1.Start(); 
            System.Windows.Forms.Timer StopRectTimer = new System.Windows.Forms.Timer(); 
            StopRectTimer.Tick += new EventHandler(timer1_Tick); 
            StopRectTimer.Interval = 50; 
            StopRectTimer.Enabled = true; 
            StopRectTimer.Start(); 
             
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source='" + strPath + "'"; 
            OleDbConnection oleCon = new OleDbConnection(ConStr); 
            string sg = "select * from buttons "; 
            string strDel = "delete * from buttons where tp='490'"; 
            OleDbCommand oleDel = new OleDbCommand(strDel, oleCon); 
            oleCon.Open(); 
            foreach (Control sad in this.Controls) 
            { 
                oleDel.ExecuteNonQuery(); 
            } 
            OleDbDataAdapter adapter = new OleDbDataAdapter(sg, oleCon); 
            // 创建DataSet,用于存储数据. 
            DataSet testDataSet = new DataSet(); 
            // 执行查询,并将数据导入DataSet. 
            adapter.Fill(testDataSet, "buttons"); 
            //string ab="select name from buttons  
            oleCon.Close(); 
            oleCon.Dispose(); 
            foreach (DataRow testRow in testDataSet.Tables["buttons"].Rows) 
            { 
                 
                string qq; 
                Button bt = new Button(); 
                bt.Name = testRow[0].ToString(); 
                //bt.Text = testRow[1].ToString(); 
                try 
                { 
                    GetSystemIcon asd = new GetSystemIcon(); 
                    string yy = testRow[2].ToString(); 
                    Icon a = asd.GetIconByFileName(@yy); 
                    bt.BackgroundImage = a.ToBitmap(); 
                    bt.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; 
                } 
                catch (Exception) 
                { 
                } 
                if (testRow[3].ToString() ==  ""||testRow[4].ToString() =="") 
                { 
                    bt.Top = bt.Left = 0; 
                } 
                else 
                { 
                    string f = testRow[3].ToString(); 
                    string g = testRow[4].ToString(); 
                    bt.Left = Convert.ToInt32(f); 
                    bt.Top = Convert.ToInt32(g) ; 
                } 
                qq = testRow[2].ToString(); 
                bt.Size = new System.Drawing.Size(60, 50); 
                bt.BackColor = Color.SkyBlue; 
                bt.ForeColor = Color.Black ; 
                bt.TextAlign = System.Drawing.ContentAlignment.BottomCenter; 
                this.Controls.Add(bt); 
                bt.Click += new EventHandler(mButton_Click); 
                bt.MouseMove += new MouseEventHandler(myButton_MouseMove); 
                bt.MouseUp += new MouseEventHandler(myButton_MouseUp); 
                ToolTip toolTip1 = new ToolTip(); 
                toolTip1.AutoPopDelay = 1000; 
                toolTip1.InitialDelay = 1000; 
                toolTip1.ReshowDelay = 1500; 
                toolTip1.ShowAlways = true; 
                toolTip1.SetToolTip(bt, bt.Name); 
            } 
        } 
        
        private void timer1_Tick(object sender, EventArgs e) 
        { 
            DateTime dt = DateTime.Now; 
            label1.Text = dt.ToString(); 
            if (this.Bounds.Contains(Cursor.Position)) 
            { 
                switch (this.StopAanhor) 
                { 
                    case AnchorStyles.Top: 
                        this.Location = new Point(this.Location.X, 0); 
                        break; 
                    case AnchorStyles.Left: 
                        this.Location = new Point(0, this.Location.Y); 
                        break; 
                    case AnchorStyles.Right: 
                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y); 
                        break; 
                    case AnchorStyles.Bottom: 
                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height); 
                        break; 
                } 
            } 
            else 
            { 
                switch (this.StopAanhor) 
                { 
                    case AnchorStyles.Top: 
                        this.Location = new Point(this.Location.X, (this.Height - 1) * (-1)); 
                        break; 
                    case AnchorStyles.Left: 
                        this.Location = new Point((-1) * (this.Width - 1), this.Location.Y); 
                        break; 
                    case AnchorStyles.Right: 
                        this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 1, this.Location.Y); 
                        break; 
                    case AnchorStyles.Bottom: 
                        this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 1)); 
                        break; 
                } 
            } 
        } 
 
        internal AnchorStyles StopAanhor = AnchorStyles.None; 
        private void mStopAnhor() 
        { 
            if (this.Top <= 0 && this.Left <= 0) 
            { 
                StopAanhor = AnchorStyles.None; 
            } 
            else if (this.Top <= 0) 
            { 
                StopAanhor = AnchorStyles.Top; 
            } 
            else if (this.Left <= 0) 
            { 
                StopAanhor = AnchorStyles.Left; 
            } 
            else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width) 
            { 
                StopAanhor = AnchorStyles.Right; 
            } 
            else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height) 
            { 
                StopAanhor = AnchorStyles.Bottom; 
            } 
            else 
            { 
                StopAanhor = AnchorStyles.None; 
            } 
        } 
 
        private void Form1_LocationChanged(object sender, EventArgs e) 
        { 
            this.mStopAnhor(); 
        } 
        [DllImport("user32.dll", EntryPoint = "SwapMouseButton")] 
        public static extern int SwapMouseButton(int bSwap); 
        [DllImportAttribute("user32.dll")] 
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags); 
 
        const int SHERB_NOCONFIRMATION = 0x000001; 
        const int SHERB_NOPROGRESSUI = 0x000002; 
        const int SHERB_NOSOUND = 0x000004; 
        [DllImportAttribute("shell32.dll")] 
        private static extern int SHEmptyRecycleBin(IntPtr handle, string root, int falgs); 
 
        private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
        { 
            AnimateWindow(this.Handle, 500, AW_SLIDE + AW_VER_NEGATIVE + AW_HIDE); 
        } 
 
        private void Form1_DragEnter(object sender, DragEventArgs e) 
        { 
            Button myButton = new Button(); 
            myButton.Size = new System.Drawing.Size(60, 50); 
            myButton.BackColor = Color.LightSteelBlue; 
            myButton.ForeColor = Color.Transparent; 
            myButton.TextAlign = System.Drawing.ContentAlignment.BottomCenter; 
            myButton.Left = 210; 
            myButton.Top = 366; 
            myButton.Click += new EventHandler(mButton_Click); 
            myButton.MouseMove += new MouseEventHandler(myButton_MouseMove); 
            myButton.MouseUp += new MouseEventHandler(myButton_MouseUp); 
            this.Controls.Add(myButton); 
 
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
            { 
                string qq; 
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); 
                string a = string.Join(files.ToString(), files); 
                SChange aa = new SChange(); 
                b = aa.Schange(a); 
                
                if (sChange.GetEnd(b, '.', 1) == "lnk") 
                { 
                    IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); 
                    IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(b); 
                    string s = shortcut.TargetPath.ToString(); 
                    SChange ww = new SChange(); 
                    qq = ww.Schange(s); 
                } 
                else 
                { 
                    qq = b; 
                } 
 
                string tt = sChange.GetEnd(sChange.GetEnd(b, '\\', 1), '.', 2); 
                string nn = sChange.GetEnd(sChange.GetEnd(b, '\\', 1), '.', 2); 
                string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source='" + strPath + "'"; 
                OleDbConnection oleCon = new OleDbConnection(ConStr); 
                string strDel = "delete * from buttons where name='" + myButton.Name + "'"; 
                string strSQL = "insert into buttons(name,[text],[link],[lt],[tp]) values('" + nn + "','" + tt + "','" + qq + "','" + myButton.Left + "','" + myButton.Top + "')"; 
                OleDbCommand oleCmd = new OleDbCommand(strSQL, oleCon); 
                OleDbCommand oleDel = new OleDbCommand(strDel, oleCon); 
                oleCon.Open(); 
                try 
                { 
                    oleDel.ExecuteNonQuery(); 
                    oleCmd.ExecuteNonQuery(); 
                } 
                catch (Exception) 
                {} 
                string sg = "select * from buttons "; 
                OleDbDataAdapter adapter = new OleDbDataAdapter(sg, oleCon); 
                // 创建DataSet,用于存储数据. 
                DataSet testDataSet = new DataSet(); 
                // 执行查询,并将数据导入DataSet. 
                adapter.Fill(testDataSet, "buttons"); 
                oleCon.Close(); 
                oleCon.Dispose(); 
 
                foreach (DataRow testRow in testDataSet.Tables["buttons"].Rows) 
                { 
                    string pp; 
                    myButton.Name = testRow[0].ToString(); 
                    try 
                    { 
                        GetSystemIcon asd = new GetSystemIcon(); 
                        string yy = testRow[2].ToString(); 
                        Icon ty = asd.GetIconByFileName(@yy); 
                        myButton.BackgroundImage = ty.ToBitmap(); 
                        myButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; 
                    } 
                    catch (Exception) 
                    { 
                    } 
                    if (testRow[3].ToString() == "" || testRow[4].ToString() == "") 
                    { 
                        myButton.Top = myButton.Left = 0; 
                    } 
                    else 
                    { 
                        string f = testRow[3].ToString(); 
                        string g = testRow[4].ToString(); 
                        myButton.Left = Convert.ToInt32(f); 
                        myButton.Top = Convert.ToInt32(g); 
                    } 
                    pp = testRow[2].ToString(); 
                    ToolTip toolTip1 = new ToolTip(); 
                    toolTip1.AutoPopDelay = 1000; 
                    toolTip1.InitialDelay = 1000; 
                    toolTip1.ReshowDelay = 1500; 
                    toolTip1.ShowAlways = true; 
                    toolTip1.SetToolTip(myButton, myButton.Name); 
                } 
            } 
        } 
        void mButton_Click(object sender, EventArgs e) 
        { 
 
            Button btn = (Button)sender; 
             
            if (click == true) 
            { 
                string strPath = Application.StartupPath + "\\buttons.mdb"; 
                string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source='" + strPath + "'"; 
                OleDbConnection oleCon = new OleDbConnection(ConStr); 
                string we = "select link from buttons where name='" + btn.Name + "'"; 
                oleCon.Open(); 
                OleDbDataAdapter adapter = new OleDbDataAdapter(we, oleCon); 
                // 创建DataSet,用于存储数据. 
                DataSet testDataSet = new DataSet(); 
                // 执行查询,并将数据导入DataSet. 
                adapter.Fill(testDataSet, "buttons"); 
                oleCon.Close(); 
                oleCon.Dispose(); 
                string ww = testDataSet.Tables[0].Rows[0][0].ToString(); 
                Process p = new Process(); 
                p.StartInfo.FileName = ww; 
                p.Start(); 
                p.Close(); 
            } 
        } 
        void myButton_MouseUp(object sender, MouseEventArgs e) 
        { 
            click = true; 
            Button btn = (Button)sender; 
            string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source='" + strPath + "'"; 
            OleDbConnection oleCon = new OleDbConnection(ConStr); 
            string strSQL = "update buttons set lt='" + btn.Left.ToString() + "',tp='" + btn.Top.ToString() + "'where name='" + btn.Name + "'"; 
             
            OleDbCommand oleCmd = new OleDbCommand(strSQL, oleCon); 
            oleCon.Open(); 
            try 
            { 
                oleCmd.ExecuteNonQuery(); 
            } 
            catch (Exception) 
            { } 
            oleCon.Close(); 
            oleCon.Dispose(); 
        } 
 
        #region  实现按钮移动 
        bool click = true; 
        Point mouse_offset; 
        void myButton_MouseMove(object sender, MouseEventArgs e) 
        { 
            if (e.Button == MouseButtons.Left) 
            { 
                //将相对屏幕坐标转换为相对工作区的坐标。 
                int left = PointToClient(Control.MousePosition).X - mouse_offset.X; 
                int top = PointToClient(Control.MousePosition).Y - mouse_offset.Y; 
 
                //左右可能越界 
                if (left < 60) 
                    left = 35; 
                if (left > this.Width) 
                    left = this.Width - ((Button)sender).Width + 35; 
                //上下可能越界 
                if (top < 50) 
                    top = 10; 
                if (top < 111 && top > 50) 
                    top = 71; 
                if (top < 172 && top > 111) 
                    top = 132; 
                if (top < 233 && top > 172) 
                    top = 193; 
                if (top < 294 && top > 233) 
                    top = 254; 
                if (top < 355 && top > 294) 
                    top = 315; 
                if (top < 416 && top > 355) 
                    top = 376; 
                if (top > 416) 
                    top = 500; 
 
                if (left < 140 && left > 60) 
                    left = 105; 
                if (left < 220 && left > 140) 
                    left = 175; 
                if (left > 220) 
                    left = 245; 
                ((Button)sender).Left = left - 35; 
                ((Button)sender).Top = top - 10; 
                click = false; 
            } 
 
        } 
        #endregion  
 
        bool formMove = false; 
        Point formPoint; 
        private void Form1_MouseMove(object sender, MouseEventArgs e) 
        { 
            if (formMove == true) 
            { 
                Point mousePos = Control.MousePosition; 
                mousePos.Offset(formPoint.X, formPoint.Y); 
                Location = mousePos; 
            } 
        } 
 
        private void Form1_MouseDown(object sender, MouseEventArgs e) 
        { 
            formPoint = new Point(); 
            int xOffset; 
            int yOffset; 
            if (e.Button == MouseButtons.Left) 
            { 
                xOffset = -e.X-3; 
                yOffset = -e.Y-26; 
                formPoint = new Point(xOffset, yOffset); 
                formMove = true;//开始移动 
            } 
        } 
 
        private void Form1_MouseUp(object sender, MouseEventArgs e) 
        { 
            if (e.Button == MouseButtons.Left)//按下的是鼠标左键 
            { 
                formMove = false;//停止移动 
            } 
        } 
 
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
        } 
 
        private void 开机自动运行ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN").SetValue("MyAngel", Application.StartupPath + "\\宝友桌面.exe", RegistryValueKind.String);//开机启动 
 
        } 
 
        private void 取消自动运行ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN").SetValue("MyAngel", Application.StartupPath + "", RegistryValueKind.String);//开机启动 
 
        } 
 
        private void 更换皮肤ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            OpenFileDialog openFileDialog = new OpenFileDialog(); 
            openFileDialog.InitialDirectory = "c:\\"; 
            openFileDialog.Filter = "所有图像文件|*.bmp;*.png;*.jpg;*.gif;"; 
            openFileDialog.Title = "打开图像文件"; 
            openFileDialog.ShowHelp = true; 
            if (openFileDialog.ShowDialog() == DialogResult.OK) 
            { 
                string fn = openFileDialog.FileName; 
                this.BackgroundImage = Image.FromFile(fn); 
            } 
        } 
    } 
}