------- <a href="http://www. target="blank">Windows Phone 7手机开发</a>、<a href=http://www. target="blank">.Net培训</a>、期待与您交流! -------
1. HandleRef
包装一个托管对象,该对象保持使用平台invoke(调用)传递给非托管代码的资源句柄。如果使用平台调用来调用托管对象,而且该对象在该调用之后不在其他地方引用,则垃圾回收器有可能终结该托管对象。 此操作将释放资源并使句柄无效,从而导致平台invoke调用失败。使用HandleRef包装句柄,可保证该托管对象在平台invoke调用完成前不被垃圾回收。
2. 在FormBorderStyle.None时调出任务栏右键
[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]public static extern int GetWindowLong(HandleRef hWnd, int nIndex);[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);int WS_SYSMENU = 0x00080000; // 系统菜单int WS_MINIMIZEBOX = 0x20000; // 最大最小化按钮int windowLong = GetWindowLong(new HandleRef(this, this.Handle), -16);SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_MINIMIZEBOX | WS_SYSMENU);
3. 不允许窗体被拖动
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if(m.Msg==0x84)
{
If((IntPtr)2==m.Result)
{
m.Result=(IntPtr)1;
}
}
}
4. 拖动窗体或者控件
[DllImport("user32.dll")]public static extern bool ReleaseCapture();[DllImport("user32.dll")]public static extern bool SendMessage(IntPtr hwnd,int wParam,int lParam);
public const int WM_SYSCOMMAND=0x0112;
public const int SC_MOVE=0xF010;
public const int HTCAPTION=0x0002;
private void form_MouseDown(object sender,MouseEeventArgs e)
{
ReleaseCapture();
SendMessage(handle, WM_SYSCOMMAND, SC_MOVE+ HTCAPTION,0);
}