程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#視頻監控系列(15):總結貼——可能用到的C# WinForm技術小結

C#視頻監控系列(15):總結貼——可能用到的C# WinForm技術小結

編輯:關於C#

正文

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, 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;
}

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等等,就不一一列舉了,多查查資料就行了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved