110,579
社区成员
发帖
与我相关
我的任务
分享
// 一.获得当前活动窗口的句柄
//用一下的API函数获得
[DllImport("user32")]
public static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
//二.重新声明 IWin32Window 接口
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
private System.IntPtr _hwnd;
public WindowWrapper(System.IntPtr handle)
{
_hwnd = handle;
}
public System.IntPtr Handle
{
get { return _hwnd; }
}
}
//三. 把当前活动的句柄转化成IWin32Window 接口,并Show窗体,这样就可以达到两个不
// 同线程的窗体粘在一快了。
System.IntPtr IntPart = GetForegroundWindow();
WindowWrapper OwinForm = new WindowWrapper(IntPart);
Form test = new Form();
test.Show(OwinForm);
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class Form1 : Form
{
Form1()
{
Button btn = new Button();
btn.Parent = this;
btn.Text = "Font";
btn.Click += delegate { new MyFontDialog(50, 80).ShowDialog(); };
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
class MyFontDialog : FontDialog
{
const int WM_INITDIALOG = 0x0110;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOZORDER = 0x0004;
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
{
SetWindowPos(hWnd, (IntPtr)0, Left, Top, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
return base.HookProc(hWnd, msg, wParam, lParam);
}
// 窗口的位置
int Left, Top;
public MyFontDialog(int x, int y)
{
Left = x;
Top = y;
}
}
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected virtual IntPtr HookProc(
IntPtr hWnd,
int msg,
IntPtr wparam,
IntPtr lparam
)
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class FontDialog : System.Windows.Forms.FontDialog
{
public int Left { get { return r.Left ; } set { r.Left = value; } }
public int Top { get { return r.Top ; } set { r.Top = value; } }
public int Width { get { return r.Width ; } set { r.Width = value; } }
public int Height { get { return r.Height ; } set { r.Height = value; } }
public Point Location { get { return r.Location; } set { r.Location = value; } }
public Size Size { get { return r.Size ; } }
Rect r;
const uint SWP_NOSIZE = 1;
const uint SWP_NOZORDER = 4;
[DllImport("user32.dll")]
static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
{
SetWindowPos(hWnd, (IntPtr)0, Left, Top, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
GetWindowRect(hWnd, ref r);
return base.HookProc(hWnd, msg, wParam, lParam);
}
}
class Form1 : Form
{
Form1()
{
Button btn = new Button();
btn.Parent = this;
btn.Text = "&Font";
btn.Click += delegate
{
FontDialog fd = new FontDialog();
fd.Location = new Point(10, 50);
fd.ShowDialog();
};
}
static void Main()
{
Application.Run(new Form1());
}
}
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public int Width { get { return Right - Left; } set { Right = Left + value; } }
public int Height { get { return Bottom - Top; } set { Bottom = Top + value; } }
public Size Size
{
get { return new Size(Width, Height); }
set { Width = value.Width; Height = value.Height; }
}
public Point Location
{
get { return new Point(Left, Top); }
set { Right -= (Left - value.X); Bottom -= (Bottom - value.Y); Left = value.X; Top = value.Y; }
}
public override string ToString()
{
return string.Format("{{X={0},Y={1},Width={2},Height={3}}}", Left, Top, Width, Height);
}
}