无论是winform内嵌wpf,还是wpf内嵌winform,效果总是不友好。那么两个窗体嵌套。这种方式简单粗暴,用户快速开发的小项目还是可以的。其实原来还是非常简单的。就是窗体叠在一起,没啥技术含量,只适合不需要自定义窗体的小项目。要特殊定制窗体样式。那么这种方式,就不适合了。
wpf-页面代码
wpf-后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WF.Demo
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
MainForm form;
public MainWindow()
{
InitializeComponent();
this.form = new MainForm { Main = this };
this.form.Show();
this.Topmost = true;
}
}
}
winform-后台代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WF.Demo
{
public partial class MainForm : Form
{
public MainWindow Main { get; set; }
public CancellationTokenSource CancelWhileSetMainSize { get; set; }
public FormWindowState LastFormWindowState { get; set; }
public MainForm()
{
InitializeComponent();
this.ResizeBegin += MainForm_ResizeBegin;
this.ResizeEnd += MainForm_ResizeEnd;
this.Move += MainForm_Move;
this.FormClosing += MainForm_FormClosing;
this.FormClosed += MainForm_FormClosed;
this.LastFormWindowState = this.WindowState;
this.CancelWhileSetMainSize = new CancellationTokenSource();
Task.Factory.StartNew(this.WhileSetMainSize, CancelWhileSetMainSize.Token);
}
private void WhileSetMainSize()
{
try
{
while (!this.CancelWhileSetMainSize.IsCancellationRequested)
{
if (this.Main != null)
{
this.Main.Dispatcher.Invoke(() =>
{
if (this.WindowState != this.LastFormWindowState)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Main.Hide();
}
else
{
this.Main.Show();
this.SetMainSize();
}
this.LastFormWindowState = this.WindowState;
}
});
Thread.Sleep(500);
}
}
}
catch (AggregateException ae)
{
foreach (Exception e in ae.InnerExceptions)
{
if (e is TaskCanceledException)
Console.WriteLine("Unable to compute mean: {0}",
((TaskCanceledException)e).Message);
else
Console.WriteLine("Exception: " + e.GetType().Name);
}
}
finally
{
this.CancelWhileSetMainSize.Dispose();
}
}
private void MainForm_ResizeBegin(object sender, EventArgs e)
{
this.Main.Hide();
}
private void MainForm_ResizeEnd(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Main.Hide();
}
else
{
if (this.Main.Visibility != System.Windows.Visibility.Visible)
{
this.Main.Show();
}
}
this.SetMainSize();
}
private void MainForm_Move(object sender, EventArgs e)
{
this.SetMainSize();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.CancelWhileSetMainSize.Cancel();
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
this.CancelWhileSetMainSize.Dispose();
App.Current.Shutdown();
}
private void SetMainSize()
{
if (Main == null) return;
//if (this.Main.Width != this.panel1.Width && this.Main.Height != this.panel1.Height)
{
this.Main.Width = this.panel1.Width;
this.Main.Height = this.panel1.Height;
}
var point = this.PointToScreen(this.panel1.Location);
//if (this.Main.Top != point.Y && this.Main.Left != point.X)
{
this.Main.Top = point.Y;
this.Main.Left = point.X;
}
}
}
}
效果图