Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

MATPaint - Simple App with Simple Features

,
Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
10 Dec 2012CPOL8 min read 51.9K   3.5K   24  
MATrix Paint - A simple app with many C# and mostly Windows Forms features for a class project.
  • MatPaint-SourceCode.zip
    • MatPaint
      • .git
        • branches
        • config
        • description
        • FETCH_HEAD
        • HEAD
        • hooks
          • applypatch-msg.sample
          • commit-msg.sample
          • post-update.sample
          • pre-applypatch.sample
          • pre-commit.sample
          • prepare-commit-msg.sample
          • pre-rebase.sample
          • update.sample
        • index
        • info
          • exclude
        • logs
          • refs
            • remotes
              • origin
                • master
        • objects
          • 02
            • 6a40ff943cb5af664959d3b50f4c431db3fed2
          • 04
            • 56f03267d3408234ad9775ab33e3288427fe7c
          • 08
            • 52d74105472ccf249db045726cee30ad109635
          • 0b
            • ebc97b17ee76d59a326d37308c961d028d423e
          • 12
            • d04808d878c5b4af042ca9725f0de90d01d027
          • 15
            • 0c2eee60fe9f7dc30d244ea2dd0bc8f205ea4b
          • 19
            • c3c3dee31d30afeb2f6ad46da654858a0364d5
          • 1a
            • bc0cde8fd964a3cbcb380fabc80eb41229f733
          • 1d
            • 8fd5e51f974637143c1348240760ae26fd6f38
          • 21
            • b19229d796c2f7eb85d6e3100c46ef2ad95577
          • 2b
            • da20eae39fea7958e24f77cbd38873cc16ae10
          • 3d
            • d9a94f833e951eb5ecf8223048f07359a34787
          • 3e
            • 4f9f30e83c132c8e990f9a6bdebd419f7e8a21
            • 911e91410845b16f5618e4aff766ffa57c6f03
            • cd43e2840b8de7c12b46ed6ccadfb25e135132
          • 3f
            • 12ce90a0bb8bec89d8887b394498f8c9546c72
          • 40
            • b933eecf2b4c270f1135ad06a4de0639d3ec53
          • 41
            • 2eeda78dc9de1186c2e0e1526764af82ab3431
          • 42
            • 72b845230f57b53302da1776b4bf2135661410
          • 4a
            • 994e8dedd71eed64956d021d6486330b160d1a
          • 4d
            • 19126660e52c8858f5431b3bc42d0facdade2d
          • 4e
            • bd0e365bfeface4e5d98452d06024d64f858ee
          • 4f
            • ec87326d0cf6176efd24bf22ef1e80a0cbf45e
          • 53
            • fde55f1282e02306992508e6da8c358c637eff
          • 55
            • 7a7ef77018823a0548a9d9e1525033652ccd0b
            • 9eabca9fbdd24372ee1efc5f81fa6b397a8604
          • 56
            • 92d5f36b547f045ce5c6a4f13c33246edc4770
          • 57
            • 9cf7c166da4eb586f43516be3b5ed09bd773a8
          • 58
            • 36eb46bf1d200a3041486b1779d6d01eb3fe6d
          • 5d
            • 6b30e99004a3dd16ea1e3f2c5f7014c1f7fe99
          • 5e
            • bd21a16d61827ba9ee2b0353fbf6c8c8e88bca
          • 62
            • 223866817095a2352c8633f93830da7e61aaed
            • d2de354a6a47f8a06bdd4ac1dc8546bd70315f
          • 68
            • fce172f3afdd8de4c9bc24a22c7559e4375134
          • 69
            • fb98a56a4282146ccd0fbc1bfaaf8e69b3a8b9
          • 6b
            • 128b5aa87dc35a2f9d9e8cca3c82176be81852
          • 6c
            • 02e8419d66b422948606cac0245f2710a17292
          • 6d
            • 1aea10825050c423d3fffecdee4b863adf474d
          • 6e
            • 9bc0ba03f5b5bb59410eef165f2f336fb19f50
          • 6f
            • 93f07fca68cd370e0a0f1ffdd82d8bb95b49ee
          • 71
            • 7ec20293a90bea84acbc6fc16d3603176431bc
          • 73
            • f366f70b417b5e915b3230038969fb50d9fc96
          • 86
            • b4353d704dd6bff78e6709eeb7c5c14a9a8779
          • 87
            • c729aa0097b18dd155ee82855489501d2ce1a2
          • 8d
            • 4570f724df7314f5b77a18f8877b7405c30661
          • 8e
            • 615f25fdc66f1a373ff9c5ab281ca0eebc52fb
          • 9d
            • 3c35ad2e4c8ef0e64009172d9efcfd7c6af238
            • f1eb7f746a70339220a1bb8f1e37698b98d222
          • 9e
            • 8c4a8286a0338e89d6d165d2e9e53181437483
          • a0
            • 1d10cdbd04b3c29108ba10948f3b5bfc54d456
          • a5
            • b6ce06e1ad111a4953bb078267c707df924939
          • a7
            • 94bec56531ca33a32c3344bc5d1018f2710411
          • aa
            • 5723f353ecad8b9e58f8bea53e04111f24e1e2
          • af
            • 7dbebbacef595e3089c01c05671016c21a8304
          • b2
            • 3181604e2b0dda01a344ba8b07d212cc677061
          • b9
            • 102d686ff923b991d05bd6a89a648a91f43123
            • 8bdd594c8fe8651e4eb20d6c2aec4e69799ff7
            • f5d7a79511454698df886ecaba100340f62e39
          • bc
            • e35b9d9b6b3ba8478ab5745ed4511b83cc57ac
          • bd
            • 8c43c8e818f6ad4330affddee527fb2d47f863
          • bf
            • 6c72dbca250c1f67d2d912c60787dc49637a51
          • c5
            • e330f767c3883d2e9325d28ced1a26cbfe8d58
          • cd
            • bea1806be2c6df868eb1396beaa8363b24d242
          • d2
            • 0a022524f92804e359b70511c745bc3104e42e
          • d3
            • c9000d9e91b1b85c344c2b3dfd0b12fb12a41d
          • df
            • d03be8a497b04bca954c7340ce102d2fa8450f
          • e2
            • a7ae77c828a102313c0e6fa5386e17cac9b434
          • e4
            • 60b3031d89fb8619bbdc27d601e0d3aa448235
          • e5
            • 4d6bf998819340467e486151c5cb1af5fbba79
          • e8
            • 11c84808e2e9f3b9307fe4e775a85ddcf02327
          • ea
            • 95050f6912fdf3e9d116ef1fcfc8293b535812
          • f1
            • 7bd7706293d4c1d7cbd8af2a71727ba21510b7
          • f8
            • a9c8851abfe950be68008dca4578693ac613aa
          • fc
            • bd9fd80287f6a510fd74049e787ce12a0aa81b
            • f7a0cbdab1869afa37f363fbbba534983a6b36
          • fd
            • 566c6e69370d49c60685fae1ee8fb019df589e
          • fe
            • 3591a343e47c2569b68b62702bcaa8420b91cf
          • info
          • pack
        • refs
          • heads
            • master
          • remotes
            • origin
              • master
          • tags
      • .gitattributes
      • .gitignore
      • MATPaint.sln
      • MATPaint.suo
      • MATPaint.v11.suo
      • obj
        • x86
          • Debug
      • README.txt
      • Setup2
      • WindowsFormsApplication6
  • DISK1.zip
  • DISK1-noexe.zip
  • MatPaint-Source-Code.zip
    • config
    • description
    • FETCH_HEAD
    • HEAD
    • applypatch-msg.sample
    • commit-msg.sample
    • post-update.sample
    • pre-applypatch.sample
    • pre-commit.sample
    • prepare-commit-msg.sample
    • pre-rebase.sample
    • update.sample
    • index
    • exclude
    • master
    • 6a40ff943cb5af664959d3b50f4c431db3fed2
    • 56f03267d3408234ad9775ab33e3288427fe7c
    • 52d74105472ccf249db045726cee30ad109635
    • ebc97b17ee76d59a326d37308c961d028d423e
    • d04808d878c5b4af042ca9725f0de90d01d027
    • 0c2eee60fe9f7dc30d244ea2dd0bc8f205ea4b
    • c3c3dee31d30afeb2f6ad46da654858a0364d5
    • bc0cde8fd964a3cbcb380fabc80eb41229f733
    • 8fd5e51f974637143c1348240760ae26fd6f38
    • b19229d796c2f7eb85d6e3100c46ef2ad95577
    • da20eae39fea7958e24f77cbd38873cc16ae10
    • d9a94f833e951eb5ecf8223048f07359a34787
    • 4f9f30e83c132c8e990f9a6bdebd419f7e8a21
    • 911e91410845b16f5618e4aff766ffa57c6f03
    • cd43e2840b8de7c12b46ed6ccadfb25e135132
    • 12ce90a0bb8bec89d8887b394498f8c9546c72
    • b933eecf2b4c270f1135ad06a4de0639d3ec53
    • 2eeda78dc9de1186c2e0e1526764af82ab3431
    • 72b845230f57b53302da1776b4bf2135661410
    • 994e8dedd71eed64956d021d6486330b160d1a
    • 19126660e52c8858f5431b3bc42d0facdade2d
    • bd0e365bfeface4e5d98452d06024d64f858ee
    • ec87326d0cf6176efd24bf22ef1e80a0cbf45e
    • fde55f1282e02306992508e6da8c358c637eff
    • 7a7ef77018823a0548a9d9e1525033652ccd0b
    • 9eabca9fbdd24372ee1efc5f81fa6b397a8604
    • 92d5f36b547f045ce5c6a4f13c33246edc4770
    • 9cf7c166da4eb586f43516be3b5ed09bd773a8
    • 36eb46bf1d200a3041486b1779d6d01eb3fe6d
    • 6b30e99004a3dd16ea1e3f2c5f7014c1f7fe99
    • bd21a16d61827ba9ee2b0353fbf6c8c8e88bca
    • 223866817095a2352c8633f93830da7e61aaed
    • d2de354a6a47f8a06bdd4ac1dc8546bd70315f
    • fce172f3afdd8de4c9bc24a22c7559e4375134
    • fb98a56a4282146ccd0fbc1bfaaf8e69b3a8b9
    • 128b5aa87dc35a2f9d9e8cca3c82176be81852
    • 02e8419d66b422948606cac0245f2710a17292
    • 1aea10825050c423d3fffecdee4b863adf474d
    • 9bc0ba03f5b5bb59410eef165f2f336fb19f50
    • 93f07fca68cd370e0a0f1ffdd82d8bb95b49ee
    • 7ec20293a90bea84acbc6fc16d3603176431bc
    • f366f70b417b5e915b3230038969fb50d9fc96
    • b4353d704dd6bff78e6709eeb7c5c14a9a8779
    • c729aa0097b18dd155ee82855489501d2ce1a2
    • 4570f724df7314f5b77a18f8877b7405c30661
    • 615f25fdc66f1a373ff9c5ab281ca0eebc52fb
    • 3c35ad2e4c8ef0e64009172d9efcfd7c6af238
    • f1eb7f746a70339220a1bb8f1e37698b98d222
    • 8c4a8286a0338e89d6d165d2e9e53181437483
    • 1d10cdbd04b3c29108ba10948f3b5bfc54d456
    • b6ce06e1ad111a4953bb078267c707df924939
    • 94bec56531ca33a32c3344bc5d1018f2710411
    • 5723f353ecad8b9e58f8bea53e04111f24e1e2
    • 7dbebbacef595e3089c01c05671016c21a8304
    • 3181604e2b0dda01a344ba8b07d212cc677061
    • 102d686ff923b991d05bd6a89a648a91f43123
    • 8bdd594c8fe8651e4eb20d6c2aec4e69799ff7
    • f5d7a79511454698df886ecaba100340f62e39
    • e35b9d9b6b3ba8478ab5745ed4511b83cc57ac
    • 8c43c8e818f6ad4330affddee527fb2d47f863
    • 6c72dbca250c1f67d2d912c60787dc49637a51
    • e330f767c3883d2e9325d28ced1a26cbfe8d58
    • bea1806be2c6df868eb1396beaa8363b24d242
    • 0a022524f92804e359b70511c745bc3104e42e
    • c9000d9e91b1b85c344c2b3dfd0b12fb12a41d
    • d03be8a497b04bca954c7340ce102d2fa8450f
    • a7ae77c828a102313c0e6fa5386e17cac9b434
    • 60b3031d89fb8619bbdc27d601e0d3aa448235
    • 4d6bf998819340467e486151c5cb1af5fbba79
    • 11c84808e2e9f3b9307fe4e775a85ddcf02327
    • 95050f6912fdf3e9d116ef1fcfc8293b535812
    • 7bd7706293d4c1d7cbd8af2a71727ba21510b7
    • a9c8851abfe950be68008dca4578693ac613aa
    • bd9fd80287f6a510fd74049e787ce12a0aa81b
    • f7a0cbdab1869afa37f363fbbba534983a6b36
    • 566c6e69370d49c60685fae1ee8fb019df589e
    • 3591a343e47c2569b68b62702bcaa8420b91cf
    • master
    • master
    • .gitattributes
    • .gitignore
    • MATPaint.sln
    • MATPaint.suo
    • MATPaint.v11.suo
    • README.txt
    • Setup2.isl
    • Setup2.isproj
    • 11-28-2012 08-23-31 AM.txt
    • 11-28-2012 08-23-35 AM.htm
    • _IsIcoRes.Exe
    • app.config
    • app.config
    • MATPaint.application
    • MATPaint.exe
    • MATPaint.exe.manifest
    • MATPaint.pdb
    • MATPaint.vshost.application
    • MATPaint.vshost.exe
    • MATPaint.vshost.exe.manifest
    • WindowsFormsApplication6.exe
    • WindowsFormsApplication6.pdb
    • WindowsFormsApplication6.vshost.exe.manifest
    • Eula.rtf
    • Form1.cs
    • Form1.Designer.cs
    • Form1.resx
    • icon.ico
    • MATPaint.csproj
    • MATPaint.csproj.user
    • MATPaint_TemporaryKey.pfx
    • DesignTimeResolveAssemblyReferences.cache
    • DesignTimeResolveAssemblyReferencesInput.cache
    • GenerateResource.read.1.tlog
    • GenerateResource.write.1.tlog
    • MATPaint.application
    • MATPaint.csproj.FileListAbsolute.txt
    • MATPaint.csproj.GenerateResource.Cache
    • MATPaint.exe
    • MATPaint.exe.manifest
    • MATPaint.Form1.resources
    • MATPaint.pdb
    • MATPaint.Properties.Resources.resources
    • ProjectM00.Form1.resources
    • ProjectM00.Properties.Resources.resources
    • ProjectM01.Form1.resources
    • ProjectM01.Properties.Resources.resources
    • ResolveAssemblyReference.cache
    • TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
    • TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
    • TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
    • Properties.Resources.Designer.cs.dll
    • WindowsFormsApplication6.csproj.FileListAbsolute.txt
    • WindowsFormsApplication6.csproj.GenerateResource.Cache
    • WindowsFormsApplication6.csprojResolveAssemblyReference.cache
    • WindowsFormsApplication6.exe
    • WindowsFormsApplication6.Form1.resources
    • WindowsFormsApplication6.pdb
    • WindowsFormsApplication6.Properties.Resources.resources
    • Program.cs
    • AssemblyInfo.cs
    • Resources.Designer.cs
    • Resources.resx
    • Settings.Designer.cs
    • Settings.settings
    • Settings.cs
  • MatPaint-Source-Code-noexe.zip
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MATPaint
{
    public partial class Form1 : Form
    {
        // for Pattern Status SAVE/Open functionality
        // to BE DONE....
        enum FileStatus { New, Open};
        FileStatus currentFile = FileStatus.New;
        bool isChanged = false;
        string matrixFileName = null;

        public Form1()
        {
            InitializeComponent();
        }
        public Form1(string matrixFileNameARG) {
            InitializeComponent();
            matrixFileName = matrixFileNameARG;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Location = Properties.Settings.Default.appLocation;
            this.WindowState = Properties.Settings.Default.appState;
            this.Size = Properties.Settings.Default.appSize;
            num_Col.Value = Properties.Settings.Default.MatCols;
            num_Row.Value = Properties.Settings.Default.MatRows;
            recent1toolStripMenuItem.Text = Properties.Settings.Default.Recent1;
            recent2toolStripMenuItem.Text = Properties.Settings.Default.Recent2;
            recent3toolStripMenuItem.Text = Properties.Settings.Default.Recent3;
            bt_RotataClock.Enabled = false;
            bt_RotateAnticlock.Enabled = false;
            if (matrixFileName != null)
            {
                OpenMatrix(matrixFileName);
                currentFile = FileStatus.Open;
                isChanged = false;
            }
            else
            {
                currentFile = FileStatus.New;
                NewMatrix();
            }
            UpdateMatrixCellSize();

            // Tooltip 
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(this.num_Col, "Select the Width");
            ToolTip1.SetToolTip(this.num_Row, "Select the Height");
            ToolTip1.SetToolTip(this.cmbx_Grid, "Select the size of the block");
            ToolTip1.SetToolTip(this.bt_MatrixCreate, "To create and display the matrix of provided parameters");
            ToolTip1.SetToolTip(this.bt_Clear, "Clear the matrix data");
            ToolTip1.SetToolTip(this.bt_Invert, "Invert the matrix");
            ToolTip1.SetToolTip(this.bt_Mirror, "Create the mirror of the matrix");
            ToolTip1.SetToolTip(this.bt_Flip, "Turn over the matrix");
            ToolTip1.SetToolTip(this.bt_RotateAnticlock, "Rotate anticlockwise by 90 deg");
            ToolTip1.SetToolTip(this.bt_RotataClock, "Rotate clockwise by 90 deg");
            ToolTip1.SetToolTip(this.bt_RotataClock, "Rotate clockwise by 90 deg");
            ToolTip1.SetToolTip(this.bt_RotataClock, "Rotate clockwise by 90 deg");
            ToolTip1.SetToolTip(this.bt_RotataClock, "Rotate clockwise by 90 deg");
            ToolTip1.SetToolTip(this.cbx_GridRoll, "Choose whether to rotate/roll the grid");
            ToolTip1.SetToolTip(this.bt_Up, "Move up in the grid");
            ToolTip1.SetToolTip(this.bt_Down, "Move down in the grid");
            ToolTip1.SetToolTip(this.bt_Left, "Move left in the grid");
            ToolTip1.SetToolTip(this.bt_Right, "Move right in the grid");

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChanged == true && currentFile == FileStatus.New)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                    SaveMatrix();
                else if (z == DialogResult.Cancel)
                    e.Cancel = true;
            }
            else if (isChanged == true && currentFile == FileStatus.Open)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                    SaveOpenMatrix();
                else if (z == DialogResult.Cancel)
                    e.Cancel = true;
            }

            Properties.Settings.Default.appLocation = this.Location;
            Properties.Settings.Default.appState = this.WindowState;
            Properties.Settings.Default.appSize = this.Size;
            Properties.Settings.Default.MatCols = Convert.ToInt16(num_Col.Value);
            Properties.Settings.Default.MatRows = Convert.ToInt16(num_Row.Value);
            Properties.Settings.Default.Recent1 = recent1toolStripMenuItem.Text;
            Properties.Settings.Default.Recent2 = recent2toolStripMenuItem.Text;
            Properties.Settings.Default.Recent3 = recent3toolStripMenuItem.Text;
            Properties.Settings.Default.Save();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                notifyIcon1.Visible = true;
                this.ShowInTaskbar = false;
            }
            else
            {
                notifyIcon1.Visible = false;
                this.ShowInTaskbar = true;
            }
        }
        
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
        }

        private void bt_MatrixCreate_Click(object sender, EventArgs e)
        {
            if (isChanged == true && currentFile == FileStatus.New)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                {
                    SaveMatrix();
                    NewMatrix();
                }
                else if (z == DialogResult.No)
                    NewMatrix();
            }
            else if (isChanged == true && currentFile == FileStatus.Open)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                {
                    SaveOpenMatrix();
                    NewMatrix();
                }
                else if (z == DialogResult.No)
                    NewMatrix();
            }
            else if (isChanged == false)
                NewMatrix();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCell.Style.BackColor != Color.Black)
                dataGridView1.CurrentCell.Style.BackColor = Color.Black;
            else
                dataGridView1.CurrentCell.Style.BackColor = Color.White;
            dataGridView1.CurrentCell.Selected = false;
            isChanged = true;
        }

        private void cmbx_Grid_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateMatrixCellSize();
        }

        private void dataGridView1_SizeChanged(object sender, EventArgs e)
        {
            UpdateMatrixCellSize();
        }

        private void UpdateMatrixCellSize()
        {
            int RowHeight = 10, ColumnWidth = 10;
            switch (cmbx_Grid.SelectedIndex)
            {
                case 0:
                    // Fill Screen
                    if (dataGridView1.RowCount > 0)
                        RowHeight = dataGridView1.Height / dataGridView1.RowCount;
                    if (dataGridView1.ColumnCount > 0)
                        ColumnWidth = dataGridView1.Width / dataGridView1.ColumnCount;
                    break;
                case 1:
                    // Tiny
                    RowHeight = 12;
                    ColumnWidth = 12;
                    break;
                case 2:
                    // Medium
                    RowHeight = 22;
                    ColumnWidth = 22;
                    break;
                case 3:
                    // Large
                    RowHeight = 42;
                    ColumnWidth = 42;
                    break;
                case 4:
                    // HUGE
                    RowHeight = 62;
                    ColumnWidth = 62;
                    break;
                default:
                    break;
            }

            for (int z = 0; z < dataGridView1.RowCount; z++)
                dataGridView1.Rows[z].Height = RowHeight;
            for (int z = 0; z < dataGridView1.ColumnCount; z++)
                dataGridView1.Columns[z].Width = ColumnWidth;
        }

        private void bt_Invert_Click(object sender, EventArgs e)
        {
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    if (dataGridView1.Rows[z].Cells[y].InheritedStyle.BackColor != Color.Black)
                        dataGridView1.Rows[z].Cells[y].Style.BackColor = Color.Black;
                    else
                        dataGridView1.Rows[z].Cells[y].Style.BackColor = Color.White;
            isChanged = true;
        }

        private void bt_Clear_Click(object sender, EventArgs e)
        {
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = Color.White;
        }

        private void bt_Mirror_Click(object sender, EventArgs e)
        {
            int x = dataGridView1.ColumnCount - 1;
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount / 2; y++)
                {
                    Color temp = dataGridView1.Rows[z].Cells[y].Style.BackColor;
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = dataGridView1.Rows[z].Cells[x - y].Style.BackColor;
                    dataGridView1.Rows[z].Cells[x - y].Style.BackColor = temp;                    
                }
            isChanged = true;
        }

        private void bt_Flip_Click(object sender, EventArgs e)
        {
            int x = dataGridView1.RowCount - 1;
            for (int z = 0; z < dataGridView1.RowCount / 2; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                {
                    Color temp = dataGridView1.Rows[z].Cells[y].Style.BackColor;
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = dataGridView1.Rows[x - z].Cells[y].Style.BackColor;
                    dataGridView1.Rows[x - z].Cells[y].Style.BackColor = temp;
                }
            isChanged = true;
        }

        private void bt_RotateAnticlock_Click(object sender, EventArgs e)
        {
            int x = dataGridView1.ColumnCount - 1;
            Color[,] GridArray = new Color[dataGridView1.RowCount, dataGridView1.ColumnCount];

            // Saving grid data in a 2D array
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    GridArray[z,y] = dataGridView1.Rows[z].Cells[y].Style.BackColor;

            // Rotating anticlockwise by 90 deg
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = GridArray[y, x - z];
            isChanged = true;
        }

        private void bt_RotataClock_Click(object sender, EventArgs e)
        {
            int x = dataGridView1.ColumnCount - 1;
            Color[,] GridArray = new Color[dataGridView1.RowCount, dataGridView1.ColumnCount];

            // Saving grid data in a 2D array
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    GridArray[z, y] = dataGridView1.Rows[z].Cells[y].Style.BackColor;

            // Rotating clockwise by 90 deg
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = GridArray[x - y, z];
            isChanged = true;
        }

        private void newToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (isChanged == true && currentFile == FileStatus.New)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                {
                    SaveMatrix();
                    NewMatrix();
                }
                else if (z == DialogResult.No)
                    NewMatrix();
            }
            else if (isChanged == true && currentFile == FileStatus.Open)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                {
                    SaveOpenMatrix();
                    NewMatrix();
                }
                else if (z == DialogResult.No)
                    NewMatrix();
            }
            else if (isChanged == false)
                NewMatrix();
        }

        private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (currentFile == FileStatus.New)
                SaveMatrix();
            else if (currentFile == FileStatus.Open)
                SaveOpenMatrix();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveMatrix();
        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (isChanged == true && currentFile == FileStatus.New)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                {
                    SaveMatrix();
                    OpenMatrix();
                }
                else if (z == DialogResult.No)
                    OpenMatrix();
            }
            else if (isChanged == true && currentFile == FileStatus.Open)
            {
                DialogResult z = MessageBox.Show("Do you want to SAVE the current Pattern", "Save Pattern", MessageBoxButtons.YesNoCancel);
                if (z == DialogResult.Yes)
                {
                    SaveOpenMatrix();
                    OpenMatrix();
                }
                else if (z == DialogResult.No)
                    OpenMatrix();
            }
            else
                OpenMatrix();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
                    this.Close();
        }

        private void NewMatrix()
        {
            dataGridView1.ColumnCount = Convert.ToInt16(num_Col.Value);
            dataGridView1.RowCount = Convert.ToInt16(num_Row.Value);
            for (int z = 0; z < dataGridView1.RowCount; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = Color.White;

            dataGridView1.CurrentCell.Selected = false;
            dataGridView1.DefaultCellStyle.BackColor = Color.White;
            UpdateMatrixCellSize();
            if (dataGridView1.RowCount == dataGridView1.ColumnCount)
            {
                bt_RotateAnticlock.Enabled = true;
                bt_RotataClock.Enabled = true;
            }
            UpdateMatrixCellSize();
            currentFile = FileStatus.New;
            isChanged = false;
            this.Text = "MATPaint - ";
        }

        private void SaveMatrix()
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string matrixCode = "";
                matrixCode = dataGridView1.ColumnCount.ToString() + "," + dataGridView1.RowCount.ToString() + "\r\n";
                int row = 0, col = 0, page = 0, z = 0;
                for (page = 0; page <= dataGridView1.RowCount / 8; page++)
                {
                    for (col = 0; col < dataGridView1.ColumnCount; col++)
                    {
                        for (row = 0; row < 8; row++)
                        {
                            if (dataGridView1.RowCount > (page * 8) + row)
                                if (dataGridView1.Rows[(page * 8) + row].Cells[col].Style.BackColor == Color.Black)
                                    z = z + Convert.ToInt16(Math.Pow(2.0, row));
                            z += 0;
                        }
                        matrixCode = matrixCode + z.ToString() + ",";
                        z = 0;
                    }
                    matrixCode = matrixCode + "\r\n";
                }
                TextWriter tw = new StreamWriter(saveFileDialog1.FileName);
                tw.Write(matrixCode);
                tw.Close();

                matrixFileName = saveFileDialog1.FileName;
                isChanged = false;
                currentFile = FileStatus.Open;
                this.Text = "MATPaint - " + matrixFileName;
                saveFileDialog1.FileName = "";
            }
        }
        
        private void SaveOpenMatrix()
        {
            string matrixCode = "";
            matrixCode = dataGridView1.ColumnCount.ToString() + "," + dataGridView1.RowCount.ToString() + "\r\n";
            int row = 0, col = 0, page = 0, z = 0;
            for (page = 0; page <= dataGridView1.RowCount / 8; page++)
            {
                for (col = 0; col < dataGridView1.ColumnCount; col++)
                {
                    for (row = 0; row < 8; row++)
                    {
                        if (dataGridView1.RowCount > (page * 8) + row)
                            if (dataGridView1.Rows[(page * 8) + row].Cells[col].Style.BackColor == Color.Black)
                                z = z + Convert.ToInt16(Math.Pow(2.0, row));
                        z += 0;
                    }
                    matrixCode = matrixCode + z.ToString() + ",";
                    z = 0;
                }
                matrixCode = matrixCode + "\r\n";
            }
            TextWriter tw = new StreamWriter(matrixFileName);
            tw.Write(matrixCode);
            tw.Close();

            isChanged = false;
            currentFile = FileStatus.Open;
            saveFileDialog1.FileName = "";
        }

        private void OpenMatrix(string matrixFileNameToOpen = null)
        {
            bool readFile = false;
            openFileDialog1.FileName = matrixFileNameToOpen;
            
            if(matrixFileNameToOpen != null)
                readFile = true;
            else if (openFileDialog1.ShowDialog() == DialogResult.OK)
                readFile = true;

            if (readFile == true)
            {
                // Open FILE SPECIFCIATIONS
                // File should be in simple TXT format, ',' delimited
                // First Line: ColumnCount, RowCount
                // Second Line and so on: Each line has data of 8 rows in INT format and commas separate the different Columns
                try
                {
                    TextReader tr = new StreamReader(openFileDialog1.FileName);
                    string matrixCode = tr.ReadToEnd();
                    tr.Close();
                    string[] cellDataS = matrixCode.Split(new char[] { ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    int[] cellData = new int[cellDataS.Length];
                    int count = 0;
                    foreach (string s in cellDataS)
                        cellData[count++] = Convert.ToInt16(s);

                    int row = cellData[1], col = cellData[0], page = 0, z = 0;

                    count = 1;
                    dataGridView1.ColumnCount = col;
                    dataGridView1.RowCount = row;
                    bt_Clear_Click(this, EventArgs.Empty);
                    try
                    {
                        for (page = 0; page <= row / 8; page++)
                            for (col = 0; col < dataGridView1.ColumnCount; col++)
                            {
                                z = cellData[++count];
                                for (row = 0; row < 8; row++)
                                    if (dataGridView1.RowCount > (page * 8) + row)
                                        if ((z & Convert.ToInt16(Math.Pow(2.0, row))) > 0)
                                            dataGridView1.Rows[(page * 8) + row].Cells[col].Style.BackColor = Color.Black;
                            }
                    }
                    catch { }
                    dataGridView1.ClearSelection();
                    matrixFileName = openFileDialog1.FileName;
                    isChanged = false;
                    currentFile = FileStatus.Open;
                    num_Col.Value = dataGridView1.ColumnCount;
                    num_Row.Value = dataGridView1.RowCount;

                    if (dataGridView1.RowCount == dataGridView1.ColumnCount)
                    {
                        bt_RotateAnticlock.Enabled = true;
                        bt_RotataClock.Enabled = true;
                    }
                    else
                    {
                        bt_RotateAnticlock.Enabled = false;
                        bt_RotataClock.Enabled = false;
                    }

                    if (matrixFileName != recent1toolStripMenuItem.Text
                        && matrixFileName != recent2toolStripMenuItem.Text
                        && matrixFileName != recent3toolStripMenuItem.Text)
                    {
                        recent3toolStripMenuItem.Text = recent2toolStripMenuItem.Text;
                        recent2toolStripMenuItem.Text = recent1toolStripMenuItem.Text;
                        recent1toolStripMenuItem.Text = matrixFileName;
                    }
                    this.Text = "MATPaint - " + matrixFileName;
                }
                catch { MessageBox.Show("Cannot Read File", "File Read Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            }
        }

        private void bt_ChngMatrixSize_Click(object sender, EventArgs e)
        {
            int tmpCol = Convert.ToInt16(num_Col.Value);
            int tmpRow = Convert.ToInt16(num_Row.Value);
            if ((tmpCol < dataGridView1.ColumnCount) || (tmpRow < dataGridView1.RowCount))
            {
                if (MessageBox.Show("The New Size is Smaller than current Size, some Clipping may occur, \nContinue ?", "Resize Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    dataGridView1.ColumnCount = tmpCol;
                    dataGridView1.RowCount = tmpRow;
                    UpdateMatrixCellSize();
                    isChanged = true;
                }
            }
            else
            {
                dataGridView1.ColumnCount = tmpCol;
                dataGridView1.RowCount = tmpRow;
                UpdateMatrixCellSize();
                isChanged = true;
            }
            if (dataGridView1.RowCount == dataGridView1.ColumnCount)
            {
                bt_RotateAnticlock.Enabled = true;
                bt_RotataClock.Enabled = true;
            }
            else {
                bt_RotateAnticlock.Enabled = false;
                bt_RotataClock.Enabled = false;
            }
        }

        private void bt_Up_Click(object sender, EventArgs e)
        {
            // Storing the data of the critical Row 0 - the first row
            Color[] temp = new Color[dataGridView1.ColumnCount];
            for (int y = 0; y < dataGridView1.ColumnCount; y++)
                temp[y] = dataGridView1.Rows[0].Cells[y].Style.BackColor;

            // Moving the rest of the grid UP
            for (int z = 0; z < dataGridView1.RowCount - 1; z++)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = dataGridView1.Rows[z + 1].Cells[y].Style.BackColor;

            // Checking whether to roll or shift the grid UP
            if (cbx_GridRoll.Checked == true)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[y].Style.BackColor = temp[y];
            else
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[y].Style.BackColor = Color.White;
        }

        private void bt_Down_Click(object sender, EventArgs e)
        {
            // Storing the data of the critical Row - the last row
            Color[] temp = new Color[dataGridView1.ColumnCount];
            for (int y = 0; y < dataGridView1.ColumnCount; y++)
                temp[y] = dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[y].Style.BackColor;

            // Moving the rest of the grid DOWN
            for (int z = dataGridView1.RowCount - 1; z > 0; z--)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[z].Cells[y].Style.BackColor = dataGridView1.Rows[z - 1].Cells[y].Style.BackColor;

            // Checking whether to roll or shift the grid DOWN
            if (cbx_GridRoll.Checked == true)
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[0].Cells[y].Style.BackColor = temp[y];
            else
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                    dataGridView1.Rows[0].Cells[y].Style.BackColor = Color.White;
        }

        private void bt_Left_Click(object sender, EventArgs e)
        {
            // Storing the data of the critical Column 0 - the first column
            Color[] temp = new Color[dataGridView1.RowCount];
            for (int x = 0; x < dataGridView1.RowCount; x++)
                temp[x] = dataGridView1.Rows[x].Cells[0].Style.BackColor;

            // Moving the rest of the grid to the LEFT
            for (int y = 0; y < dataGridView1.ColumnCount - 1; y++)
                for (int x = 0; x < dataGridView1.RowCount; x++)
                    dataGridView1.Rows[x].Cells[y].Style.BackColor = dataGridView1.Rows[x].Cells[y + 1].Style.BackColor;

            // Checking whether to roll or shift the grid to the LEFT
            if (cbx_GridRoll.Checked == true)
                for (int x = 0; x < dataGridView1.RowCount; x++)
                    dataGridView1.Rows[x].Cells[dataGridView1.ColumnCount - 1].Style.BackColor = temp[x];
            else
                for (int x = 0; x < dataGridView1.RowCount; x++)
                    dataGridView1.Rows[x].Cells[dataGridView1.ColumnCount - 1].Style.BackColor = Color.White;
        }

        private void bt_Right_Click(object sender, EventArgs e)
        {
            // Storing the data of the critical Column 0 - the last column
            Color[] temp = new Color[dataGridView1.RowCount];
            for (int x = 0; x < dataGridView1.RowCount; x++)
                temp[x] = dataGridView1.Rows[x].Cells[dataGridView1.ColumnCount - 1].Style.BackColor;

            // Moving the rest of the grid to the LEFT
            for (int y = dataGridView1.ColumnCount - 1; y > 0; y--)
                for (int x = 0; x < dataGridView1.RowCount; x++)
                    dataGridView1.Rows[x].Cells[y].Style.BackColor = dataGridView1.Rows[x].Cells[y - 1].Style.BackColor;

            // Checking whether to roll or shift the grid to the RIGHT
            if (cbx_GridRoll.Checked == true)
                for (int x = 0; x < dataGridView1.RowCount; x++)
                    dataGridView1.Rows[x].Cells[0].Style.BackColor = temp[x];
            else
                for (int x = 0; x < dataGridView1.RowCount; x++)
                    dataGridView1.Rows[x].Cells[0].Style.BackColor = Color.White;


        }

        private void recent1toolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenMatrix(recent1toolStripMenuItem.Text);
        }

        private void recent2toolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenMatrix(recent2toolStripMenuItem.Text);
        }

        private void recent3toolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenMatrix(recent3toolStripMenuItem.Text);
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap matrixPNG = new Bitmap(dataGridView1.Width, dataGridView1.Height);
            dataGridView1.DrawToBitmap(matrixPNG, dataGridView1.ClientRectangle);
            e.Graphics.DrawImage(matrixPNG, new Point(0,0));
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" MATPaint - MATrix Paint\n Made by:\n Zaid Pirwani and Maaz Ahmed\n\nas class project for C# Course (OOP)\ntaught by Engr. Sajid Hussain\nat\nIIEE PCSIR","About BOX",MessageBoxButtons.OK,MessageBoxIcon.Information);

        }

        private void imagePNGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap matrixPNG = new Bitmap(dataGridView1.Width, dataGridView1.Height);
            dataGridView1.DrawToBitmap(matrixPNG, dataGridView1.ClientRectangle);

            saveFileDialog1.Filter = "PNG Image File|*.png";
            saveFileDialog1.Title = "Save PNG Screenshot";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                matrixPNG.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
            saveFileDialog1.Filter = "Matrix Pattern Files|*.maz";
            saveFileDialog1.Title = "Save Pattern File";
            saveFileDialog1.FileName = "";
        }

        private void binaryCodeHEXToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Binary Code File |*.hex";
            saveFileDialog1.Title = "Save Pattern Hex Code";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                using (FileStream hex = new FileStream(saveFileDialog1.FileName, FileMode.Create))
                {
                    using (BinaryWriter writer = new BinaryWriter(hex))
                    {
                        int row = 0, col = 0, page = 0;
                        byte z=0;
                        for (page = 0; page <= dataGridView1.RowCount / 8; page++)
                            for (col = 0; col < dataGridView1.ColumnCount; col++)
                            {
                                for (row = 0; row < 8; row++)
                                {
                                    if (dataGridView1.RowCount > (page * 8) + row)
                                        if (dataGridView1.Rows[(page * 8) + row].Cells[col].Style.BackColor == Color.Black)
                                            z = Convert.ToByte(z + Math.Pow(2.0, row));
                                    z += 0;
                                }
                                writer.Write(z);
                                z = 0;
                            }
                        writer.Close();
                    }
                }
            saveFileDialog1.Filter = "Matrix Pattern Files|*.maz";
            saveFileDialog1.Title = "Save Pattern File";
            saveFileDialog1.FileName = "";
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Ejaad
United States United States
Founded Ejaad, a Technology and Engineering consultancy Startup, based in Karachi, Pakistan.

Student at Industrial Electronics Engineering Student at IIEE-PCSIR, Karachi, Pakistan.

Freelance Web Developer and General Tech Support provider at Guru.com and Local Businesses

Written By
Student
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions