• 可能用到的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;

            
    /// <summary>
            
    /// 声明读写INI文件的API函数 
            
    /// </summary>
            
    /// <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);


            [DllImport(
    "kernel32")]
            
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

            
    /// <summary>
            
    /// 类的构造函数,传递INI文件名
            
    /// </summary>
            
    /// <param name="inipath"></param>
            public IniFile(string inipath)
            {
                
    //
                
    // TODO: Add constructor logic here
                
    //
                Path = inipath;
            }

            
    /// <summary>
            
    /// 写INI文件
            
    /// </summary>
            
    /// <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);

            }

            
    /// <summary>
            
    /// 读取INI文件指定
            
    /// </summary>
            
    /// <param name="Section"></param>
            
    /// <param name="Key"></param>
            
    /// <returns></returns>
            public string IniReadValue(string Section, string Key)
            {
                StringBuilder temp 
    = new StringBuilder(5000);
                
    int i = GetPrivateProfileString(Section, Key, "", temp, 5000this.Path);
                
    return temp.ToString();

            }
        }
    复制代码

         3.     获取网卡号

              可用于软件加密

    复制代码
            /// <summary>
            
    /// 获得网卡号
            
    /// </summary>
            
    /// <returns></returns>
            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。

    复制代码
            /// <summary>
            
    /// 获取板卡序号
            
    /// </summary>
            
    /// <returns></returns>
            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.     全屏显示视频窗口

    复制代码
            /// <summary>
            
    /// 全屏
            
    /// </summary>
            
    /// <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?)$");
            }
            
    /// <summary>
            
    /// 是否是无符号整数(正数)
            
    /// </summary>
            
    /// <param name="value"></param>
            
    /// <returns></returns>
            public bool IsUInt(string value)
            {
                
    return Regex.IsMatch(value, @"^\d*$");
            }
    复制代码

    结束

         实际开发中远不只这么多,基本上也是有一个这样的功能需求就去GOOGLE一段代码出来,还有其他如delegate和event在窗体之间使用,分屏也是简单的采用了4个Panel,当放大时就分别隐藏其他Panel等等,就不一一列举了,多查查资料就行了: )    

  • 相关阅读:
    Asp.Net MVC3.0中防止跨站的POST
    .NET实现字符串base64编码
    silverlight调用MVC WebApi方法
    MVC实现上传图片的方法
    ASP.NET使用文件上传控件上传图片
    设置网页icon标志
    C# 连接EXCEL 和 ACCESS
    javascript前进、后退、刷新的实现
    DataTable数据统计方法
    常量指针和指针常量
  • 原文地址:https://www.cnblogs.com/ywsoftware/p/2892554.html
Copyright © 2020-2023  润新知