我刚刚更新了我的 Moveable WinForm 解决方案:http ://www.hjgode.de/wp/2012/11/07/mobile-development-move-your-form/ 。
更新的源代码位于http://code.google.com/p/win-mobile-code/source/browse/#svn%2Ftrunk%2FMoveableWinForm%2FMovableForm
将 msg 发送到表单的基本子类代码是(参见 winapi.cs):
#region subclassing
public class subclassForm:IDisposable
{
#region delegate_event_stuff
public class wndprocEventArgs : EventArgs
{
public IntPtr hWnd;
public uint msg;
public IntPtr lParam;
public IntPtr wParam;
public wndprocEventArgs(IntPtr lphWnd, uint iMsg, IntPtr lpLParam, IntPtr lpWParam)
{
hWnd = lphWnd;
msg = iMsg;
lParam = lpLParam;
wParam = lpWParam;
}
}
public delegate void wndProcEventHandler(object sender, wndprocEventArgs wndProcArgs);
public event wndProcEventHandler wndProcEvent;
void onWndProcEvent(wndprocEventArgs wa)
{
if (this.wndProcEvent == null)
return;
wndProcEvent(this, wa);
}
#endregion
public enum WNDMSGS:uint{
WM_MOVE=0x0003,
WM_SIZE=0x0005,
}
public subclassForm(System.Windows.Forms.Form form)
{
_form = form;
lpPrevWndFunc = _subClassForm(_form);
}
public void Dispose()
{
unsubClassForm(_form);
}
IntPtr lpPrevWndFunc=IntPtr.Zero;
System.Windows.Forms.Form _form;
static WndProcDelegate persistentWndProc;
IntPtr _subClassForm(System.Windows.Forms.Form form)
{
//avoid multiple subclassing
if (lpPrevWndFunc != IntPtr.Zero)
return IntPtr.Zero;
persistentWndProc = WndProc;
lpPrevWndFunc = (IntPtr)GetWindowLong(form.Handle, GWL_WNDPROC);
SetWindowLong(form.Handle, GWL_WNDPROC, persistentWndProc);
return lpPrevWndFunc;
}
IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
System.Diagnostics.Debug.WriteLine("HWND: " + hWnd + " MSG: " + msg + " WPARAM: " + wParam + " LPARAM: " + lParam);
onWndProcEvent(new wndprocEventArgs(hWnd, msg, lParam, wParam));
return CallWindowProc(lpPrevWndFunc, hWnd, msg, wParam, lParam);
}
bool unsubClassForm(System.Windows.Forms.Form form)
{
bool bRet = false;
if (lpPrevWndFunc == IntPtr.Zero)
return bRet;
if (SetWindowLong(form.Handle, GWL_WNDPROC, lpPrevWndFunc.ToInt32()) != 0)
{
bRet = true;
lpPrevWndFunc = IntPtr.Zero;
}
return bRet;
}
}
#endregion
如果表单被移动并且可能超出主屏幕边界,您应该能够使用代码“识别”。