简体   繁体   中英

WPF Custom control in WinForms with floating precision size

I was wondering is it possible to create a panel-like control in wpf, and another wpf custom control that will be the child of this panel, all in WinForms application. And then, would it be possible to set the custom control's size in float? Also would I be able to rotate that custom control?

Thanks in advance

You will need to use the System.Windows.Forms.Integration NameSpace, more specifically the ElementHost Control , that will allow you to embed a WPF Control in Winforms.


This is a quick and simple demonstration of changing the WPF UserControls Child UserControls Size and Location. I will leave the animation to you.

Main Winform Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ElementHost host;
        WpfControlLibrary1.UserControl1 uc;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            host = new ElementHost();
            host.Dock = DockStyle.Top;
            uc = new WpfControlLibrary1.UserControl1();
            host.Child = uc;
            this.Controls.Add(host);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            uc.SetChildLocation =  new  System.Windows.Point(uc.SetChildLocation.X + 25.5, uc.SetChildLocation.Y + 10.2);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            uc.SetChildSize = new System.Windows.Size(uc.SetChildSize.Width + .25, uc.SetChildSize.Height + .25);
        }
    }
}

UserControl1.xaml

<UserControl x:Class="WpfControlLibrary1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Background="Red" xmlns:my="clr-namespace:WpfControlLibrary1">
    <Canvas>
        <my:UserControl2 HorizontalAlignment="Center"   x:Name="userControl21" VerticalAlignment="Center" Canvas.Left="0" Canvas.Top="0" />

    </Canvas>
</UserControl>

UserControl1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfControlLibrary1  
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public Point  SetChildLocation
        {
            get
            {
                return new Point(Canvas.GetLeft(userControl21), Canvas.GetTop(userControl21));
            }

            set
            {
                Canvas.SetLeft(userControl21, value.X);
                Canvas.SetTop(userControl21, value.Y);
            }
        }

        public Size SetChildSize
        {
            get
            {
                return new Size(userControl21.ActualWidth, userControl21.ActualHeight);
            }
            set
            {
                userControl21.Width = value.Width;
                userControl21.Height = value.Height;
            }
        }
    }
}

UserControl2.xaml

<UserControl x:Class="WpfControlLibrary1.UserControl2"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" SizeChanged="UserControl_SizeChanged">
    <Grid>
        <Rectangle x:Name="rect" Fill="Blue" Height="40" Width="120"></Rectangle>
    </Grid>
</UserControl>

UserControl2.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfControlLibrary1
{
    /// <summary>
    /// Interaction logic for UserControl2.xaml
    /// </summary>
    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            rect.Width = e.NewSize.Width;
            rect.Height = e.NewSize.Height;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM