//----------------------------------------------------
// PrintaBunchaButtons.cs (c) 2006 by Charles Petzold
//----------------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Petzold.PrintaBunchaButtons
{
public class PrintaBunchaButtons : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new PrintaBunchaButtons());
}
public PrintaBunchaButtons()
{
Title = "Tisk spousty tlacitek";
SizeToContent = SizeToContent.WidthAndHeight;
ResizeMode = ResizeMode.CanMinimize;
// Vytvori tlacitko 'Tisk'.
Button btn = new Button();
btn.FontSize = 24;
btn.Content = "Tisk ...";
btn.Padding = new Thickness(12);
btn.Margin = new Thickness(96);
btn.Click += PrintOnClick;
Content = btn;
}
void PrintOnClick(object sender, RoutedEventArgs args)
{
PrintDialog dlg = new PrintDialog();
if ((bool)dlg.ShowDialog().GetValueOrDefault())
{
// Vytvori panel Grid.
Grid grid = new Grid();
// Definuje pet radku a sloupcu s automatickou velikosti.
for (int i = 0; i < 5; i++)
{
ColumnDefinition coldef = new ColumnDefinition();
coldef.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(coldef);
RowDefinition rowdef = new RowDefinition();
rowdef.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowdef);
}
// Nastavi pro panel Grid gradientni stetec.
grid.Background =
new LinearGradientBrush(Colors.Gray, Colors.White,
new Point(0, 0), new Point(1, 1));
// Kazdy program potrebuje trochu nahodnosti.
Random rand = new Random();
// Umisti na panel Grid 25 tlacitek.
for (int i = 0; i < 25; i++)
{
Button btn = new Button();
btn.FontSize = 12 + rand.Next(8);
btn.Content = "Tlacitko c. " + (i + 1);
btn.HorizontalAlignment = HorizontalAlignment.Center;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.Margin = new Thickness(6);
grid.Children.Add(btn);
Grid.SetRow(btn, i % 5);
Grid.SetColumn(btn, i / 5);
}
// Nastavi velikost panelu Grid.
grid.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
Size sizeGrid = grid.DesiredSize;
// Urci bod pro vystredeni panelu Grid na strance.
Point ptGrid =
new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2,
(dlg.PrintableAreaHeight - sizeGrid.Height) / 2);
// Rozlozeni je hotovo.
grid.Arrange(new Rect(ptGrid, sizeGrid));
// Nyni se vytiskne.
dlg.PrintVisual(grid, Title);
}
}
}
}