天天看点

C# 视频监控系列(15):总结贴——可能用到的C# WinForm技术小结

 1.     IntPtr转换成byte[]

        public byte[] ConvertToBytes(IntPtr dataBuf, int length)

        {

            byte[] byteBuf = new byte[length];

            Marshal.Copy(dataBuf, byteBuf, 0, length);

            return byteBuf;

        }

2.     读写INI文件

          一般用于读写配置文件

    /// <summary>

    /// 读写INI文件

    /// </summary>

    public class IniFile

    {

        /// <summary>

        /// 文件INI名称

        /// </summary>

        public string Path;

        /// 声明读写INI文件的API函数 

        /// <param name="section"></param>

        /// <param name="key"></param>

        /// <param name="val"></param>

        /// <param name="filePath"></param>

        /// <returns></returns>

        [DllImport("kernel32")]

        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// 类的构造函数,传递INI文件名

        /// <param name="inipath"></param>

        public IniFile(string inipath)

            //

            // TODO: Add constructor logic here

            Path = inipath;

        /// 写INI文件

        /// <param name="Section"></param>

        /// <param name="Key"></param>

        /// <param name="Value"></param>

        public void IniWriteValue(string Section, string Key, string Value)

            WritePrivateProfileString(Section, Key, Value, this.Path);

        /// 读取INI文件指定

        public string IniReadValue(string Section, string Key)

            StringBuilder temp = new StringBuilder(5000);

            int i = GetPrivateProfileString(Section, Key, "", temp, 5000, this.Path);

            return temp.ToString();

    }

3.     获取网卡号

          可用于软件加密

        /// 获得网卡号

        public static string GetNetCardMacAddress()

            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

            ManagementObjectCollection moc = mc.GetInstances();

            string str = "";

            foreach (ManagementObject mo in moc)

            {

                if ((bool)mo["IPEnabled"] == true)

                    str = mo["MacAddress"].ToString();

            }

            return str;

4.     获取采集卡序列化

          可用于软件加密绑定,注意结构体DS_BOARD_DETAIL.sn是16位的,但是实际只有12位,VC++源码也只取了12位,后门都是0。

        /// 获取板卡序号

        public static string GetBoardSN()

            uint boardCount = HikVisionSDK.GetBoardCount();

            StringBuilder sn = new StringBuilder();

            for (uint i = 0; i < boardCount; )

                DS_BOARD_DETAIL boardDetail = new DS_BOARD_DETAIL();

                HikVisionSDK.GetBoardDetail(i, ref boardDetail);

                for (int j = 0; j < 12; j++)

                {

                    sn.Append((char)(boardDetail.sn[j] + 0x30));

                }

                break;

            return sn.ToString();

5.     全屏显示视频窗口

        /// 全屏

        /// <param name="tsmFullScreen">右键</param>

        /// <param name="pVideo">显示视频的窗口</param>

        /// <param name="cs">视频连接成功返回的值</param>

        private void FullScreen(ToolStripMenuItem tsmFullScreen, Panel pVideo, int cs)

            if (this.WindowState == System.Windows.Forms.FormWindowState.Maximized)

                tsmFullScreen.Text = "全屏窗口";

                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

                this.WindowState = System.Windows.Forms.FormWindowState.Normal;

                this.TopMost = false;

                pVideo.Width = pre_Width;

                pVideo.Height = pre_Height;

                pVideo.Top = pre_Top;

                pVideo.Left = pre_Left;

                //隐藏其他控件

                HideOtherControls(pVideo, true);

                minPanel(pVideo, cs);

            else

                tsmFullScreen.Text = "关闭全屏";

                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

                this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

                this.TopMost = true;

                pre_Width = pVideo.Width;

                pre_Height = pVideo.Height;

                pre_Top = pVideo.Top;

                pre_Left = pVideo.Left;

                pVideo.Width = this.Width;

                pVideo.Height = this.Height;

                pVideo.Top = 0;

                pVideo.Left = 0;

                //显示其他控件

                HideOtherControls(pVideo, false);

6.     其他代码

        //判断是不是IP地址

        public bool IsIPAddress(string ip)

            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");

        /// 是否是无符号整数(正数)

        /// <param name="value"></param>

        public bool IsUInt(string value)

            return Regex.IsMatch(value, @"^\d*$");

继续阅读