Reversing a region in windows

0

I have somehow managed to write the following code which reverses a portion of the screen:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;

namespace RegionInvert
{
    /*
     * Create a class which is a Child of Form
     * 
     */
    public partial class RegionInvert : Form, IMessageFilter
    {
        private Rectangle rect;
        private int toggle = 0;
        int screen_width = SystemInformation.VirtualScreen.Width;
    int screen_height = SystemInformation.VirtualScreen.Height;

    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetFocus(IntPtr hWnd);

        enum KeyModifier
    {
            None = 0,
            Alt = 1,
        Control = 2,
        Shift = 4,
        WinKey = 8
    }

    public bool PreFilterMessage(ref Message m)
    {
        const int WM_KEYUP = 0x101;
        if (m.Msg == WM_KEYUP)
        {
            return true;
        } else {
            return false;
        }

    }

        public RegionInvert()
        {
            Application.AddMessageFilter(this);

            int id = 0;

        this.FormBorderStyle = FormBorderStyle.None;
                /*
                * Initialize component
                * 
                */
                InitializeComponent();

                /*
                 * Form background, border and transparency
                 * 
                 */
                this.Bounds = Screen.PrimaryScreen.Bounds;
                this.TopMost = true;
                this.Size = new Size(screen_width, screen_height);
                this.BackColor = Color.White;
                this.TransparencyKey  = Color.Black;

                this.Opacity = 0;
                this.Show();
    }

    protected override CreateParams CreateParams
    {
        get
        {
        CreateParams createParams = base.CreateParams;
        createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT

        return createParams;
        }
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
            switch(keyData)
            {
                case Keys.A:
                    this.rect.X = PointToScreen(Cursor.Position).X;
                    this.rect.Y = PointToScreen(Cursor.Position).Y;;
                    break;
                case Keys.B:
                    this.rect.Width = PointToScreen(Cursor.Position).X - this.rect.X;
                    this.rect.Height = PointToScreen(Cursor.Position).Y - this.rect.Y;
                    break;
            case Keys.V:
                InvertRegion();
                break;
            case Keys.D:
                InvertRegion();
                break;
                    case Keys.X:
                        this.Close();
                        Application.Exit();
                        break;
        }   
        return base.ProcessCmdKey(ref msg, keyData);
    }

    private void InvertRegion()
    {
        // Graphics g = this.CreateGraphics();
        // bool result = InvertRect( g.GetHdc(), ref this.rect);
        ControlPaint.FillReversibleRectangle(this.rect, Color.Black);
        ControlPaint.DrawReversibleFrame(this.rect, Color.Black, FrameStyle.Dashed);
        return;
    }


    #region Windows Form Designer generated code

    private void InitializeComponent()
    {

                // 
                // Form1
                // 

                this.AutoSize = false;
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(screen_width, screen_height);
                this.Name = "Region Invert";
                this.Text = "Region Invert";
                this.ResumeLayout(false);
            this.PerformLayout();
    }   

    #endregion

    [STAThread]
    static void Main()
    {
            Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new RegionInvert());
    }
    }
}

However, the problem is that the code runs once. After that, it does not run again. Following is the way to use it:

  • Move mouse to start position
  • Press a
  • Move mouse to end position
  • Press b
  • Press v to invert the region
  • Press v again to invert it back

However, after some time, this stops working. Not sure what could be the cause

.net
asked on Stack Overflow Oct 13, 2018 by frenzy man

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0