0

我在 WPF 应用程序中有一个带有 WindowsFormHost 的面板。最终我需要能够点击和绘制点。我让它在一个单独的 WinForm 表单中完全工作......但由于某种原因,当 Invalidate() 被调用时,绘制事件根本不会触发!当消息框出现在点击时,点击事件肯定会触发。即使没有窥视油漆。无法弄清楚为什么我的生活......这是代码。

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();
    }

编辑:

为了进一步检查绘制事件是否是我将线添加g.FillEllipse(s,400,400,400,400);到点击事件中的问题,它成功地绘制了椭圆。

4

1 回答 1

1

不是一个 WPF 家伙,但我的猜测是添加这个:

<WindowsFormsHost.Child>
  <wf:Panel x:Name="wfSurface" Paint="wfSurface_Paint"
                               MouseClick="wfSurface_MouseClick"/>
</WindowsFormsHost.Child>
于 2013-02-27T17:30:53.653 回答