|
VB .NET Helper Newsletter
|
Rod Stephens
|
Mar 30, 2009 08:57 PST
|
Someone posted another review of my book "Beginning Database Design
Solutions."
If you have a copy (for example, if I gave you a free copy), PLEASE post
a review. Books do much better (and people can decide much more
effectively whether they should buy a book) if it has lots of reviews.
-----
The WPF/XAML example demonstrates one of my pet peeves with Microsoft.
Actually it's more than a "pet." More like a wild peeve. It's not tame
enough to be domesticated.
<Peeve>
The basic idea can be summarized as "Making Web development easier at
the expense of desktop development" and this isn't the first (or last)
where Microsoft has done such a thing.
The problem here is that the WPF Image control displays an Image. But
you can't just give it an image that you have included in the project as
a resources. Instead you need to give it an ImageSource.
One of the easiest ways to do that is to build a BitmapImage. But the
weirdness doesn't stop there. If you have included an image as a
resource, you can't just grab it.
Instead you need to create the BitmapImage from a URI (Uniform Resource
Identifier). If the resource is a file on the Web or file system, you
can simply use its location as the URI. Therefore this is "easy" if
you're building a Web application.
But if you're including the image as a resource you need to use the
bizarre syntax "pack://application:,,,/Earth.jpg" to refer to it. Nice!
See the example for the few additional details.
</Peeve>
-----
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
Both Contents:
1. New HowTo: Display an image saved as a resource in WPF/XAML using
Visual Basic 2008
2. New HowTo: Print a Window in WPF/XAML using Visual Basic 2008
3. New Links
==========
++++++++++
<Both>
++++++++++
==========
1. New HowTo: Display an image saved as a resource in WPF/XAML using
Visual Basic 2008
http://www.vb-helper.com/howto_xaml_load_image_resource.html
http://www.vb-helper.com/HowTo/howto_xaml_load_image_resource.zip
Include the image file in the project and make sure it is set to
Resource. Then loading the image into an Image control at run time is
easy, once you figure out the bizarre syntax.
imgEarth.Source = New BitmapImage(New
Uri("pack://application:,,,/Earth.jpg"))
==========
2. New HowTo: Print a Window in WPF/XAML using Visual Basic 2008
http://www.vb-helper.com/howto_xaml_print_window_centered.html
http://www.vb-helper.com/HowTo/howto_xaml_print_window_centered.zip
This is basically a version of PrintForm for WPF.
The PrintDialog class can display a print dialog and its PrintVisual
method can easily print a Window or other visual control. Unfortunately
the result is positioned at the extreme upper left corner of the printed
page. Because most printers cannot print all the way to the edge of the
paper, that means the result is clipped at the top and left.
This program centers a Window's image, optionally enlarging it.
First, add references to System.Printing and ReachFramework (another
fine, intuitive name brought to you by WPF ;-). This example also
imports System.Windows.Shapes and System.Windows.Media.Effects to make
it easier to produce the results it generates.
When you click the Print Centered button, the following code creates a
new PrintDialog and displays it. If the user selects a printer and
clicks OK, the code calls PrintWindowCentered to do the actual printing.
Dim pd As New PrintDialog()
If pd.ShowDialog() Then
PrintWindowCentered(pd, Me, "New Customer", Nothing)
End If
When you click the Print Stretched button, the following code calls
PrintWindowCentered. The final argument is a margin around the edges of
the paper. The image of the Window will be enlarged as much as possible
to fit within this margin.
Dim pd As New PrintDialog()
If pd.ShowDialog() Then
PrintWindowCentered(pd, Me, "New Customer", New Thickness(50))
End If
The PrintVisual method prints a visual control in the paper's upper left
corner. While you can't change that (at least as far as I know), you can
create new controls that position the image of the control where you
want it. Subroutine PrintWindowCentered does that.
The routine first creates a Grid control that fills the entire printed
page and places a Viewbox centered inside it.
If the margin parameter is Nothing, then the code sets the Viewbox's
Stretch property to None so it won't resize the Window's image.
If the margin is not Nothing, then the code sets the Viewbox's Stretch
property to Uniform so it will stretch the Window's
image as much as possible without distorting it. It also sets the
Viewbox's Margin property to the desired margin so the Viewbox is
enlarged to fill the paper, minus the margin.
Next the code makes a VisualBrush from the Window. That makes the brush
contain an image of the Window.
The code then places a Rectangle inside the Viewbox, filling it with the
VisualBrush. This makes the Rectangle display an image of the Window.
The code adds a black border and a drop shadow.
Next the code calls thue Grid's Arrange method to make it arrange and
render its children recursively. If you don't do this, then the printout
is blank.
Finally thue code calls the PrintDialog's PrintVisual method to print
the Grid and its contents.
' Print a Window centered on the printer.
Private Sub PrintWindowCentered(ByVal pd As PrintDialog, ByVal win As
Window, ByVal title As String, ByVal margin As Thickness?)
' Make a Grid to hold the contents.
Dim drawing_area As New Grid()
drawing_area.Width = pd.PrintableAreaWidth
drawing_area.Height = pd.PrintableAreaHeight
' Make a Viewbox to stretch the result if necessary.
Dim view_box As New Viewbox()
drawing_area.Children.Add(view_box)
view_box.HorizontalAlignment = Windows.HorizontalAlignment.Center
view_box.VerticalAlignment = Windows.VerticalAlignment.Center
If margin Is Nothing Then
' Center without resizing.
view_box.Stretch = Windows.Media.Stretch.None
Else
' Resize to fit the margin.
view_box.Margin = margin.Value
view_box.Stretch = Windows.Media.Stretch.Uniform
End If
' Make a VisualBrush holding an image of the Window's contents.
Dim vis_br As New VisualBrush(win)
' Make a rectangle the size of the Window.
Dim win_rect As New Rectangle()
view_box.Child = win_rect
win_rect.Width = win.Width
win_rect.Height = win.Height
win_rect.Fill = vis_br
win_rect.Stroke = Brushes.Black
win_rect.BitmapEffect = New DropShadowBitmapEffect()
' Arrange to produce output.
Dim rect As New Rect(0, 0, _
pd.PrintableAreaWidth, pd.PrintableAreaHeight)
drawing_area.Arrange(rect)
' Print it.
pd.PrintVisual(drawing_area, title)
End Sub
Note that WPF doesn't have a print preview dialog. I guess it is assumed
that you are printing something that you have already displayed on the
screen so you don't need a preview. (You can also print things generated
in code but it's more work and in that casae you'll have to provide your
own preview.
However, you can print using the "Microsoft XPS Document Writer" and
then view the resulting .xps file using the XPS Viewer or Internet
Explorer (version 7 works--I don't know about earlier versions).
==========
3. New Links
http://www.vb-helper.com/links.html
First Tutors
http://www.firsttutors.co.uk
Find tutors for different subjects at various levels in the U.K.
Apex Web Visual Basic Tutorials
http://www.computer-training-software.com
Video-based training covering a wide selection of VBA, VB.Net and Visual
Basic 2005 topics. Suitable for users of all levels.
Updated Link:
Wise Owl Business Solutions
http://www.wiseowl.co.uk
Scheduled and on-site training courses in MS Office, VB, and VB.NET.
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|