www.pudn.com > GoogleMini.zip > Form1.cs, change:2012-05-22,size:14095b


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; 
 
namespace GoogleMini 
{ 
    public partial class GoogleMini : Form 
    { 
        public GoogleMini() 
        { 
            InitializeComponent(); 
        } 
 
        #region  公共变量 
        IntPtr Tem_Handle;//获取控件及窗体的句柄 
        Point CPoint;//获取控件中鼠标的坐标 
        int Tem_Y = 0; 
        #endregion 
 
        #region  API声明 
        //获取当前鼠标下可视化控件的句柄 
        [DllImport("user32.dll")] 
        public static extern int WindowFromPoint(int xPoint, int yPoint); 
        //获取指定句柄的父级句柄 
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] 
        public static extern IntPtr GetParent(IntPtr hWnd); 
        //获取屏幕的大小 
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")] 
        private static extern int GetSystemMetrics(int mVal); 
        #endregion 
 
        private void panel1_MouseDown(object sender, MouseEventArgs e) 
        { 
            timer1.Enabled = false; 
            CPoint = new Point(-e.X, -e.Y); 
        } 
 
        private void panel1_MouseMove(object sender, MouseEventArgs e) 
        { 
            FrmMove(this, e); 
        } 
 
        private void panel1_MouseUp(object sender, MouseEventArgs e) 
        { 
            timer1.Enabled = true; 
        } 
 
        #region  利用窗体上的控件移动窗体 
        /// <summary> 
        /// 利用控件移动窗体 
        /// </summary> 
        /// <param Frm="Form">窗体</param> 
        /// <param e="MouseEventArgs">控件的移动事件</param> 
        public void FrmMove(Form Frm, MouseEventArgs e)  //Form或MouseEventArgs添加命名空间using System.Windows.Forms; 
        { 
            if (e.Button == MouseButtons.Left) 
            { 
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标 
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置 
                Frm.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置 
            } 
        } 
        #endregion 
 
        #region  获取当前鼠标下可视化控件的句柄 
        /// <summary> 
        /// 获取当前鼠标下可视化控件的句柄 
        /// </summary> 
        /// <param x="int">当前鼠标的X坐标</param> 
        /// <param y="int">当前鼠标的Y坐标</param> 
        public IntPtr FormNameAt(int x, int y) 
        { 
            IntPtr Tem_hWnd;//设置存储句柄的变量 
            Tem_Handle = (IntPtr)(WindowFromPoint(x, y));//获取当前鼠标下可视化控件的句柄 
            Tem_hWnd = Tem_Handle;//记录原始句柄 
            while (Tem_hWnd != ((IntPtr)0))//遍历该句柄的父级句柄 
            { 
                Tem_Handle = Tem_hWnd;//记录当前句柄 
                Tem_hWnd = GetParent(Tem_hWnd);//获取父级句柄 
            } 
            return Tem_Handle;//返回最底层的父级句柄 
        } 
        #endregion 
 
        private void timer1_Tick(object sender, EventArgs e) 
        { 
            if (this.Top < 3)//如果窗体被移到屏幕的顶部 
            { 
                if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上 
                { 
                    panel1.Tag = 1;//设置标识,用于判断窗体在屏幕顶部 
                    timer2.Enabled = false;//不对窗体进行拉伸操作 
                    this.Top = 0;//使窗体致顶 
                } 
                else 
                { 
                    panel1.Tag = 1;//设置标识,用于判断窗体在屏幕顶部 
                    timer2.Enabled = true;//将窗体在顶部进行隐藏 
                } 
            } 
            else 
            { 
                if (this.Left < 3 || this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的左端或右端 
                { 
                    if (this.Left < 3)//如果窗体被移到屏幕的左端 
                    { 
                        if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上 
                        { 
                            panel1.Tag = 2;//设置标识,用于判断窗体在屏幕左端 
                            timer2.Enabled = false; 
                            this.Left = 0;//使窗体致左 
                        } 
                        else 
                        { 
                            panel1.Tag = 2; 
                            timer2.Enabled = true;//将窗体在左端进行隐藏 
                        } 
                    } 
                    if (this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的右端 
                    { 
                        if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上 
                        { 
                            panel1.Tag = 3;//设置标识,用于判断窗体在屏幕右端 
                            timer2.Enabled = false; 
                            this.Left = GetSystemMetrics(0) - this.Width;//使窗体致右 
                        } 
                        else 
                        { 
                            panel1.Tag = 3; 
                            timer2.Enabled = true;//将窗体在右端进行隐藏 
                        } 
                    } 
 
                } 
            } 
        } 
 
        private void timer2_Tick(object sender, EventArgs e) 
        { 
            switch (Convert.ToInt16(panel1.Tag.ToString()))//对标识进行判断 
            { 
                case 1://顶端隐藏 
                    { 
                        if (this.Top < 5) 
                            this.Top = -(this.Height - 2); 
                        break; 
                    } 
                case 2://左端隐藏 
                    { 
                        if (this.Left < 5) 
                            this.Left = -(this.Width - 2); 
                        break; 
                    } 
                case 3://右端隐藏 
                    { 
                        if ((this.Left + this.Width) > (GetSystemMetrics(0) - 5)) 
                            this.Left = GetSystemMetrics(0) - 2; 
                        break; 
                    } 
            } 
        } 
 
        private void Form1_Resize(object sender, EventArgs e) 
        { 
            if (this.WindowState == FormWindowState.Minimized) 
            { 
                NormalToMinimized(); 
            } 
        } 
 
        #region method 
 
        //恢复窗体 
        private void MinimizedToNormal() 
        { 
            SavePoint(); 
            this.Visible = true; 
            this.WindowState = FormWindowState.Normal; 
        } 
        //最小化至托盘 
        private void NormalToMinimized() 
        { 
            this.Visible = false; 
            this.WindowState = FormWindowState.Minimized; 
        } 
 
 
        //保存当前坐标 
        private void SavePoint() 
        { 
            int x = this.Location.X; 
            int y = this.Location.Y; 
            this.Location = new Point(x, y); 
        } 
        #endregion 
 
        private void lblClose_Click(object sender, EventArgs e) 
        { 
            if (MessageBox.Show("您确定要退出GoogleMini吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) 
            { 
                this.notifyIcon1.Visible = false; 
                this.Close(); 
                this.Dispose(); 
                Application.Exit(); 
            } 
            else 
            { 
                return; 
            } 
        } 
 
        private void GoogleMini_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            //SavePoint(); 
            //timer1.Enabled = false; 
            e.Cancel = true; 
            NormalToMinimized(); 
        } 
 
        private void 关于本软件ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            AboutBox1 ab = new AboutBox1(); 
            ab.Show(); 
        } 
 
        private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            if (MessageBox.Show("您确定要退出GoogleMini吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) 
            { 
                this.notifyIcon1.Visible = false; 
                this.Close(); 
                this.Dispose(); 
                Application.Exit(); 
            } 
            else 
            { 
                return; 
            } 
        } 
 
        private void lblAbout_Click(object sender, EventArgs e) 
        { 
            AboutBox1 ab = new AboutBox1(); 
            ab.Show(); 
        } 
 
        private void btnSearch_Click(object sender, EventArgs e) 
        { 
            //网页搜索 
            if (this.rbtWeb.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://www.google.com.hk/webhp?hl=zh-CN"); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start(" http://www.google.com.hk/webhp?hl=zh-CN&q&hl=zh-CN&tab=ew#hl=zh-CN&newwindow=1&safe=strict&site=webhp&source=hp&q=" + this.textBox1.Text + ""); 
                } 
            } 
            //新闻搜索 
            else if (this.rbtNews.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://news.google.com.hk/news?ned=cn"); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start(" http://www.google.com.hk/webhp?hl=zh-CN&gl=cn&tbm=nws&q=" + this.textBox1.Text + ""); 
                } 
            } 
            //谷歌地图 
            else if (this.rbtMap.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://ditu.google.cn/maps?hl=zh-CN&tab=Tl&q="); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start("http://ditu.google.cn/maps?hl=zh-CN&tab=Tl&q=" + this.textBox1.Text + ""); 
                } 
            } 
            //视频搜索 
            else if (this.rbtVideo.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://www.google.com.hk/search?tbm=vid&hl=zh-CN&source=hp&biw=1174&bih=645&q="); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start("http://www.google.com.hk/search?tbm=vid&hl=zh-CN&source=hp&biw=1174&bih=645&q=" + this.textBox1.Text + ""); 
                } 
            } 
            //图片搜索 
            else if (this.rbtPic.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://images.google.com.hk/imghp?hl=zh-CN"); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start("http://www.google.com.hk/search?hl=zh-CN&tbm=isch&source=og&tab=ni&q=" + this.textBox1.Text + ""); 
                } 
            } 
            //英译汉 
            else if (this.rbtTranEN.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://translate.google.com.hk/#en|zh-CN|"); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start("http://translate.google.com.hk/#en|zh-CN|" + this.textBox1.Text + ""); 
                } 
            } 
            //汉译英 
            else if (this.rbtTranCH.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://translate.google.com.hk/#zh-CN|en|"); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start("http://translate.google.com.hk/#zh-CN|en|" + this.textBox1.Text + ""); 
                } 
            } 
            //图书搜索 
            else if (this.rbtBook.Checked == true) 
            { 
                if (this.textBox1.Text == null || this.textBox1.Text == "") 
                { 
                    System.Diagnostics.Process.Start("http://books.google.com.hk/books?hl=zh-CN"); 
                } 
                else 
                { 
                    System.Diagnostics.Process.Start("http://www.google.com.hk/search?tbm=bks&hl=zh-CN&q=" + this.textBox1.Text + ""); 
                } 
            } 
        } 
 
        private void lblMin_Click(object sender, EventArgs e) 
        { 
            NormalToMinimized(); 
        } 
 
        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            MinimizedToNormal(); 
        } 
 
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 
        { 
            MinimizedToNormal(); 
        } 
        private void GoogleMini_Load(object sender, EventArgs e) 
        { 
        } 
    } 
}