前言
本章主要把可能用到的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