Thursday, October 29, 2009

Simple Print Window in WPF (PDF and DOC)

I struggled with this for a few hours because the window would not close when the application closed using a WindowsFormHost and the solution was tons simpilar than I thought to begin with.

You make a window:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowState="Maximized"
Icon="/MyProj;component/Images/favicon.ico"
Title="MyProj - Print">





Then the code behind is simple:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MyProj
{
///
/// Interaction logic for PrintPDFWindow.xaml
///

public partial class PrintPDFWindow : Window
{
private System.Windows.Forms.WebBrowser webbrowserOne;
public PrintPDFWindow()
{
InitializeComponent();
}
public PrintPDFWindow(string filePath)
{
InitializeComponent();
Uri uri = new Uri(filePath, UriKind.RelativeOrAbsolute);
this.webBrowser.Source = uri;
}
}
}


You call it like this:

PrintPDFWindow ppw = new PrintPDFWindow(currentMarketingPath + currentMarketingMaterial.MarketingMaterialURL);
ppw.Title = "MyProj - " + currentMarketingMaterial.MarketingMaterialName;
ppw.Show();


That is it!

Friday, October 16, 2009

WPF Horizontal Listbox Bad Keyboard Behavior

What a pain in the arse. Took me hours to find this.

I have a horizontal listbox resembling a filmstrip. It had images on it. A mouse click on the next item in the scrollbar produced a correct onselectionchanged event. Arrowing right or left did not product the correct selected index. An Arrow Key Right it went to index 0. An Arrow Key Left went to the last item in the list.

I overrode the listbox class with the following code:

public class HorizontalListBox : ListBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
KeyDownHandler(this, e);
e.Handled = true;
}
void KeyDownHandler(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Left)
{
MoveUp();
}
if (e.Key == System.Windows.Input.Key.Right)
{
MoveDown();
}
}
void MoveUp()
{
if (this.SelectedIndex > 0)
{
this.SelectedIndex = this.SelectedIndex - 1;
}
}
void MoveDown()
{
try
{
this.SelectedIndex = this.SelectedIndex + 1;
}
catch { }
}
}

Happy coding!
Linda

Tuesday, October 13, 2009

WPF Application Icon when StartURL is a Page

Create an LoadCompleted event on the application (App.xaml).

LoadCompleted="Application_LoadCompleted"

private void Application_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{

System.Drawing.Bitmap bmp = global::Docs.Properties.Resources.favicon.ToBitmap();
MemoryStream strm = new MemoryStream();
bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
strm.Seek(0, SeekOrigin.Begin);
PngBitmapDecoder pbd = new PngBitmapDecoder(strm, BitmapCreateOptions.None, BitmapCacheOption.Default);
//((System.Windows.Navigation.NavigationWindow)(Application.Current.MainWindow)).Icon = pbd.Frames[0];
Application.Current.MainWindow.Icon = pbd.Frames[0];
Application.Current.MainWindow.Top = 0;
Application.Current.MainWindow.Left = 0;
}

Took me a couple of hours to find this one.
~Linda