简体   繁体   English

在 WindowsFormHost 面板内排列控件 - wpf

[英]Arranging controls inside a panel of WindowsFormHost - wpf

I have three controls inside a panel like below.我在下面的面板中有三个控件。 I have to arrange one at center of the panel.我必须在面板的中心安排一个。 and other two should place just left and right side of center one.其他两个应该放在中心一个的左右两侧。 I have tried like below.我已经尝试过如下。 But getting all three controls are aligned to left.但是让所有三个控件都向左对齐。 Please help to correct.请帮忙改正。

   <WindowsFormsHost  x:Name="windowsFormsHost1"  Grid.Row="5"   Grid.ColumnSpan="3"   Initialized="WindowsFormsHost_Initialized"  HorizontalAlignment="Center" VerticalAlignment="Center"  >
        <wf:Panel x:Name="Panel_glcontrol" Dock="None" BackColor="yellow" >
            <wf:Panel.Controls>
                <opentk:GLControl x:Name="glControl" Width="450" Height="299"  Dock="None"   Visible="True" MouseMove="GlControl_MouseMove" MouseDown="GlControl_MouseDown"  Resize="glControl_Resize" Paint="glControl_Paint"  />
                <opentk:GLControl x:Name="glControl2"  Width="450" Height="299" Dock="None"    MouseDown="GlControl2_MouseDown" MouseMove="GlControl2_MouseMove"   Visible="True"  Resize="glControl2_Resize" Paint="glControl2_Paint"  />
                <opentk:GLControl x:Name="glControl3" Width="450" Dock="None" Height="299"  Visible="True"  MouseDown="GlControl3_MouseDown" MouseMove="GlControl3_MouseMove" Resize="glControl3_Resize" Paint="glControl3_Paint" />
            </wf:Panel.Controls>
        </wf:Panel>
    </WindowsFormsHost>

     glControl2.Location = new System.Drawing.Point((_Screenwidth / 2) - glControl2.Width / 2, (_Screenheight - glControl.Height) / 2);
     glControl.Location = new System.Drawing.Point(glControl2.Location.X - glControl.Width - 5, (_Screenheight - glControl2.Height) / 2 );          
     glControl3.Location = new System.Drawing.Point(glControl2.Location.X + glControl2.Width + 5, (_Screenheight - glControl3.Height) / 2 );

What if you put each WinForms control in its own host and then used WPF panels to for the layout?如果您将每个 WinForms 控件放在其自己的主机中,然后使用 WPF 面板进行布局怎么办? Something like this:像这样的东西:

<UniformGrid Rows="1" Grid.Row="5" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Yellow">
    <WindowsFormsHost>
        <opentk:GLControl x:Name="glControl" Width="450" Height="299" Dock="None" Visible="True" MouseMove="GlControl_MouseMove" MouseDown="GlControl_MouseDown" Resize="glControl_Resize" Paint="glControl_Paint"/>
    </WindowsFormsHost>
    <WindowsFormsHost>
        <opentk:GLControl x:Name="glControl2" Width="450" Height="299" Dock="None" MouseDown="GlControl2_MouseDown" MouseMove="GlControl2_MouseMove" Visible="True" Resize="glControl2_Resize" Paint="glControl2_Paint"/>
    </WindowsFormsHost>
    <WindowsFormsHost>
        <opentk:GLControl x:Name="glControl3" Width="450" Dock="None" Height="299" Visible="True" MouseDown="GlControl3_MouseDown" MouseMove="GlControl3_MouseMove" Resize="glControl3_Resize" Paint="glControl3_Paint"/>
    </WindowsFormsHost>
</UniformGrid>

I have solved it by removing horizontal and vertical alignment of WindowsFormsHost.我已经通过删除 WindowsFormsHost 的水平和垂直 alignment 来解决它。 And then adding below function to relocate the controls.然后在下面添加 function 以重新定位控件。

     private void RelocateOpenGLs()
    {
        int PSBH = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
        int TaskBarHeight = PSBH - System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;//have to hide taskbar

        glControl2.Location = new System.Drawing.Point((_Screenwidth / 2) - glControl2.Width / 2, ((Panel_glcontrol.Height - glControl.Height) / 2) - TaskBarHeight);
        glControl.Location = new System.Drawing.Point(glControl2.Location.X - glControl.Width - 2, ((Panel_glcontrol.Height - glControl2.Height) / 2) - TaskBarHeight);
        glControl3.Location = new System.Drawing.Point(glControl2.Location.X + glControl2.Width + 2, ((Panel_glcontrol.Height - glControl3.Height) / 2) - TaskBarHeight);

    }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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