WindowsFormHost Paint Event Not Firing

 ̄綄美尐妖づ 提交于 2019-12-23 04:29:22

问题


I have a panel withing a WindowsFormHost in a WPF app. Eventually i need to be able to click and draw points. I had it working fully in a separate WinForm form... but for some reason the paint event won't fire at all when Invalidate() is called! The click event is definitely firing as the message box comes up on the click. Not a peep out of the paint even though. Can't figure out why for the life of me... here's the code.

XML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    Title="MainWindow" Height="1000" Width="1000"
    WindowState="Maximized" SizeChanged="Window_SizeChanged" LocationChanged="Window_LocationChanged" Background="Gray">

<DockPanel LastChildFill="True">
    <ToolBarTray DockPanel.Dock="Top">
    <ToolBar Width="Auto" HorizontalAlignment="Stretch">
            <Menu>
                <MenuItem Header="File" VerticalAlignment="Center">
                        <MenuItem Header="New" Click="MenuItem_Click_3" />
                        <MenuItem Header="Open" Click="MenuItem_Click_4"/>
                        <MenuItem Header="Open Recent">
                            <MenuItem Header="Some Recent Files"/>
                        </MenuItem>
                        <MenuItem Header="Import H2OFate File" Click="MenuItem_Click_7"/>
                        <Separator/>
                        <MenuItem Header="Save" Click="MenuItem_Click_1" />
                        <MenuItem Header="Save As" Click="MenuItem_Click_5"/>
                        <MenuItem Header="Save without Results"/>
                        <MenuItem Header="Delete Unnecessary Files"/>
                        <Separator/>
                        <MenuItem Header="File Options"/>
                        <Separator/>
                        <MenuItem Header="Project Info"/>
                        <MenuItem Header="Supply Info" />
                        <Separator/>
                        <MenuItem Header="Print" Click="MenuItem_Click_6" />
                        <Separator/>
                        <MenuItem Header="Reload Current File"/>
                        <MenuItem Header="Exit                CTRL+Q" Click="MenuItem_Click"/>
                        </MenuItem>
            <MenuItem Header="Network Design" VerticalAlignment="Center">
                        <MenuItem Header="Something" Click="MenuItem_Click_8"/>
            </MenuItem>
            <MenuItem Header="Run Analysis" VerticalAlignment="Center">
                    <MenuItem Header="Run" Click="MenuItem_Click_2"/>
            </MenuItem>
            <MenuItem Header="View Results" VerticalAlignment="Center">
                    <MenuItem Header="Numeric"/>
                    <MenuItem Header="X-Y Plots"/>
                    <MenuItem Header="Contours"/>
                    <MenuItem Header="Reports"/>                                                
            </MenuItem>
            <MenuItem Header="Facilities Management" VerticalAlignment="Center">
                    <MenuItem Header="Some Item"/>
                    <MenuItem Header="Hydraulics"/>
                    <MenuItem Header="Contamination"/>
                    <MenuItem Header="Sensor Placement"/>
            </MenuItem>
            <MenuItem Header="Tools/Databases" VerticalAlignment="Center">
                    <MenuItem Header="Some Item"/>
                    <MenuItem Header="Hydraulics"/>
                    <MenuItem Header="Contamination"/>
                    <MenuItem Header="Sensor Placement"/>
            </MenuItem>
            <MenuItem Header="Help" VerticalAlignment="Center">
                    <MenuItem Header="Some Item"/>
                    <MenuItem Header="Help"/>
                    <MenuItem Header="Units"/>
                    <MenuItem Header="Demo Examples"/>
                    <MenuItem Header="Tutorial"/>
                    <MenuItem Header="How To"/>
                    <MenuItem Header="About"/>
            </MenuItem>
            </Menu>
            </ToolBar>

    <ToolBar>

            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_1">                        
                <Ellipse Width="25" Height="25" Fill="Blue" Stroke="Black" VerticalAlignment="Center"/>
            </Button >
            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_1">
                <Rectangle Width="38" Fill="Red" Height="7" Stroke="Black"/>
            </Button >
            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_2">
                <Polygon Fill="Yellow" Stroke="Black" Points="0,10 30,10 30,0 28,0 28,3 2,3 2,0 0,0" VerticalAlignment="Center"/>
            </Button >
            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_3">
                <Polygon Stroke="Black" Fill="Green" VerticalAlignment="Center" Points="22,0 22,7 15,7 15,15 9,15 9,7 0,7 0,0  "/>
            </Button > 
    </ToolBar>
        </ToolBarTray>
            <TabControl Width ="1000" DockPanel.Dock="Left">
        <TabItem Header="Map">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <WindowsFormsHost Grid.Row="0" x:Name="wfPanel" >
                    <WindowsFormsHost.Child>
                        <wf:Panel x:Name="wfSurface" MouseClick="wfSurface_MouseClick"/>
                    </WindowsFormsHost.Child>
                </WindowsFormsHost>
            </Grid>
        </TabItem>
        <TabItem Header="Map Settings">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="250"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Button Content="Testing 123"/>
            </Grid>
        </TabItem>

    </TabControl>
    <TabControl DockPanel.Dock="Right">

    </TabControl>
</DockPanel>   

C#

private void wfSurface_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Graphics g;
        g = wfSurface.CreateGraphics();
        epanet epa = epanet.GetInstance();
        SolidBrush s = new SolidBrush(System.Drawing.Color.Blue);
        g.FillEllipse(s, Convert.ToInt32(epa.xCord), Convert.ToInt32(epa.yCord), 50, 50);
        g.FillEllipse(s, 400, 400, 500, 500);
        MessageBox.Show("paint event fired");
    }

    private void wfSurface_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        epanet epa = epanet.GetInstance();
        epa.xCord = e.X;
        epa.yCord = e.Y;
        MessageBox.Show("xCord is:  " + e.X.ToString());
        wfSurface.Invalidate();
    }

Edit:

And to further check that the paint event is the issue I added the line g.FillEllipse(s,400,400,400,400); into the click event and it successfully drew the ellipse.


回答1:


Not much of a WPF guy, but my guess would be to add this:

<WindowsFormsHost.Child>
  <wf:Panel x:Name="wfSurface" Paint="wfSurface_Paint"
                               MouseClick="wfSurface_MouseClick"/>
</WindowsFormsHost.Child>


来源:https://stackoverflow.com/questions/15118173/windowsformhost-paint-event-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!