WPF页面嵌入exe文件,怎样控制嵌入文件窗体的缩放比例?

ylly2508 2010-07-21 03:19:02
在页面上做了一个 Border 控件,将.exe文件打开后,将接收到的窗口句柄放到 Border控件中,但是当放大缩小Border的时候.exe窗体始终保持原大小,这个是为什么~?

我想让.exe文件的窗口,始终保持和border控件的大小一样,也跟着缩放~~能办到么??

还请高手指点下~~!

以下是部分代码:


[DllImport("user32.dll")]
private static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint newLong);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

[DllImport("user32.dll")]
private static extern int EnumWindows(CallBackPtr callPtr, ref WindowInfo WndInfoRef);
[DllImport("User32.dll")]
static extern int GetWindowText(IntPtr handle, StringBuilder text, int MaxLen);
[DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hwnd, ref RECT rc);
internal const int
GWL_WNDPROC = (-4),
GWL_HINSTANCE = (-6),
GWL_HWNDPARENT = (-8),
GWL_STYLE = (-16),
GWL_EXSTYLE = (-20),
GWL_USERDATA = (-21),
GWL_ID = (-12);
internal const uint
WS_CHILD = 0x40000000,
WS_VISIBLE = 0x10000000,
LBS_NOTIFY = 0x00000001,
HOST_ID = 0x00000002,
LISTBOX_ID = 0x00000001,
WS_VSCROLL = 0x00200000,
WS_BORDER = 0x00800000,
WS_POPUP = 0x80000000;
private const int HWND_TOP = 0x0;
private const int WM_COMMAND = 0x0112;
private const int WM_QT_PAINT = 0xC2DC;
private const int WM_PAINT = 0x000F;
private const int WM_SIZE = 0x0005;
private const int SWP_FRAMECHANGED = 0x0020;
private Border WndHoster;
private double screenW, screenH;
private System.Diagnostics.Process appProc;
private uint oldStyle;
private IntPtr hwndHost;
private String appPath;
public EmbeddedApp(Border b, double sW, double sH, String p, String f)
{
WndHoster = b;
screenH = sH;
screenW = sW;
appPath = p;
WinInfo = new WindowInfo();
WinInfo.winTitle = f;
}
protected override HandleRef BuildWindowCore( HandleRef hwndParent )
{
hwndHost = FindTheWindow( );
//if (hwndHost == null)
//{
appProc = new System.Diagnostics.Process( );
appProc.StartInfo.FileName = appPath;
appProc.Start( );
Thread.Sleep( 1000 );
hwndHost = FindTheWindow( );
//}
// 嵌入在HwnHost中的窗口必须要 设置为WS_CHILD风格
oldStyle = GetWindowLong( hwndHost , GWL_STYLE );
uint newStyle = oldStyle;
//WS_CHILD和WS_POPUP不能同时存在。有些Win32窗口,比如QQ的窗口,有WS_POPUP属性,这样嵌入的时候会导致程序错误
newStyle |= WS_CHILD;
newStyle &= ~WS_POPUP;
newStyle &= ~WS_BORDER;
SetWindowLong( hwndHost , GWL_STYLE , newStyle );
//将窗口居中,实际上是将窗口的容器居中
RePosWindow( WndHoster , WndHoster.Width , WndHoster.Height );

//将netterm的父窗口设置为HwndHost
SetParent( hwndHost , hwndParent.Handle );
return new HandleRef( this , hwndHost );
}
protected override void DestroyWindowCore(System.Runtime.InteropServices.HandleRef hwnd)
{
SetWindowLong(hwndHost, GWL_STYLE, oldStyle);
SetParent(hwndHost, (IntPtr)0);
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct WindowInfo
{
public String winTitle;
public RECT r;
public IntPtr hwnd;
}
public delegate bool CallBackPtr(IntPtr hwnd, ref WindowInfo WndInfoRef);
private static CallBackPtr callBackPtr;
private WindowInfo WinInfo;
public static bool CallBackProc(IntPtr hwnd, ref WindowInfo WndInfoRef)
{
StringBuilder str = new StringBuilder(512);
GetWindowText(hwnd, str, str.Capacity);
if (str.ToString().IndexOf(WndInfoRef.winTitle, 0) >= 0)
{
WndInfoRef.hwnd = hwnd;
GetWindowRect(hwnd, ref (WndInfoRef.r));
}

return true;
}
public IntPtr FindTheWindow()
{
callBackPtr = new CallBackPtr(CallBackProc);
EnumWindows(callBackPtr, ref WinInfo);
return WinInfo.hwnd;
}
public void RePosWindow(Border b, double screenW, double screenH)
{
double width = WinInfo.r.right - WinInfo.r.left;
double height = WinInfo.r.bottom - WinInfo.r.top;

double left = ( screenW - width ) / 2;
double right = ( screenW - width ) / 2;
double top = ( screenH - height ) / 2;
double bottom = ( screenH - height ) / 2;

//double width = 300;
//double height = 300;
//double left = 50;
//double right = 50;
//double top = 50;
//double bottom = 50;

b.Margin = new Thickness(left, top, right, bottom);
}



//加载.exe文件
String path=Environment.CurrentDirectory + @"\QQ.exe";
EmbeddedApp ea = new EmbeddedApp( WndHost , 100 , 100 , path , "photomain" );
WndHost.Child = ea;

System.Threading.Thread.Sleep( 10000 );
...全文
1767 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ylly2508 2010-07-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jv9 的回复:]
ZoomPanel是自定义控件,需要添加引用,安装该控件。
[/Quote]

先谢谢楼上的帮助~~~ZoomPanel的dll我找到了~~也测试了~~~这个控件是放入一个xaml页面的~~要指定页面的路径,并不能赋一个窗口句柄~~~

我前面代码中 hwndHost = FindTheWindow( ); 是找到一个窗口句柄~~

控制一个窗口句柄这么难么~???
jv9 2010-07-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ylly2508 的回复:]
我写的时候为什么提示

xml 命名空间 “clr-namespace:Ab2d.Controls;assembly=Ab2d.Controls.ZoomPanel” 中不存在标记 “ZoomPanel” ????

请问要加什么引用么??
[/Quote]

ZoomPanel是自定义控件,需要添加引用,安装该控件。
ylly2508 2010-07-23
  • 打赏
  • 举报
回复
顶~~!
ylly2508 2010-07-22
  • 打赏
  • 举报
回复
我写的时候为什么提示

xml 命名空间 “clr-namespace:Ab2d.Controls;assembly=Ab2d.Controls.ZoomPanel” 中不存在标记 “ZoomPanel” ????

请问要加什么引用么??
ylly2508 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jv9 的回复:]
使用Border控制应用窗口缩放比较困难。我看到过ZoomPanel可以实现你的需求。

http://www.wpf-graphics.com/ZoomPanel.aspx

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b141a9d1-dfbc-4992-969a-3d3672c7e18f
[/Quote]

这个控件是,在控件的内部控制应用程序的窗口吧~~~我想实现的是,让窗口随着控件的缩放自动缩放,可以办到么??
jv9 2010-07-22
  • 打赏
  • 举报
回复
使用Border控制应用窗口缩放比较困难。我看到过ZoomPanel可以实现你的需求。

http://www.wpf-graphics.com/ZoomPanel.aspx

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b141a9d1-dfbc-4992-969a-3d3672c7e18f

8,755

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧