是的,你只需要使用user32.dll中的一些低級別的win32函數:SetParent,GetWindowLog,SetWindowLong,MoveWindow。您可以創建一個空的.NET容器控件,將本機窗口的父級設置爲.NET控件,然後(可選)修改窗口樣式(即刪除本機窗口的邊框),並注意將其與.NET控件。請注意,在受管級別,.NET控件將不知道它有任何子級。
在.NET控件這樣做
public void AddNativeChildWindow(IntPtr hWndChild){
//adjust window style of child in case it is a top-level window
int iStyle = GetWindowLong(hWndChild, GWL_STYLE);
iStyle = iStyle & (int)(~(WS_OVERLAPPEDWINDOW | WS_POPUP));
iStyle = iStyle | WS_CHILD;
SetWindowLong(hWndChild, GWL_STYLE, iStyle);
//let the .NET control be the parent of the native window
SetParent((IntPtr)hWndChild, this.Handle);
this._childHandle=hWndChild;
// just for fun, send an appropriate message to the .NET control
SendMessage(this.Handle, WM_PARENTNOTIFY, (IntPtr)1, (IntPtr)hWndChild);
}
然後覆蓋.NET控件的WndProc中,使其適當調整原生形式 - 例如,以填補客戶區。
protected override unsafe void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_PARENTNOTIFY:
//... maybe change the border styles , etc
break;
case WM_SIZE:
iWid =(int)((int)m.LParam & 0xFFFF);
iHei= (int) (m.LParam) >> 16;
if (_childHandle != (IntPtr)0)
{
MoveWindow(_childHandle, 0, 0, iWid, iHei, true);
}
break;
}
}
您的意思是嵌入爲顯示爲子窗口,還是嵌入到.NET項目中的代碼中? – 2011-03-01 16:52:39