Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ramos committed Jun 15, 2022
1 parent 1d0fa33 commit 98cf594
Show file tree
Hide file tree
Showing 17 changed files with 392 additions and 0 deletions.
15 changes: 15 additions & 0 deletions App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Application
x:Class="PrintSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PrintSample">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
</ResourceDictionary>
</Application.Resources>
</Application>
51 changes: 51 additions & 0 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace PrintSample
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
}

private Window m_window;
}
}
Binary file added Assets/AusterNY.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/LockScreenLogo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/SplashScreen.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/Square150x150Logo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/Square44x44Logo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/StoreLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/Wide310x150Logo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Window
x:Class="PrintSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PrintSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
RowDefinitions =" Auto,*">
<Button HorizontalAlignment="Center" Click="PrintButtonClick">Print</Button>
<Rectangle x:Name="RectangleToPrint"
Grid.Row="1"
Width="500"
Height="500">
<Rectangle.Fill>
<ImageBrush ImageSource="Assets/AusterNY.jpg" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
148 changes: 148 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Printing;
using System;
using Windows.Graphics.Printing;

namespace PrintSample
{
public sealed partial class MainWindow : Window
{
private PrintManager printMan;
private PrintDocument printDoc;
private IPrintDocumentSource printDocSource;
public MainWindow()
{
this.InitializeComponent();
RegisterPrint();
}

#region Register for printing

void RegisterPrint()
{
// Register for PrintTaskRequested event
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

printMan = PrintManagerInterop.GetForWindow(hWnd);
printMan.PrintTaskRequested += PrintTaskRequested;

// Build a PrintDocument and register for callbacks
printDoc = new PrintDocument();
printDocSource = printDoc.DocumentSource;
printDoc.Paginate += Paginate;
printDoc.GetPreviewPage += GetPreviewPage;
printDoc.AddPages += AddPages;
}

#endregion

#region Showing the print dialog

private async void PrintButtonClick(object sender, RoutedEventArgs e)
{
if (PrintManager.IsSupported())
{
try
{
// Show print UI
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd);
}
catch
{
// Printing cannot proceed at this time
ContentDialog noPrintingDialog = new ContentDialog()
{
XamlRoot = (sender as Button).XamlRoot,
Title = "Printing error",
Content = "\nSorry, printing can' t proceed at this time.",
PrimaryButtonText = "OK"
};
await noPrintingDialog.ShowAsync();
}
}
else
{
// Printing is not supported on this device
ContentDialog noPrintingDialog = new ContentDialog()
{
XamlRoot = (sender as Button).XamlRoot,
Title = "Printing not supported",
Content = "\nSorry, printing is not supported on this device.",
PrimaryButtonText = "OK"
};
await noPrintingDialog.ShowAsync();
}
}

private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequrested);

// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompleted;
}

private void PrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}

#endregion

#region Print preview

private void Paginate(object sender, PaginateEventArgs e)
{
// As I only want to print one Rectangle, so I set the count to 1
printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}

private void GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
printDoc.SetPreviewPage(e.PageNumber, this.RectangleToPrint);
}

#endregion

#region Add pages to send to the printer

private void AddPages(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(this.RectangleToPrint);

// Indicate that all of the print pages have been provided
printDoc.AddPagesComplete();
}

#endregion

#region Print task completed

private void PrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
this.DispatcherQueue.TryEnqueue(async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
XamlRoot = this.Content.XamlRoot,
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
await noPrintingDialog.ShowAsync();
});
}
}

#endregion
}
}
48 changes: 48 additions & 0 deletions Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>

<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">

<Identity
Name="25e13cae-9bce-442f-a8b9-473610c2ebc0"
Publisher="CN=migue"
Version="1.0.0.0" />

<Properties>
<DisplayName>PrintSample</DisplayName>
<PublisherDisplayName>migue</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
</Dependencies>

<Resources>
<Resource Language="x-generate"/>
</Resources>

<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="PrintSample"
Description="PrintSample"
BackgroundColor="transparent"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>

<Capabilities>
<rescap:Capability Name="runFullTrust" />
</Capabilities>
</Package>
40 changes: 40 additions & 0 deletions PrintSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>PrintSample</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
</PropertyGroup>
<ItemGroup>
<None Remove="Assets\AusterNY.jpg" />
</ItemGroup>

<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

<!-- Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored -->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnablePreviewMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
</Project>
43 changes: 43 additions & 0 deletions PrintSample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32505.426
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrintSample", "PrintSample.csproj", "{03182683-C013-4B6B-8CA2-86E37F0C92F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|arm64 = Debug|arm64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|arm64 = Release|arm64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|arm64.ActiveCfg = Debug|arm64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|arm64.Build.0 = Debug|arm64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|arm64.Deploy.0 = Debug|arm64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|x64.ActiveCfg = Debug|x64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|x64.Build.0 = Debug|x64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|x64.Deploy.0 = Debug|x64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|x86.ActiveCfg = Debug|x86
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|x86.Build.0 = Debug|x86
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Debug|x86.Deploy.0 = Debug|x86
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|arm64.ActiveCfg = Release|arm64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|arm64.Build.0 = Release|arm64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|arm64.Deploy.0 = Release|arm64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|x64.ActiveCfg = Release|x64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|x64.Build.0 = Release|x64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|x64.Deploy.0 = Release|x64
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|x86.ActiveCfg = Release|x86
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|x86.Build.0 = Release|x86
{03182683-C013-4B6B-8CA2-86E37F0C92F6}.Release|x86.Deploy.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {34B2169B-5785-443A-BC66-A5676586D921}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"PrintSample (Package)": {
"commandName": "MsixPackage"
},
"PrintSample (Unpackaged)": {
"commandName": "Project"
}
}
}
Loading

0 comments on commit 98cf594

Please sign in to comment.