ConsoleWindow.xaml.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Interop;
  4. using System.Windows.Media;
  5. namespace NTMiner.Views {
  6. public partial class ConsoleWindow : Window {
  7. public static ConsoleWindow Instance { get; private set; } = new ConsoleWindow();
  8. private IntPtr _thisWindowHandle;
  9. private HwndSource hwndSource;
  10. private ConsoleWindow() {
  11. this.Width = AppRoot.MainWindowWidth;
  12. this.Height = AppRoot.MainWindowHeight;
  13. InitializeComponent();
  14. this.Loaded += (sender, e) => {
  15. _thisWindowHandle = new WindowInteropHelper(this).Handle;
  16. IntPtr console = NTMinerConsole.GetOrAlloc();
  17. SafeNativeMethods.SetWindowLong(console, SafeNativeMethods.GWL_STYLE, SafeNativeMethods.WS_VISIBLE);
  18. SafeNativeMethods.SetParent(console, _thisWindowHandle);
  19. hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
  20. hwndSource.AddHook(new HwndSourceHook(Win32Proc.WindowProc));
  21. };
  22. 2.SecondsDelay().ContinueWith(t => {
  23. UIThread.Execute(() => {
  24. this.TbDescription.Visibility = Visibility.Visible;
  25. });
  26. });
  27. }
  28. protected override void OnClosed(EventArgs e) {
  29. hwndSource?.Dispose();
  30. hwndSource = null;
  31. base.OnClosed(e);
  32. }
  33. private int _marginLeft, _marginTop, _height, _width;
  34. public void MoveWindow(int marginLeft, int marginTop, int width, int height) {
  35. if (!this.IsLoaded) {
  36. return;
  37. }
  38. if (width < 0) {
  39. width = 0;
  40. }
  41. if (_marginLeft == marginLeft && _marginTop == marginTop && _height == height && _width == width) {
  42. return;
  43. }
  44. var rect = Microsoft.Windows.Shell.Standard.DpiHelper.LogicalRectToDevice(new Rect(marginLeft, marginTop, width, height));
  45. marginLeft = (int)rect.Left;
  46. marginTop = (int)rect.Top;
  47. width = (int)rect.Width;
  48. height = (int)rect.Height;
  49. _marginLeft = marginLeft;
  50. _marginTop = marginTop;
  51. _height = height;
  52. _width = width;
  53. NTMinerConsole.MoveWindow(marginLeft, marginTop, width, height, true);
  54. }
  55. }
  56. }