月度归档: 2018 年 4 月

  • C# 使用TTS播放声音

    C# 使用Windows系统自带的TTS播放声音,以下代码基于.NET Framework4.0

    // 定义成全局的,因为每次创建实例需要一定的时间,会造成播放延迟
    private dynamic spVoice = Activator.CreateInstance(Type.GetTypeFromProgID("SAPI.SpVoice"));
    
    //调用的时候
    this.spVoice.Speak("你好,我是TTS合成语音");
  • C# WinForm设置dataGridView表头字体大小

    C# WinForm中,设置dataGridView控件表头字体的大小:

    var font = new Font(dataGridView1.ColumnHeadersDefaultCellStyle.Font.FontFamily, dataGridView1.ColumnHeadersDefaultCellStyle.Font.Size + 8);
    dataGridView1.ColumnHeadersDefaultCellStyle.Font = font;

    示例

    // 构造函数
    public HomeForm()
    {
            InitializeComponent();
            //奇数行的单元格的背景色为黄绿色
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
            var font = new Font(dataGridView1.ColumnHeadersDefaultCellStyle.Font.FontFamily, dataGridView1.ColumnHeadersDefaultCellStyle.Font.Size + 8);
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = font;
            BindCombox();
            BindDataGrid();
            dataGridView1.Columns[0].Width = 300;
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
    }
  • C# WinForm 自动关闭 定时关闭 MessageBox

    C# WinForm 的开发中,有时需要一个可以自动关闭的弹出框,下面的代码是自动关闭或定时关闭的一个MessageBox的实现:

    public class AutoClosingMessageBox
    {
        System.Threading.Timer _timeoutTimer;
        string _caption;
        DialogResult _result;
        DialogResult _timerResult;
        AutoClosingMessageBox(string text, string caption, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, DialogResult timerResult = DialogResult.None)
        {
            _caption = caption;
            _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
                null, timeout, System.Threading.Timeout.Infinite);
            _timerResult = timerResult;
            using (_timeoutTimer)
                _result = MessageBox.Show(text, caption, buttons);
        }
        public static DialogResult Show(string text, string caption, int timeout, MessageBoxButtons buttons = MessageBoxButtons.OK, DialogResult timerResult = DialogResult.None)
        {
            return new AutoClosingMessageBox(text, caption, timeout, buttons, timerResult)._result;
        }
        void OnTimerElapsed(object state)
        {
            IntPtr mbWnd = FindWindow("#32770", _caption); // lpClassName is #32770 for MessageBox
            if (mbWnd != IntPtr.Zero)
                SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            _timeoutTimer.Dispose();
            _result = _timerResult;
        }
        const int WM_CLOSE = 0x0010;
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
    }
    

    用法:

    AutoClosingMessageBox.Show("Text", "Caption", 1000);

    返回值 

    var userResult = AutoClosingMessageBox.Show("Yes or No?", "Caption", 1000, MessageBoxButtons.YesNo);if(userResult == System.Windows.Forms.DialogResult.Yes) {
    // do something}

    也可以通过Nuget安装这个包

    Install-Package AutoClosingMessageBox
  • ASP.NET WebForm 根据请求显示 不同的页面(类似PHP的include)

    ASP.NET WebForm 根据请求显示 不同的页面(类似PHP的include)

    protected void Page_Load(object sender, EventArgs e)
    {
        var area = Request.QueryString["id"].ToUpper();
        if (area == "Z")
        {
            StringWriter writer = new StringWriter();
            Server.Execute("z.aspx", writer);
            String html = writer.ToString();
            Response.Write(html);
        }
        else
        {
            StringWriter writer = new StringWriter();
            Server.Execute("area.aspx", writer);
            String html = writer.ToString();
            Response.Write(html);
        }
         
    }

  • PHP5.2 使用php_mssql.dll 连接sql server2000 sp4

    PHP5.2 使用php_mssql.dll 连接sql server2000 sp4的数据库连接串:

    $conn=mssql_connect("127.0.0.1:8829", "sa", "5628171" ) or die("连接数据库失败:" . mssql_get_last_message());

    注意端口是用冒号,而不是sql server的连接串 里面用的逗号

  • 无法打开登录所请求的数据库  ASPState 登录失败

    在经典asp.net的开发中,有时会遇到:无法打开登录所请求的数据库  ASPState 登录失败 的问题。

    解决办法: 在数据库服务器上打开 C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/InstallSqlState.sql

    在数据库管理工具中执行即可,有错误忽略即可。

  • apache自带的ab.exe压力测试工具

    apache自带的ab.exe压力测试工具的使用方法:

    ./ab -n 10000 -c 5000  http://192.168.86.129/

    -n 表示请求数

    -c  表示并发数

    注意:实际使用中要把http://192.168.86.129/换成目标网址。