using System.Runtime.InteropServices;命名空间
1.获取控件滑化的距离
[DllImport("user32")]
public static extern int GetScrollPos(int hwnd, int nBar);
如要得到listView(自动产生滚动条)滑块的距离
int pos = GetScrollPos(ListView1.Handle.ToInt32(),0);
2.在指定一个控件上加载一个空间
[DllImport("user32")]
private static extern int SetParent(System.IntPtr hWndChild, System.IntPtr hWndNewParent);
如在已知面板上加一个window
IntPtr parentPtr = this.Panel1.Handle;
panel的对象句柄
IntPtr childPtr = Window.Handle;
SetParent(childPtr,parentPtr);
3.加载变化的cursor,改变图标
[DllImport("user32", EntryPoint="SetClassLong")]//EntryPoint="SetClassLong" 指明它的原始方法,表可以换名称
public static extern long SetClassLongA(int hwnd, int nIndex,IntPtr dwNewLong);
/// <summary>
/// FLITER THE WINDOW MESSAGE AND RESIZE THE GISWINDOW
/// </summary>
/// <param name="m"></param>
protected override void WndProc( ref Message m )
{
const int WM_EXITSIZE = 0x232; //AFTER USER RESIZE THE GISWINDOW AND MOUSE UP
const int WM_SIZING = 0x214; //USER RESIZING THE GISWINDOW
const int WM_SIZE = 0x5;
const int SIZE_MAXIMIZED = 2;
const int SIZE_RESTORED = 0;
const int WM_NCLBUTTONDOWN = 161;
const int WM_SYSCOMMAND = 274;
const int HTCAPTION = 2;
const int SC_MOVE = 61456;
const int WM_SETCURSOR = 0x20;
const int HTCLIENT = 0x2000001; //Spy++ get this value
//for control mouse down do not action
if(m.Msg == WM_SETCURSOR )
{
//mouse moving on client(panel control)
//鼠标进入panel控件,才让panel中
if(m.LParam.ToInt32() == HTCLIENT)
{
//该变量与Activated事件关联,只要鼠标
bMapMouseMove = true;
}
else
{
//mouse move out of axMap
bNeedResetMapMousePointer = true;
bMapMouseMove = false;
}
}
if ( (m.Msg == WM_SYSCOMMAND) &&
(m.WParam.ToInt32() == SC_MOVE) &&
(bMaximized == true) )
{
return;
}
if ( (m.Msg == WM_NCLBUTTONDOWN) &&
(m.WParam.ToInt32() == HTCAPTION) &&
(bMaximized == true) )
{
this.Focus();
return;
}
base.WndProc( ref m );
}
点击最小化按钮让窗体变成隐藏的功能,在网上搜索了半天才找到一段代码,贴在这里,大家共享吧

const int WM_SYSCOMMAND = 0x112;
const int SC_CLOSE = 0xF060;
const int SC_MINIMIZE = 0xF020;
const int SC_MAXIMIZE = 0xF030;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == SC_MINIMIZE)
{
this.Visible = false;
return;
}
}
base.WndProc(ref m);
}
5.GetTempFileName
创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern uint GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName);
第一个参数是希望创建新文件的驱动器和/或路径的名称,在下面 的样例程序中,新文件创建在驱动器C的根目录下。
第二个参数是指派给文件名称的前缀。如果将该前缀指定为"TES T",则函数将把创建的新文件名称的前四个字符设置为"TEST",即"TES T*.TMP"。
第三个参数应该被设置为0,告诉该函数为文件名称自动产生随机 号码。该随机号码会被添加到前缀字符的后面,以产生一个独特而且 完整的文件名称。
第四个参数是一个字符串缓存,它至少要有256个字符的长度,以 存放临时文件的名称。
在调用该函数之后,新文件创建在指定的磁盘上。这里重申一下, 当退出应用程序时,必须物理地将该文件从磁盘上删除。
1.获取控件滑化的距离
[DllImport("user32")]
public static extern int GetScrollPos(int hwnd, int nBar);
如要得到listView(自动产生滚动条)滑块的距离
int pos = GetScrollPos(ListView1.Handle.ToInt32(),0);
2.在指定一个控件上加载一个空间
[DllImport("user32")]
private static extern int SetParent(System.IntPtr hWndChild, System.IntPtr hWndNewParent);
如在已知面板上加一个window
IntPtr parentPtr = this.Panel1.Handle;
panel的对象句柄
IntPtr childPtr = Window.Handle;
SetParent(childPtr,parentPtr);
3.加载变化的cursor,改变图标
[DllImport("user32", EntryPoint="SetClassLong")]//EntryPoint="SetClassLong" 指明它的原始方法,表可以换名称
public static extern long SetClassLongA(int hwnd, int nIndex,IntPtr dwNewLong);
public long LoadCursorByMapTool(MapToolEnum tool)
{
//resource path.
//图标存放位置在Cursor文件下,要设该图片Build Action属性为内欠资源
string resourceFile = "Cursor.";
switch(tool)
{
case MapToolEnum.BinCell:
resourceFile += "BinCellCvg.cur";
break;
case MapToolEnum.Blank:
resourceFile += "Accurate2.cur";
break;
}
//反射
System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream(resourceFile);
Cursor cur = new Cursor(stream);
//鼠标进入mapWindow窗体上panel1,就改变鼠标状态
long ret = SetClassLongA(mapWindow.panel1.Handle,-12,cur.Handle);
return ret;
}


4.WndProc函数是消息处理函数,一旦有消息产生,就会调用该函数
{
//resource path.
//图标存放位置在Cursor文件下,要设该图片Build Action属性为内欠资源
string resourceFile = "Cursor.";
switch(tool)
{
case MapToolEnum.BinCell:
resourceFile += "BinCellCvg.cur";
break;
case MapToolEnum.Blank:
resourceFile += "Accurate2.cur";
break;
}
//反射 System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream(resourceFile);
Cursor cur = new Cursor(stream);//鼠标进入mapWindow窗体上panel1,就改变鼠标状态
long ret = SetClassLongA(mapWindow.panel1.Handle,-12,cur.Handle);
return ret;
}

/// <summary>
/// FLITER THE WINDOW MESSAGE AND RESIZE THE GISWINDOW
/// </summary>
/// <param name="m"></param>
protected override void WndProc( ref Message m )
{
const int WM_EXITSIZE = 0x232; //AFTER USER RESIZE THE GISWINDOW AND MOUSE UP
const int WM_SIZING = 0x214; //USER RESIZING THE GISWINDOW
const int WM_SIZE = 0x5;
const int SIZE_MAXIMIZED = 2;
const int SIZE_RESTORED = 0;
const int WM_NCLBUTTONDOWN = 161;
const int WM_SYSCOMMAND = 274;
const int HTCAPTION = 2;
const int SC_MOVE = 61456;
const int WM_SETCURSOR = 0x20;
const int HTCLIENT = 0x2000001; //Spy++ get this value
//for control mouse down do not action
if(m.Msg == WM_SETCURSOR )
{
//mouse moving on client(panel control)//鼠标进入panel控件,才让panel中
if(m.LParam.ToInt32() == HTCLIENT)
{ //该变量与Activated事件关联,只要鼠标
bMapMouseMove = true;
}
else
{
//mouse move out of axMap
bNeedResetMapMousePointer = true;
bMapMouseMove = false;
}
}
if ( (m.Msg == WM_SYSCOMMAND) &&
(m.WParam.ToInt32() == SC_MOVE) &&
(bMaximized == true) )
{
return;
}
if ( (m.Msg == WM_NCLBUTTONDOWN) &&
(m.WParam.ToInt32() == HTCAPTION) &&
(bMaximized == true) )
{
this.Focus();
return;
}
base.WndProc( ref m );
}
点击最小化按钮让窗体变成隐藏的功能,在网上搜索了半天才找到一段代码,贴在这里,大家共享吧
const int WM_SYSCOMMAND = 0x112;
const int SC_CLOSE = 0xF060;
const int SC_MINIMIZE = 0xF020;
const int SC_MAXIMIZE = 0xF030;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == SC_MINIMIZE)
{
this.Visible = false;
return;
}
}
base.WndProc(ref m);
}[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern uint GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName);
第一个参数是希望创建新文件的驱动器和/或路径的名称,在下面 的样例程序中,新文件创建在驱动器C的根目录下。
第二个参数是指派给文件名称的前缀。如果将该前缀指定为"TES T",则函数将把创建的新文件名称的前四个字符设置为"TEST",即"TES T*.TMP"。
第三个参数应该被设置为0,告诉该函数为文件名称自动产生随机 号码。该随机号码会被添加到前缀字符的后面,以产生一个独特而且 完整的文件名称。
第四个参数是一个字符串缓存,它至少要有256个字符的长度,以 存放临时文件的名称。
在调用该函数之后,新文件创建在指定的磁盘上。这里重申一下, 当退出应用程序时,必须物理地将该文件从磁盘上删除。

浙公网安备 33010602011771号