程序开发点滴

本文介绍了程序开发中的两个实用技巧:一是使用Interop.SpeechLib进行语音合成,包括添加dll引用、设置发音速度;二是实现窗体在任意位置拖动的功能,通过ReleaseCapture和SendMessage实现拖动,并展示了两种不同的实现方式。
摘要由CSDN通过智能技术生成

一、语音程序

添加应用 Interop.SpeechLib.dll

下载地址:http://download.csdn.net/detail/jsjyyjs07/4398579

using SpeechLib;

        /// <summary>  
        /// 发音  
        /// </summary>  
        /// <param name="content"></param>  
        void read(object content)  
        {  
            try 
            {  
                backgroundWorker1.RunWorkerAsync(content);  
            }  
            catch (Exception er)  
            {
                MessageBox.Show(er.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);  
            }  
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpVoice voice = new SpVoice();
            voice.Rate = 0;//调整发音语速,可以为负数,如-3,0,5
            voice.Speak(e.Argument.ToString(), spFlags);
        }

二、在窗体的任意位置拖动窗体

实例一:

private int LEFT = 5, RIGHT = 5, BOTTOM = 5, TOP = 5, TITLE_WIDTH = 45;//边框和标题栏的大小
        private MouseAction mouse;//鼠标的事件响应对象
        private int x = 0, y = 0;//保存鼠标的临时坐标

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Form2_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            x = e.X;
            y = e.Y;
            //this.Parent.Cursor = CheckCursorType(e.X, e.Y);//改变鼠标的指针形状
            if (mouse != null)
            {
                mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//执行时间响应
                //注意坐标是Control.MousePosition这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
            }

            //鼠标点击左上边框
            if ((e.X <= LEFT + 10 && e.Y <= TOP) || (e.Y <= TOP + 10 && e.X <= LEFT))
            {
                //mouse = new MouseSizeTopLeft(Location.X, Location.Y, Width, Height);
                return;
            }
        }

        private void DrawMiddle_Left(Graphics g)
        {
            //Brush brush = new TextureBrush(Middle_Left, new Rectangle(0, 0,Middle_Left.Width, Middle_Left.Height));
            //g.FillRectangle(brush, 0, TITLE_WIDTH, Middle_Left.Width,Height - Bottom_Middle.Height - TITLE_WIDTH);
        }


        //定义了一个抽象的基类MouseAction,用来表示所有的鼠标事件,它有一个抽象方法Action
        public abstract class MouseAction
        {
            public abstract void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form);
        }
        //向右拉伸窗口事件
        public class MouseSizeRight : MouseAction
        {
            private int lx;
            public MouseSizeRight(int LocationX)
            {
                lx = LocationX;
            }

            public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
            {
                form.Width = ScreenX - lx;
                form.Invalidate();
            }
        }
        //鼠标拖动
        public class MouseDrag : MouseAction
        {
            private int x, y;
            public MouseDrag(int hitX, int hitY)
            {
                x = hitX;
                y = hitY;
            }

            public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
            {
                form.Location = new Point(ScreenX - x, ScreenY - y);
            }
        }

示例二:

        public static Point CPoint;
        bool down = false;

        private void Form3_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(e.X, e.Y);
            down = true;
        }

        private void Form3_MouseMove(object sender, MouseEventArgs e)
        {
            if (down == true)
            {
                if (e.Button == MouseButtons.Left)
                {
                    Point mousePosition = Control.MousePosition;
                    mousePosition.Offset(-CPoint.X, -CPoint.Y);
                    this.DesktopLocation = mousePosition;
                }
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值