天天看點

winform-wpf-窗體混合顯示winform-wpf-窗體疊加顯示

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;
            }
        }
    }
}
           
效果圖
winform-wpf-窗體混合顯示winform-wpf-窗體疊加顯示

繼續閱讀