winform-wpf-窗体混合显示

来源:互联网 发布:steam网络无法连接 编辑:程序博客网 时间:2024/05/22 02:26

winform-wpf-窗体叠加显示

无论是winform内嵌wpf,还是wpf内嵌winform,效果总是不友好。那么两个窗体嵌套。这种方式简单粗暴,用户快速开发的小项目还是可以的。其实原来还是非常简单的。就是窗体叠在一起,没啥技术含量,只适合不需要自定义窗体的小项目。要特殊定制窗体样式。那么这种方式,就不适合了。

wpf-页面代码

<Window    x:Class="WF.Demo.MainWindow"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:local="clr-namespace:WF.Demo"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    Title="MainWindow"    Width="800"    Height="600"    AllowsTransparency="True"    Background="{x:Null}"    ShowInTaskbar="False"    WindowStyle="None"    mc:Ignorable="d">    <Grid>        <Grid.RowDefinitions>            <RowDefinition Height="auto" />            <RowDefinition Height="*" />            <RowDefinition Height="auto" />        </Grid.RowDefinitions>        <Grid            Grid.Row="0"            Height="60"            Background="#CC8A2BE2">            <TextBlock                HorizontalAlignment="Center"                VerticalAlignment="Center"                Foreground="White"                Text="标题" />        </Grid>        <Grid            Grid.Row="2"            Height="60"            Background="#CC000000">            <TextBlock                HorizontalAlignment="Center"                VerticalAlignment="Center"                Foreground="White"                Text="菜单" />        </Grid>    </Grid></Window>

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{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    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;            }        }    }}

效果图

这里写图片描述

原创粉丝点击