正文
1. IntPtr转换成byte[]
public
byte
[] ConvertToBytes(IntPtr dataBuf,
int
length)
{
byte [] byteBuf = new byte [length];
Marshal.Copy(dataBuf, byteBuf, 0 , length);
return byteBuf;
}
{
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, 5000 , this .Path);
return temp.ToString();
}
}
/// 读写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, 5000 , this .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;
}
/// 获得网卡号
/// </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();
}
/// 获取板卡序号
/// </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 );
}
}
/// 全屏
/// </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*$ " );
}
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等等,就不一一列举了,多查查资料就行了: )
本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/586627,如需转载请自行联系原作者