正文
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*$");
結束
實際開發中遠不隻這麼多,基本上也是有一個這樣的功能需求就去GOOGLE一段代碼出來,還有其他如delegate和event在窗體之間使用,分屏也是簡單的采用了4個Panel,當放大時就分别隐藏其他Panel等等,就不一一列舉了,多查查資料就行了: )
本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/586627,如需轉載請自行聯系原作者