×

Varovanie

JUser: :_load: Nemôžem načítať používateľa s ID: 75
sobota, 25 február 2012 11:51 Written by 1352 times
Rate this item
(11 votes)

C# - Filter obrazovky

Tento program je akýsi filter obrazovky, je využiteľný hlavne v noci. Základom je úplne čierny Form bez okrajov, ktorý maximalizujeme, pridáme mu priehľadnosť, nastavíme aby bol na vrchu a povolíme kliknutie cez tento program. Filter sa nastavuje cez kontextové menu v tray ikone.

 NightFilter

 

using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Night_Filter
{
    public partial class Form1 : Form
    {
        // funkcie na povolenie Click through
        [DllImport("user32.dll")]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]

        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        private bool visible = true, fullscreen = true;
        private int initialStyle;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // ulozime si styl
            initialStyle = GetWindowLong(this.Handle, -20);
           // povolime Click through
            SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (visible)
            {
                Hide();
                visible = false;
                showToolStripMenuItem.Text = "Show";
            }
            else
            {
                Show();
                visible = true;
                showToolStripMenuItem.Text = "Hide";
            }
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (showToolStripMenuItem.Text)
            {
                case "Show": Show();
                              visible = true;
                              showToolStripMenuItem.Text = "Hide";
                              break;
                case "Hide": Hide();
                              visible = false;
                              showToolStripMenuItem.Text = "Show";
                              break;
            }
        }

        private void onTopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TopMost = onTopToolStripMenuItem.Checked;
            forcedOnTopToolStripMenuItem.Enabled = onTopToolStripMenuItem.Checked;
        }

        private void fullscreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fullscreen)
            {
                WindowState = FormWindowState.Normal;
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                if (clickThroughToolStripMenuItem.Checked) SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
                fullscreen = false;
            }
            else
            {
                WindowState = FormWindowState.Maximized;
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                if (clickThroughToolStripMenuItem.Checked) SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
                fullscreen = true;
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Opacity = 0.9;
            uncheck(toolStripMenuItem2, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Opacity = 0.8;
            uncheck(toolStripMenuItem3, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Opacity = 0.7;
            uncheck(toolStripMenuItem4, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            Opacity = 0.6;
            uncheck(toolStripMenuItem5, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            Opacity = 0.5;
            uncheck(toolStripMenuItem6, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            Opacity = 0.4;
            uncheck(toolStripMenuItem7, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            Opacity = 0.3;
            uncheck(toolStripMenuItem8, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            Opacity = 0.2;
            uncheck(toolStripMenuItem9, brightnessToolStripMenuItem.DropDownItems);
        }
        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            Opacity = 0.1;
            uncheck(toolStripMenuItem10, brightnessToolStripMenuItem.DropDownItems);
        }

        private void uncheck(ToolStripMenuItem exclude, ToolStripItemCollection where)
        {
            foreach (ToolStripMenuItem item in where)
            {
                if (item != exclude) item.Checked = false;
                else item.Checked = true;
            }
        }

        private void toolStripTextBox1_TextChanged(object sender, EventArgs e)  // zo stringu precita pocet percent a nastavi priehladnost
        {
            if (toolStripTextBox1.Text.Length != 0)
            {
                if (toolStripTextBox1.Text.Contains("%")) toolStripTextBox1.Text.Replace("%", "");
                try
                {
                    Opacity = Convert.ToDouble(toolStripTextBox1.Text) / 100;
                    uncheck(otherToolStripMenuItem, brightnessToolStripMenuItem.DropDownItems);
                }
                catch {}
                if (!toolStripTextBox1.Text.Contains(",")) toolStripTextBox1.Text = (Opacity * 100).ToString();
            }
            else toolStripTextBox1.Text = (Opacity * 100).ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Program si nastavi kazdych 15ms, aby bol na vrchu (aby prekryl aj kontextove menu atd)
            if (visible) TopMost = true;
        }

        private void forcedOnTopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = forcedOnTopToolStripMenuItem.Checked;
        }

        private void clickThroughToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (clickThroughToolStripMenuItem.Checked)
            {
               // zapnutie Click through
                SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
            }
            else
            {
                // nastavenim stylu okraja aplikacie sa Click through vypne
                if (fullscreenToolStripMenuItem.Checked) FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                else FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            }
        }

        private void changeColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // zmena farby (defaultna je cierna)
            colorDialog1.ShowDialog();
            BackColor = colorDialog1.Color;
        }
    }
}

 

Poznámka: Kód nie je kompletný, pre lepší prehľad som z neho vymazal "omáčky", ktoré ale neovplyvňujú hlavnú funkcionalitu programu.

Kompletný program si môžte stiahnuť v odkaze nižšie.

Last modified on pondelok, 19 december 2016 21:26