My WPF sample using GdViewer with WindowsFormsHost

Using GdPicture.NET in WPF application.
Post Reply
dan276
Posts: 20
Joined: Fri Mar 15, 2013 1:07 am

My WPF sample using GdViewer with WindowsFormsHost

Post by dan276 » Fri Aug 09, 2013 10:46 pm

FYI for people using WIndowsFormsHosts in WPF, it works very well. Here is some sample code to add buttons and to help to dynamically re-size the viewer based on window size.

Code: Select all

<WindowsFormsHost Grid.Row="1"   Grid.ColumnSpan="3" Margin="0 5 0 0" 
                                    
                                      Canvas.ZIndex="1"  Name="WindowsFormsHost1"  VerticalAlignment="Top">
                   
                        <gdpicture:GdViewer x:Name="gdpicViewer"  BackColor="White" Margin="5,5,5,5" DisplayQuality="DisplayQualityBicubicHQ"
                                            Dock="Fill"
                               DocumentAlignment="DocumentAlignmentTopCenter" DocumentPosition="DocumentPositionTopCenter" MouseButtonForMouseMode="MouseButtonLeft" MouseWheelMode="MouseWheelModeZoom"
                                              RectBorderSize="1" RectBorderColor="CornflowerBlue" RectIsEditable="True" 
                                             PageChanged="gdpicViewer_PageChanged"
                                            ZoomMode="ZoomModeWidthViewer" ZoomStep="25" PdfEnableFileLinks="True"
                                            EnabledProgressBar="True" EnableMouseWheel="True" ForceScrollBars="True" />
                    </WindowsFormsHost>

                    <Grid Grid.Row="0" Grid.ColumnSpan="3" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="110"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" Margin="3 0 10 0" Orientation="Horizontal">
                            <nqlib:PrevSmallButton Height="20" ToolTip="Previous Page" Name="btnPrevPage" Click="btnPrev_Click" />
   
                            <nqlib:NextSmallButton Height="20" Margin="5 0 0 0" ToolTip="Next Page" Name="btnNextPage" Click="btnNext_Click" />
                        </StackPanel>
                        <StackPanel Grid.Column="1" Orientation="Horizontal">
                            <TextBox Name="tbPageNo" Width="35" KeyUp="tbPageNo_KeyUp" />
                            <TextBlock Name="txtDashTotalPages" FontWeight="Bold"  Foreground="Black" Text=" / 34 Pages" />

                        </StackPanel>

                        <nqlib:PrintWordButton Grid.Column="2" Margin="0 0 0 0" Height="22" Click="PrintWordButton_Click" />
                        <StackPanel Grid.Column="3" Orientation="Horizontal">
                            <telerik:RadWatermarkTextBox Name="tbFind" WatermarkContent="Search for Text"
                                                         Margin="10 0 0 0" Width="100" KeyUp="tbFind_KeyUp" />
                          

                        </StackPanel>

                        <StackPanel Grid.Column="4" Margin="8 0 0 0" Orientation="Horizontal">
                            <Button FontWeight="Bold" Width="20" ToolTip="Zoom In" Name="btnZoomIn"  Click="btnZoomIn_Click" >
                                <Image Source="/MyApp;component/Icons/zoomIn.png" />
                            </Button>
                            <Button  FontWeight="Bold" Width="20"  ToolTip="Zoom Out" Name="btnZoomOut" Margin="5 0 0 0" Click="btnZoomOut_Click" >
                                <Image Source="/MyApp;component/Icons/zoomOut.png" />
                            </Button>
                            <Button  FontWeight="Bold" Width="20" Height="20"  ToolTip="Rotate 90 Degrees" Name="btnRotate90" Margin="5 0 0 0" Click="btnRotate90_Click" >
                                <Image Source="/MyApp;component/Icons/rotate.png" />
                            </Button>
                            <Button Content="Fit" Name="btnFit" ToolTip="Fit Document to Viewer" Margin="5 0 0 0"   Click="btnFit_Click" />
                            <Button Content="Width"  Margin="5 0 0 0" ToolTip="Fit the Document width to the Viewer"  Name="btnFitHeight"  Click="btnFitHeight_Click" />
                        </StackPanel>

                    </Grid>


Add a LayoutRoot_SizeChanged event

Code: Select all

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {

 
            rtcProjectAndTasks.Height = LayoutRoot.ActualHeight - 70;
            rtcTasksForProject.Height = LayoutRoot.ActualHeight - 70;

            try
            {
                WindowsFormsHost1.Height = LayoutRoot.ActualHeight - 365;
                gdpicViewer.Height = Convert.ToInt32(LayoutRoot.ActualHeight - 365);
    
         
            }
            catch {

                WindowsFormsHost1.Height = 200;
        
            }
      
        }

User avatar
Loïc
Site Admin
Posts: 5568
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: My WPF sample using GdViewer with WindowsFormsHost

Post by Loïc » Sun Aug 11, 2013 11:40 am

Thank you very much for sharing this Dan. You are awesome!

Cheers,

Loïc

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest