前言
本章主要把可能用到的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="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?)$");
/// 是否是無符号整數(正數)
public bool isuint(string value)
return regex.ismatch(value, @"^\d*$");

結束
實際開發中遠不隻這麼多,基本上也是有一個這樣的功能需求就去google一段代碼出來,還有其他如delegate和event在窗體之間使用,分屏也是簡單的采用了4個panel,當放大時就分别隐藏其他panel等等,就不一一列舉了,多查查資料就行了: )
轉載:http://www.cnblogs.com/over140/archive/2009/04/04/1429298.html