Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pinguupinguu committed Jul 10, 2023
1 parent 85bfe97 commit 168ec43
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 0 deletions.
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
57 changes: 57 additions & 0 deletions ConsoleApp1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CE05578D-0247-4C11-9319-5F1E77CCB4E1}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ConsoleApp1</RootNamespace>
<AssemblyName>ConsoleApp1</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
213 changes: 213 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace GridToggler
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}

public class MainForm : Form
{
private const int GRID_SIZE = 20;
private const int BOX_SIZE = 20;

private int[,] boxes;
private Rectangle[,] rectangles;
private bool isFilling;
private bool isClearing;

private MenuStrip menuStrip;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem saveImageToolStripMenuItem;
private ToolStripMenuItem clearGridToolStripMenuItem;

public MainForm()
{
InitializeComponent();

// Initialize grid of boxes
boxes = new int[GRID_SIZE, GRID_SIZE];
rectangles = new Rectangle[GRID_SIZE, GRID_SIZE];

for (int x = 0; x < GRID_SIZE; x++)
{
for (int y = 0; y < GRID_SIZE; y++)
{
boxes[x, y] = 0;
rectangles[x, y] = new Rectangle(x * BOX_SIZE, y * BOX_SIZE, BOX_SIZE, BOX_SIZE);
}
}
}

private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

for (int x = 0; x < GRID_SIZE; x++)
{
for (int y = 0; y < GRID_SIZE; y++)
{
Rectangle rect = rectangles[x, y];

if (boxes[x, y] == 1)
g.FillRectangle(Brushes.Black, rect);
else
g.FillRectangle(Brushes.White, rect);

g.DrawRectangle(Pens.Black, rect);
}
}
}

private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
isFilling = true;
else if (e.Button == MouseButtons.Right)
isClearing = true;

ToggleBoxState(e);
}

private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
isFilling = false;
isClearing = false;
}

private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (isFilling || isClearing)
ToggleBoxState(e);
}

private void ToggleBoxState(MouseEventArgs e)
{
int x = e.X / BOX_SIZE;
int y = e.Y / BOX_SIZE;

if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE)
{
if (isFilling)
boxes[x, y] = 1;
else if (isClearing)
boxes[x, y] = 0;

Invalidate(rectangles[x, y]);
}
}

private void SaveImageToolStripMenuItem_Click(object sender, EventArgs e)
{
using (Bitmap bitmap = new Bitmap(GRID_SIZE * BOX_SIZE, GRID_SIZE * BOX_SIZE))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
for (int x = 0; x < GRID_SIZE; x++)
{
for (int y = 0; y < GRID_SIZE; y++)
{
Rectangle rect = rectangles[x, y];

if (boxes[x, y] == 1)
g.FillRectangle(Brushes.Black, rect);
else
g.FillRectangle(Brushes.White, rect);

g.DrawRectangle(Pens.Black, rect);
}
}
}

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "PNG Image|*.png";
saveFileDialog.Title = "Save Image";
saveFileDialog.ShowDialog();

if (saveFileDialog.FileName != "")
bitmap.Save(saveFileDialog.FileName, ImageFormat.Png);
}
}

private void ClearGridToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int x = 0; x < GRID_SIZE; x++)
{
for (int y = 0; y < GRID_SIZE; y++)
{
boxes[x, y] = 0;
Invalidate(rectangles[x, y]);
}
}
}

private void InitializeComponent()
{
menuStrip = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
saveImageToolStripMenuItem = new ToolStripMenuItem();
clearGridToolStripMenuItem = new ToolStripMenuItem();

menuStrip.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem });
fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { clearGridToolStripMenuItem, saveImageToolStripMenuItem });
fileToolStripMenuItem.Name = "fileToolStripMenuItem";
fileToolStripMenuItem.Size = new Size(37, 20);
fileToolStripMenuItem.Text = "File";

clearGridToolStripMenuItem.Name = "clearGridToolStripMenuItem";
fileToolStripMenuItem.Size = new Size(37, 20);
this.SuspendLayout();

// Create menu strip
menuStrip = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
saveImageToolStripMenuItem = new ToolStripMenuItem();
clearGridToolStripMenuItem = new ToolStripMenuItem();

menuStrip.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem });
fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { clearGridToolStripMenuItem, saveImageToolStripMenuItem });
fileToolStripMenuItem.Name = "fileToolStripMenuItem";
fileToolStripMenuItem.Size = new Size(37, 20);
fileToolStripMenuItem.Text = "File";

saveImageToolStripMenuItem.Name = "saveImageToolStripMenuItem";
saveImageToolStripMenuItem.Size = new Size(133, 22);
saveImageToolStripMenuItem.Text = "Save Image";
saveImageToolStripMenuItem.Click += SaveImageToolStripMenuItem_Click;

clearGridToolStripMenuItem.Name = "clearGridToolStripMenuItem";
clearGridToolStripMenuItem.Size = new Size(133, 22);
clearGridToolStripMenuItem.Text = "Clear Grid";
clearGridToolStripMenuItem.Click += ClearGridToolStripMenuItem_Click;

Controls.Add(menuStrip);

// Configure form properties
ClientSize = new Size(GRID_SIZE * BOX_SIZE, GRID_SIZE * BOX_SIZE);
DoubleBuffered = true;
FormBorderStyle = FormBorderStyle.FixedSingle;
MaximizeBox = false;
Name = "MainForm";
StartPosition = FormStartPosition.CenterScreen;
Text = "Grid Toggler";
Paint += MainForm_Paint;
MouseDown += MainForm_MouseDown;
MouseUp += MainForm_MouseUp;
MouseMove += MainForm_MouseMove;
ResumeLayout(false);
PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
36 changes: 36 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ConsoleApp1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ConsoleApp1")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ce05578d-0247-4c11-9319-5f1e77ccb4e1")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit 168ec43

Please sign in to comment.