123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace ConFrames
- {
- public class Desktop
- {
- public Desktop(int width, int height)
- {
-
- _stdout = Interop.GetStdHandle(Interop.STD_OUTPUT_HANDLE);
-
-
- _buffer = Interop.CreateConsoleScreenBuffer(Interop.GENERIC_READWRITE, (uint)0, IntPtr.Zero, Interop.CONSOLE_TEXTMODE_BUFFER, IntPtr.Zero);
-
- ActiveBorderBackgroundColor = ConsoleColor.Blue;
- ActiveBorderLineColor = ConsoleColor.White;
- InactiveBorderLineColor = ConsoleColor.Gray;
- InactiveBorderBackgroundColor = ConsoleColor.DarkBlue;
-
- DesktopSize = new Size(width, height);
- DesktopColor = ConsoleColor.DarkBlue;
- }
-
- IntPtr _stdout;
- IntPtr _buffer;
-
- Size _desktopSize;
- public Size DesktopSize
- {
- get { return _desktopSize; }
- set
- {
- try
- {
-
- Interop.SetBufferAndScreenSize(_buffer, (short)value.Width, (short)value.Height);
-
- _desktopSize = value;
- }
- catch
- {
- try
- {
-
- Interop.SetBufferAndScreenSize(_buffer, (short)_desktopSize.Width, (short)_desktopSize.Height);
- }
- catch { }
- throw;
- }
- }
- }
-
- List<Window> _windows = new List<Window>();
-
- internal void AddWindow(Window window)
- {
- if (_windows.IndexOf(window)<0)
- {
- _windows.Add(window);
- InvalidateDesktop();
- }
- }
-
- internal void RemoveWindow(Window window)
- {
- _windows.Remove(window);
- InvalidateDesktop();
- }
-
- public ConsoleColor ActiveBorderBackgroundColor
- {
- get;
- set;
- }
- public ConsoleColor ActiveBorderLineColor
- {
- get;
- set;
- }
- public ConsoleColor InactiveBorderBackgroundColor
- {
- get;
- set;
- }
- public ConsoleColor InactiveBorderLineColor
- {
- get;
- set;
- }
-
- ConsoleColor _desktopColor;
- public ConsoleColor DesktopColor
- {
- get
- {
- return _desktopColor;
- }
- set
- {
- _desktopColor = value;
- InvalidateDesktop();
- }
- }
-
- protected virtual void OnWillUpdate()
- {
- }
-
- protected virtual void OnDidUpdate()
- {
- }
-
- protected virtual void OnEnterProcessing()
- {
- }
-
- protected virtual void OnLeaveProcessing()
- {
- }
-
- protected virtual bool OnPreviewKey(ConsoleKeyInfo key)
- {
- if (PreviewKey != null)
- return PreviewKey(key);
- return false;
- }
-
- public Func<ConsoleKeyInfo, bool> PreviewKey;
-
- public Window ActiveWindow
- {
- get { return _windows.Count == 0 ? null : _windows[_windows.Count - 1]; }
- set
- {
- var oldActive = ActiveWindow;
- int pos = _windows.IndexOf(value);
- if (pos < _windows.Count-1)
- {
- _windows.RemoveAt(pos);
- _windows.Add(value);
- }
- if (oldActive!=ActiveWindow)
- {
- Invalidate(oldActive);
- Invalidate(ActiveWindow);
- }
- }
- }
-
- bool _needRedraw = false;
- bool _needClear = false;
- public void Invalidate(Window w)
- {
- _needRedraw = true;
- }
- public void InvalidateDesktop()
- {
- _needClear = true;
- _needRedraw = true;
- }
-
- public void Update()
- {
-
- if (_needRedraw)
- {
-
- OnWillUpdate();
-
- _needRedraw = false;
-
- if (_needClear)
- {
- _needClear = false;
-
- Interop.CONSOLE_SCREEN_BUFFER_INFO info;
- Interop.GetConsoleScreenBufferInfo(_buffer, out info);
-
- CharInfo[] buf = new CharInfo[info.dwSize.X * info.dwSize.Y];
-
- var defAttributes = (ushort)((ushort)0 | ((ushort)_desktopColor << 4));
- for (int i = 0; i < buf.Length; ++i)
- {
- buf[i].Attributes = defAttributes;
- buf[i].Char = (char)' ';
- }
-
- var r = new Interop.SmallRect()
- {
- Top = (short)0,
- Left = (short)0,
- Right = (short)info.dwSize.X,
- Bottom = (short)info.dwSize.Y,
- };
- Interop.WriteConsoleOutput(_buffer,
- buf,
- new Interop.Coord() { X = (short)info.dwSize.X, Y = info.dwSize.Y },
- new Interop.Coord() { X = 0, Y = 0 },
- ref r);
- }
-
- foreach (var w in _windows)
- {
- var buf = w.Draw();
- var r = new Interop.SmallRect()
- {
- Top = (short)w.FrameRectangle.Top,
- Left = (short)w.FrameRectangle.Left,
- Right = (short)w.FrameRectangle.Right,
- Bottom = (short)w.FrameRectangle.Bottom,
- };
- Interop.WriteConsoleOutput(_buffer,
- buf,
- new Interop.Coord() { X = (short)w.FrameRectangle.Width, Y = (short)w.FrameRectangle.Height },
- new Interop.Coord() { X = 0, Y = 0 },
- ref r);
- }
-
- OnDidUpdate();
- }
-
- var active = ActiveWindow;
- if (active != null)
- {
- if (active.CursorPosition.X >= 0 && active.CursorPosition.Y >= 0 &&
- active.CursorPosition.X < active.FrameRectangle.Width - 2 &&
- active.CursorPosition.Y < active.FrameRectangle.Height - 2)
- {
- Interop.SetConsoleCursorPosition(_buffer, new Interop.Coord(
- (short)(active.FrameRectangle.Left + active.CursorPosition.X + 1),
- (short)(active.FrameRectangle.Top + active.CursorPosition.Y + 1)
- ));
- Interop.SetConsoleCursorVisible(_buffer, active.CursorVisible);
- }
- else
- {
- Interop.SetConsoleCursorVisible(_buffer, false);
- }
- }
- else
- {
- Interop.SetConsoleCursorVisible(_buffer, false);
- }
- }
-
- bool _continueProcessing = false;
- public void EndProcessing()
- {
- _continueProcessing = false;
- }
-
- public void Process()
- {
-
- OnEnterProcessing();
-
- ViewMode = ViewMode.Desktop;
-
- _continueProcessing = true;
- while (_continueProcessing)
- {
-
- Update();
-
- BringToFront();
-
- var key = Console.ReadKey(true);
-
- if (key.Key == ConsoleKey.Tab)
- {
- if (key.Modifiers == ConsoleModifiers.Control)
- {
- if (_windows.Any())
- {
- var top = _windows[_windows.Count - 1];
- _windows.RemoveAt(_windows.Count-1);
- _windows.Insert(0, top);
- _needRedraw = true;
- }
- continue;
- }
- if (key.Modifiers == (ConsoleModifiers.Control | ConsoleModifiers.Shift))
- {
- if (_windows.Any())
- {
- var top = _windows[0];
- _windows.RemoveAt(0);
- _windows.Add(top);
- _needRedraw = true;
- }
- continue;
- }
- }
-
- if (key.Key == ConsoleKey.F4 && key.Modifiers == 0)
- {
- ViewMode = ViewMode.StdOut;
- Console.ReadKey(true);
- ViewMode = ViewMode.Desktop;
- continue;
- }
-
- if (OnPreviewKey(key))
- continue;
-
- var aw = ActiveWindow;
- if (aw!= null)
- {
- aw.OnKey(key);
- }
- }
-
- OnLeaveProcessing();
-
- return;
- }
-
- IntPtr _oldForegroundWindow;
- public void BringToFront()
- {
- _oldForegroundWindow = Interop.GetActiveWindow();
- Interop.SetForegroundWindow(Interop.GetConsoleWindow());
- }
-
- public void RestoreForegroundWindow()
- {
- if (_oldForegroundWindow==IntPtr.Zero)
- {
- _oldForegroundWindow = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
- }
- if (_oldForegroundWindow !=IntPtr.Zero)
- {
- Interop.SetForegroundWindow(_oldForegroundWindow);
- _oldForegroundWindow = IntPtr.Zero;
- }
- }
-
- ViewMode _viewMode = ViewMode.StdOut;
- public ViewMode ViewMode
- {
- get
- {
- return _viewMode;
- }
- set
- {
- if (_viewMode !=value)
- {
- _viewMode = value;
- if (_viewMode==ViewMode.Desktop)
- {
- Interop.SetConsoleActiveScreenBuffer(_buffer);
- }
- else
- {
- Interop.SetConsoleActiveScreenBuffer(_stdout);
- }
- }
- }
- }
- }
- }
|