diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d1e85c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.sdf +*.suo + +Debug/* +Release/* +MinesweeperForms/Debug/* +MinesweeperForms/Release/* \ No newline at end of file diff --git a/MainFrm.cpp b/MainFrm.cpp new file mode 100644 index 0000000..67189f1 --- /dev/null +++ b/MainFrm.cpp @@ -0,0 +1,123 @@ +/* Copyright (c) 2020 Dreamy Cecil +This program is free software; you can redistribute it and/or modify +it under the terms of version 2 of the GNU General Public License as published by +the Free Software Foundation + + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +#include "MainFrm.h" +#include + +using namespace System; +using namespace System::Windows::Forms; + +static bool _bTrapped = false; + +// Hotkey +#define HOTKEY (GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState(VK_F11) & 0x8000) + +static bool _bKeyPressed = false; +static bool _bKeyHolding = false; +static bool _bKeyReleased = false; + +// Get limits for trapping +bool MouseTrapper::MainFrm::GetLimits(RECT &rcClip) { + try { + int iX1 = Int32::Parse(txtX1->Text); + int iY1 = Int32::Parse(txtY1->Text); + int iX2 = Int32::Parse(txtX2->Text); + int iY2 = Int32::Parse(txtY2->Text); + + // Fix invalid horizontal limits + if (iX1 >= iX2) { + iX2 = iX1+1; + + std::ostringstream strPos; + strPos << iX2; + + txtX2->Text = %String(strPos.str().c_str()); + } + + // Fix invalid vertical limits + if (iY1 >= iY2) { + iY2 = iY1+1; + + std::ostringstream strPos; + strPos << iY2; + + txtY2->Text = %String(strPos.str().c_str()); + } + + rcClip.left = iX1; + rcClip.top = iY1; + rcClip.right = iX2; + rcClip.bottom = iY2; + + } catch (FormatException ^) { + MessageBox::Show("One of the limits doesn't contain a number!", "Incorrect limits", MessageBoxButtons::OK, MessageBoxIcon::Error); + + _bTrapped = false; + return false; + } + + return true; +}; + +// Trap or untrap +Void MouseTrapper::MainFrm::btnTrap_Click(Object ^sender, EventArgs ^e) { + _bTrapped = !_bTrapped; +}; + +// Trapper loop +Void MouseTrapper::MainFrm::tmLoop_Tick(Object ^sender, EventArgs ^e) { + _bKeyPressed = (HOTKEY && !_bKeyHolding); + _bKeyReleased = (!HOTKEY && _bKeyHolding); + + _bKeyHolding = HOTKEY; + + if (_bKeyPressed) { + btnTrap_Click(sender, e); + } + + // Trap the mouse + if (_bTrapped) { + RECT rcClip; + + if (GetLimits(rcClip)) { + btnTrap->Text = "UNTRAP"; + ClipCursor(&rcClip); + } + + } else { + btnTrap->Text = "TRAP"; + ClipCursor(NULL); + } + + // Print mouse position + POINT ptMouse; + + if (GetCursorPos(&ptMouse)) { + std::ostringstream strMouse; + strMouse << "Mouse position: " << ptMouse.x << ", " << ptMouse.y; + + lblMouse->Text = %String(strMouse.str().c_str()); + } +}; + +[STAThreadAttribute] +void Main(array ^args) +{ + Application::EnableVisualStyles(); + Application::SetCompatibleTextRenderingDefault(false); + + MouseTrapper::MainFrm frm; + Application::Run(%frm); +}; \ No newline at end of file diff --git a/MainFrm.h b/MainFrm.h new file mode 100644 index 0000000..24d0041 --- /dev/null +++ b/MainFrm.h @@ -0,0 +1,216 @@ +/* Copyright (c) 2020 Dreamy Cecil +This program is free software; you can redistribute it and/or modify +it under the terms of version 2 of the GNU General Public License as published by +the Free Software Foundation + + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + +#pragma once + +#include + +namespace MouseTrapper { + using namespace System; + using namespace System::ComponentModel; + using namespace System::Collections; + using namespace System::Windows::Forms; + using namespace System::Data; + using namespace System::Drawing; + + /// + /// Summary for MainFrm + /// + public ref class MainFrm : public System::Windows::Forms::Form + { + public: + MainFrm(void) { + InitializeComponent(); + }; + + // Get mouse limits + bool GetLimits(RECT &rcClip); + + protected: + ~MainFrm() { + if (components) { + delete components; + } + }; + + private: System::Windows::Forms::TextBox^ txtX1; + private: System::Windows::Forms::TextBox^ txtY1; + private: System::Windows::Forms::TextBox^ txtX2; + private: System::Windows::Forms::TextBox^ txtY2; + private: System::Windows::Forms::Label^ lblTopLeft; + private: System::Windows::Forms::Label^ lblBottomRight; + private: System::Windows::Forms::Button^ btnTrap; + private: System::Windows::Forms::Label^ lblShortcut; + private: System::Windows::Forms::Timer^ tmLoop; + private: System::Windows::Forms::Label^ lblMouse; + private: System::ComponentModel::IContainer^ components; + +#pragma region Windows Form Designer generated code + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + void InitializeComponent(void) + { + this->components = (gcnew System::ComponentModel::Container()); + this->txtX1 = (gcnew System::Windows::Forms::TextBox()); + this->txtY1 = (gcnew System::Windows::Forms::TextBox()); + this->txtX2 = (gcnew System::Windows::Forms::TextBox()); + this->txtY2 = (gcnew System::Windows::Forms::TextBox()); + this->lblTopLeft = (gcnew System::Windows::Forms::Label()); + this->lblBottomRight = (gcnew System::Windows::Forms::Label()); + this->btnTrap = (gcnew System::Windows::Forms::Button()); + this->lblShortcut = (gcnew System::Windows::Forms::Label()); + this->tmLoop = (gcnew System::Windows::Forms::Timer(this->components)); + this->lblMouse = (gcnew System::Windows::Forms::Label()); + this->SuspendLayout(); + // + // txtX1 + // + this->txtX1->Font = (gcnew System::Drawing::Font(L"Segoe UI Semibold", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->txtX1->Location = System::Drawing::Point(12, 33); + this->txtX1->MaxLength = 10; + this->txtX1->Name = L"txtX1"; + this->txtX1->Size = System::Drawing::Size(128, 29); + this->txtX1->TabIndex = 0; + this->txtX1->Text = L"0"; + // + // txtY1 + // + this->txtY1->Font = (gcnew System::Drawing::Font(L"Segoe UI Semibold", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->txtY1->Location = System::Drawing::Point(12, 68); + this->txtY1->MaxLength = 10; + this->txtY1->Name = L"txtY1"; + this->txtY1->Size = System::Drawing::Size(128, 29); + this->txtY1->TabIndex = 1; + this->txtY1->Text = L"0"; + // + // txtX2 + // + this->txtX2->Font = (gcnew System::Drawing::Font(L"Segoe UI Semibold", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->txtX2->Location = System::Drawing::Point(164, 33); + this->txtX2->MaxLength = 10; + this->txtX2->Name = L"txtX2"; + this->txtX2->Size = System::Drawing::Size(128, 29); + this->txtX2->TabIndex = 2; + this->txtX2->Text = L"640"; + // + // txtY2 + // + this->txtY2->Font = (gcnew System::Drawing::Font(L"Segoe UI Semibold", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->txtY2->Location = System::Drawing::Point(164, 68); + this->txtY2->MaxLength = 10; + this->txtY2->Name = L"txtY2"; + this->txtY2->Size = System::Drawing::Size(128, 29); + this->txtY2->TabIndex = 3; + this->txtY2->Text = L"480"; + // + // lblTopLeft + // + this->lblTopLeft->AutoSize = true; + this->lblTopLeft->Font = (gcnew System::Drawing::Font(L"Segoe UI Semibold", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->lblTopLeft->Location = System::Drawing::Point(12, 9); + this->lblTopLeft->Name = L"lblTopLeft"; + this->lblTopLeft->Size = System::Drawing::Size(66, 21); + this->lblTopLeft->TabIndex = 1; + this->lblTopLeft->Text = L"Top left"; + // + // lblBottomRight + // + this->lblBottomRight->AutoSize = true; + this->lblBottomRight->Font = (gcnew System::Drawing::Font(L"Segoe UI Semibold", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->lblBottomRight->Location = System::Drawing::Point(194, 9); + this->lblBottomRight->Name = L"lblBottomRight"; + this->lblBottomRight->Size = System::Drawing::Size(105, 21); + this->lblBottomRight->TabIndex = 1; + this->lblBottomRight->Text = L"Bottom right"; + // + // btnTrap + // + this->btnTrap->Font = (gcnew System::Drawing::Font(L"Segoe UI", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->btnTrap->Location = System::Drawing::Point(12, 181); + this->btnTrap->Name = L"btnTrap"; + this->btnTrap->Size = System::Drawing::Size(280, 40); + this->btnTrap->TabIndex = 4; + this->btnTrap->Text = L"TRAP"; + this->btnTrap->UseVisualStyleBackColor = true; + this->btnTrap->Click += gcnew System::EventHandler(this, &MainFrm::btnTrap_Click); + // + // lblShortcut + // + this->lblShortcut->AutoSize = true; + this->lblShortcut->Font = (gcnew System::Drawing::Font(L"Segoe UI", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->lblShortcut->Location = System::Drawing::Point(82, 157); + this->lblShortcut->Name = L"lblShortcut"; + this->lblShortcut->Size = System::Drawing::Size(138, 21); + this->lblShortcut->TabIndex = 1; + this->lblShortcut->Text = L"Shortcut: Ctrl+F11"; + this->lblShortcut->TextAlign = System::Drawing::ContentAlignment::TopCenter; + // + // tmLoop + // + this->tmLoop->Enabled = true; + this->tmLoop->Interval = 10; + this->tmLoop->Tick += gcnew System::EventHandler(this, &MainFrm::tmLoop_Tick); + // + // lblMouse + // + this->lblMouse->AutoSize = true; + this->lblMouse->Font = (gcnew System::Drawing::Font(L"Segoe UI", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, + static_cast(204))); + this->lblMouse->Location = System::Drawing::Point(8, 100); + this->lblMouse->Name = L"lblMouse"; + this->lblMouse->Size = System::Drawing::Size(120, 21); + this->lblMouse->TabIndex = 1; + this->lblMouse->Text = L"Mouse position:"; + this->lblMouse->TextAlign = System::Drawing::ContentAlignment::TopCenter; + // + // MainFrm + // + this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); + this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; + this->ClientSize = System::Drawing::Size(304, 233); + this->Controls->Add(this->btnTrap); + this->Controls->Add(this->lblBottomRight); + this->Controls->Add(this->lblMouse); + this->Controls->Add(this->lblShortcut); + this->Controls->Add(this->lblTopLeft); + this->Controls->Add(this->txtY2); + this->Controls->Add(this->txtY1); + this->Controls->Add(this->txtX2); + this->Controls->Add(this->txtX1); + this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle; + this->MaximizeBox = false; + this->Name = L"MainFrm"; + this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen; + this->Text = L"Mouse Trapper"; + this->ResumeLayout(false); + this->PerformLayout(); + + } +#pragma endregion + + Void btnTrap_Click(Object ^sender, EventArgs ^e); + Void tmLoop_Tick(Object ^sender, EventArgs ^e); +}; +} diff --git a/MainFrm.resx b/MainFrm.resx new file mode 100644 index 0000000..251fcea --- /dev/null +++ b/MainFrm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/MouseTrapper.sln b/MouseTrapper.sln new file mode 100644 index 0000000..257d6a5 --- /dev/null +++ b/MouseTrapper.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MouseTrapper", "MouseTrapper.vcxproj", "{E65F63BD-6DA4-4A90-9B53-D5DEA7AEA701}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E65F63BD-6DA4-4A90-9B53-D5DEA7AEA701}.Debug|Win32.ActiveCfg = Debug|Win32 + {E65F63BD-6DA4-4A90-9B53-D5DEA7AEA701}.Debug|Win32.Build.0 = Debug|Win32 + {E65F63BD-6DA4-4A90-9B53-D5DEA7AEA701}.Release|Win32.ActiveCfg = Release|Win32 + {E65F63BD-6DA4-4A90-9B53-D5DEA7AEA701}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/MouseTrapper.vcxproj b/MouseTrapper.vcxproj new file mode 100644 index 0000000..8c11a6f --- /dev/null +++ b/MouseTrapper.vcxproj @@ -0,0 +1,98 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {E65F63BD-6DA4-4A90-9B53-D5DEA7AEA701} + v4.5 + ManagedCProj + MouseTrapper + + + + Application + true + v120 + true + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + Level3 + Disabled + WIN32;_DEBUG;%(PreprocessorDefinitions) + + + true + user32.lib + Windows + Main + + + + + Level3 + WIN32;NDEBUG;%(PreprocessorDefinitions) + + + true + user32.lib + Main + Windows + + + + + + + + + + + + + + + CppForm + + + + + MainFrm.h + + + + + + \ No newline at end of file diff --git a/MouseTrapper.vcxproj.filters b/MouseTrapper.vcxproj.filters new file mode 100644 index 0000000..cdf127e --- /dev/null +++ b/MouseTrapper.vcxproj.filters @@ -0,0 +1,32 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4d341e3 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Mouse Trapper + +This is a simple program that traps the mouse cursor within certain limits. Based on Windows Forms using C++. + + \ No newline at end of file