하위 form2, form3 은 항상 form1보다 앞에 위치!

(Winform) 하위 form2, form3이 form1 앞에 위치


(WPF에 Winform 삽입) form1이 MainWindow 앞에 위치

 

Form 앞쪽에 고정 시키고 싶은 Form이 있는 경우입니다.

Winform에서는 Owner 속성을 지정해주면 해당 Form보다 항상 앞에 위치하게 됩니다.

WPF에서는 창의 Handle 값을 가져와서 Winform에서 Show 할 때 지정해줄 수 있습니다.

 

[Winform]

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private Form2 form2 = new Form2();
        private Form3 form3 = new Form3();
        private int titleSize = 30;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            form2.Owner = this; //Form3은 Form1 앞에 위치
            form3.Owner = this; //Form3은 Form1 앞에 위치

            form2.Size = new Size(this.Width / 2, this.Height - titleSize);
            form3.Size = new Size(this.Width / 2, this.Height - titleSize);

            form2.Show();
            form3.Show();

            form2.Location = new Point(this.Location.X, this.Location.Y + titleSize);
            form3.Location = new Point(this.Location.X + form2.Size.Width, this.Location.Y + titleSize);
        }

        private void Form1_Move(object sender, EventArgs e)
        {
            form2.Location = new Point(this.Location.X, this.Location.Y + titleSize);
            form3.Location = new Point(this.Location.X + form2.Size.Width, this.Location.Y + titleSize);
        }
    }
}

 

[WPF 에서 Winform]

using System;
using System.Windows;
using System.Windows.Interop;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Form1 form1 = new Form1();

            WindowInteropHelper helper = new WindowInteropHelper(this);
            IntPtr handle = helper.Handle; //WPF 핸들값 가져오기

            form1.Show(new Win32Window(handle)); //form1이 항상 윗쪽에!
        }
    }

    class Win32Window : System.Windows.Forms.IWin32Window
    {
        private IntPtr handle;
        public IntPtr Handle
        {
            get
            {
                return handle;
            }
        }

        public Win32Window(IntPtr handle)
        {
            this.handle = handle;
        }
    }
}

+ Recent posts