Skip to content

Commit 23560fa

Browse files
committed
Adding a simple mouse example.
1 parent c0a9983 commit 23560fa

File tree

8 files changed

+293
-2
lines changed

8 files changed

+293
-2
lines changed

ConsoleGUI.MouseExample/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{97B69CAE-3F74-4DEA-86FF-929FC45D3080}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>ConsoleGUI.MouseExample</RootNamespace>
10+
<AssemblyName>ConsoleGUI.MouseExample</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="MouseHandler.cs" />
47+
<Compile Include="Program.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<ProjectReference Include="..\ConsoleGUI\ConsoleGUI.csproj">
55+
<Project>{E3D8BC25-7EA1-4F81-9C31-BCF2DCBF77A9}</Project>
56+
<Name>ConsoleGUI</Name>
57+
</ProjectReference>
58+
</ItemGroup>
59+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
60+
</Project>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using ConsoleGUI.Space;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace ConsoleGUI.MouseExample
10+
{
11+
public static class MouseHandler
12+
{
13+
private static IntPtr _inputHandle = IntPtr.Zero;
14+
private static INPUT_RECORD[] _inputBuffer;
15+
16+
public static Position MousePosition { get; private set; }
17+
18+
public static void Initialize()
19+
{
20+
_inputHandle = GetStdHandle(unchecked((uint)-10));
21+
_inputBuffer = new INPUT_RECORD[100];
22+
}
23+
24+
public static void Read()
25+
{
26+
if (_inputHandle == IntPtr.Zero)
27+
throw new InvalidOperationException("First call the Initialize method of the MouseHandler");
28+
29+
if (!ReadConsoleInput(_inputHandle, _inputBuffer, (uint)_inputBuffer.Length, out var eventsRead)) return;
30+
31+
for (int i = 0; i < eventsRead; i++)
32+
{
33+
var inputEvent = _inputBuffer[i];
34+
35+
if (inputEvent.EventType == INPUT_RECORD.MOUSE_EVENT)
36+
ProcessMouseEvent(inputEvent.MouseEvent);
37+
else
38+
WriteConsoleInput(_inputHandle, new[] { inputEvent }, 1, out var eventsWritten);
39+
}
40+
}
41+
42+
private static void ProcessMouseEvent(in MOUSE_EVENT_RECORD mouseEvent)
43+
{
44+
MousePosition = new Position(mouseEvent.dwMousePosition.X, mouseEvent.dwMousePosition.Y);
45+
}
46+
47+
private struct COORD
48+
{
49+
public short X;
50+
public short Y;
51+
}
52+
53+
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
54+
private struct KEY_EVENT_RECORD
55+
{
56+
[FieldOffset(0)]
57+
public bool bKeyDown;
58+
[FieldOffset(4)]
59+
public ushort wRepeatCount;
60+
[FieldOffset(6)]
61+
public ushort wVirtualKeyCode;
62+
[FieldOffset(8)]
63+
public ushort wVirtualScanCode;
64+
[FieldOffset(10)]
65+
public char UnicodeChar;
66+
[FieldOffset(10)]
67+
public byte AsciiChar;
68+
[FieldOffset(12)]
69+
public uint dwControlKeyState;
70+
}
71+
72+
private struct MOUSE_EVENT_RECORD
73+
{
74+
public COORD dwMousePosition;
75+
76+
public uint dwButtonState;
77+
public const uint
78+
FROM_LEFT_1ST_BUTTON_PRESSED = 0x0001,
79+
FROM_LEFT_2ND_BUTTON_PRESSED = 0x0004,
80+
FROM_LEFT_3RD_BUTTON_PRESSED = 0x0008,
81+
FROM_LEFT_4TH_BUTTON_PRESSED = 0x0010,
82+
RIGHTMOST_BUTTON_PRESSED = 0x0002;
83+
84+
public uint dwControlKeyState;
85+
public const int
86+
CAPSLOCK_ON = 0x0080,
87+
ENHANCED_KEY = 0x0100,
88+
LEFT_ALT_PRESSED = 0x0002,
89+
LEFT_CTRL_PRESSED = 0x0008,
90+
NUMLOCK_ON = 0x0020,
91+
RIGHT_ALT_PRESSED = 0x0001,
92+
RIGHT_CTRL_PRESSED = 0x0004,
93+
SCROLLLOCK_ON = 0x0040,
94+
SHIFT_PRESSED = 0x0010;
95+
96+
public uint dwEventFlags;
97+
public const int
98+
DOUBLE_CLICK = 0x0002,
99+
MOUSE_HWHEELED = 0x0008,
100+
MOUSE_MOVED = 0x0001,
101+
MOUSE_WHEELED = 0x0004;
102+
}
103+
104+
[StructLayout(LayoutKind.Explicit)]
105+
private struct INPUT_RECORD
106+
{
107+
public const ushort MOUSE_EVENT = 0x0002;
108+
109+
[FieldOffset(0)]
110+
public ushort EventType;
111+
[FieldOffset(4)]
112+
public KEY_EVENT_RECORD KeyEvent;
113+
[FieldOffset(4)]
114+
public MOUSE_EVENT_RECORD MouseEvent;
115+
}
116+
117+
[DllImport("kernel32.dll")]
118+
public static extern IntPtr GetStdHandle(uint nStdHandle);
119+
120+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
121+
private static extern bool ReadConsoleInput(IntPtr hConsoleInput, [Out] INPUT_RECORD[] lpBuffer, uint nLength, out uint lpNumberOfEventsRead);
122+
123+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
124+
private static extern bool WriteConsoleInput(IntPtr hConsoleInput, INPUT_RECORD[] lpBuffer, uint nLength, out uint lpNumberOfEventsWritten);
125+
}
126+
}

ConsoleGUI.MouseExample/Program.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using ConsoleGUI.Controls;
2+
using ConsoleGUI.Input;
3+
using ConsoleGUI.Space;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace ConsoleGUI.MouseExample
12+
{
13+
class Program
14+
{
15+
static void Main()
16+
{
17+
MouseHandler.Initialize();
18+
19+
ConsoleManager.Setup();
20+
ConsoleManager.CompatibilityMode = true;
21+
ConsoleManager.DontPrintTheLastCharacter = true;
22+
ConsoleManager.Resize(new Size(150, 40));
23+
24+
var textBox = new TextBox { Text = "Hello world" };
25+
var textBlock = new TextBlock { Text = "Hello world" };
26+
var button = new Button { Content = new Margin { Offset = new Offset(4, 1, 4, 1), Content = new TextBlock { Text = "Button" } } };
27+
28+
ConsoleManager.Content = new VerticalStackPanel
29+
{
30+
Children = new IControl[]
31+
{
32+
textBox,
33+
textBlock,
34+
new Box { Content = button }
35+
}
36+
};
37+
38+
var inputs = new IInputListener[]
39+
{
40+
textBox
41+
};
42+
43+
while (true)
44+
{
45+
MouseHandler.Read();
46+
ConsoleManager.ReadInput(inputs);
47+
ConsoleManager.OnMouseMove(MouseHandler.MousePosition);
48+
49+
textBlock.Text = $"({MouseHandler.MousePosition.X}, {MouseHandler.MousePosition.Y})";
50+
51+
Thread.Sleep(50);
52+
}
53+
}
54+
}
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ConsoleGUI.MouseExample")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ConsoleGUI.MouseExample")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("97b69cae-3f74-4dea-86ff-929fc45d3080")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

ConsoleGUI.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleGUI.Example", "Conso
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleGUI", "ConsoleGUI\ConsoleGUI.csproj", "{E3D8BC25-7EA1-4F81-9C31-BCF2DCBF77A9}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleGUI.Test", "ConsoleGUI.Test\ConsoleGUI.Test.csproj", "{ABC28308-500E-48E9-AD7E-AA8977370D35}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleGUI.Test", "ConsoleGUI.Test\ConsoleGUI.Test.csproj", "{ABC28308-500E-48E9-AD7E-AA8977370D35}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleGUI.MouseExample", "ConsoleGUI.MouseExample\ConsoleGUI.MouseExample.csproj", "{97B69CAE-3F74-4DEA-86FF-929FC45D3080}"
1113
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +29,10 @@ Global
2729
{ABC28308-500E-48E9-AD7E-AA8977370D35}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{ABC28308-500E-48E9-AD7E-AA8977370D35}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{ABC28308-500E-48E9-AD7E-AA8977370D35}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{97B69CAE-3F74-4DEA-86FF-929FC45D3080}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{97B69CAE-3F74-4DEA-86FF-929FC45D3080}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{97B69CAE-3F74-4DEA-86FF-929FC45D3080}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{97B69CAE-3F74-4DEA-86FF-929FC45D3080}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

ConsoleGUI/ConsoleManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ private static MouseContext? MouseContext
9393
{
9494
value.Value.MouseListener.OnMouseMove(value.Value.RelativePosition);
9595
}
96+
97+
_mouseContext = value;
9698
}
9799
}
98100

ConsoleGUI/Controls/Button.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public IControl Content
2929
.Then(BindContent);
3030
}
3131

32-
private Color _mouseOverColor = new Color(150, 150, 200);
32+
private Color _mouseOverColor = new Color(50, 50, 100);
3333
public Color MouseOverColor
3434
{
3535
get => _mouseOverColor;

0 commit comments

Comments
 (0)