ntwain/Tests/Tester.WPF/MainWindow.xaml.cs

141 lines
4.1 KiB
C#
Raw Normal View History

2014-04-21 04:57:38 +08:00
using GalaSoft.MvvmLight.Messaging;
using ModernWPF.Controls;
using NTwain;
using NTwain.Data;
using System;
2014-11-15 11:52:07 +08:00
using System.Collections;
2014-04-21 04:57:38 +08:00
using System.ComponentModel;
using System.Diagnostics;
2014-04-03 07:01:21 +08:00
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
2014-04-21 04:57:38 +08:00
using System.Windows.Media.Imaging;
2014-04-03 07:01:21 +08:00
namespace Tester.WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TwainVM _twainVM;
2014-04-03 07:01:21 +08:00
public MainWindow()
{
InitializeComponent();
2014-04-12 23:22:05 +08:00
if (!DesignerProperties.GetIsInDesignMode(this))
{
2014-11-14 10:49:12 +08:00
if (PlatformInfo.Current.IsApp64Bit)
2014-09-25 09:52:28 +08:00
{
Title = Title + " (64bit)";
}
else
{
Title = Title + " (32bit)";
}
_twainVM = new TwainVM();
this.DataContext = _twainVM;
2014-04-12 23:22:05 +08:00
Messenger.Default.Register<DialogMessage>(this, msg =>
{
2014-04-15 07:30:25 +08:00
if (Dispatcher.CheckAccess())
{
ModernMessageBox.Show(this, msg.Content, msg.Caption, msg.Button, msg.Icon, msg.DefaultResult);
}
else
{
Dispatcher.BeginInvoke(new Action(() =>
{
ModernMessageBox.Show(this, msg.Content, msg.Caption, msg.Button, msg.Icon, msg.DefaultResult);
}));
}
2014-04-12 23:22:05 +08:00
});
}
2014-04-03 07:01:21 +08:00
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = _twainVM.State > 4;
base.OnClosing(e);
}
protected override void OnClosed(EventArgs e)
{
if (_twainVM.State == 4)
{
2014-05-20 19:25:57 +08:00
_twainVM.CurrentSource.Close();
}
_twainVM.Close();
base.OnClosed(e);
}
2014-04-03 07:01:21 +08:00
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// use this for internal msg loop
//var rc = _twainVM.Open();
2014-09-25 09:52:28 +08:00
// use this to hook into current app loop
var rc = _twainVM.Open(new WpfMessageLoopHook(new WindowInteropHelper(this).Handle));
if (rc == ReturnCode.Success)
{
SrcList.ItemsSource = _twainVM.Select(s => new DSVM { DS = s });
}
2014-04-03 07:01:21 +08:00
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_twainVM.TestCapture(new WindowInteropHelper(this).Handle);
}
2014-04-03 07:01:21 +08:00
private void SrcList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_twainVM.State == 4)
2014-04-03 07:01:21 +08:00
{
2014-05-20 19:25:57 +08:00
_twainVM.CurrentSource.Close();
}
2014-04-03 07:01:21 +08:00
var dsId = SrcList.SelectedItem as DSVM;
if (dsId != null)
{
2014-11-15 12:09:12 +08:00
dsId.Open();
}
}
2014-04-08 07:46:03 +08:00
private void CapList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var capVM = CapList.SelectedItem as CapVM;
if (capVM != null)
{
2014-11-15 11:52:07 +08:00
CapDetailList.ItemsSource = capVM.Get();
CapDetailList.SelectedItem = capVM.GetCurrent();
2014-04-03 07:01:21 +08:00
}
else
{
CapDetailList.ItemsSource = null;
2014-04-03 07:01:21 +08:00
}
}
private void CapDetailList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
2014-11-15 11:52:07 +08:00
var capVM = CapList.SelectedItem as CapVM;
if (capVM != null)
{
if (capVM.Supports.HasFlag(QuerySupports.Set))
{
try
{
capVM.Set(CapDetailList.SelectedItem);
}
catch (Exception ex)
{
ModernMessageBox.Show(this, ex.Message, "Cannot Set", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
2014-04-03 07:01:21 +08:00
}
}
}