其實所謂Windows前景色彩調節器就是利用Winform表單遮蓋整個Windows地區。主要要求實現視窗透明,且滑鼠可以穿過表單點擊其他程式。
痛點就是怎麼樣讓滑鼠穿透表單,代碼也是從網上找的,現在找不到原連結了:)
原理就是調用Windows API設定視窗的屬性。
代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
/*
* 作者:Billy Qing
* 日期:2009年11月20日
* 說明:EyesBaby Windows 前景視窗。
* 版本:1.0
*/
namespace EyesBaby
{
public partial class WinScreenAdjust : Form
{
/*
* 下面這段代碼主要用來調用Windows API實現表單透明(滑鼠可以穿透表單)
* 也是從網上找的:)
*/
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern long GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
[DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
private static extern int SetLayeredWindowAttributes(IntPtr Handle, int crKey, byte bAlpha, int dwFlags);
const int GWL_EXSTYLE = -20;
const int WS_EX_TRANSPARENT = 0x20;
const int WS_EX_LAYERED = 0x80000;
const int LWA_ALPHA = 2;
public WinScreenAdjust()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 取消表單工作列
ShowInTaskbar = false;
// 表單位於Windows最頂部
this.TopMost = true;
// 去除表單邊框
this.FormBorderStyle = FormBorderStyle.None;
// 設定表單最大化大小(除底部工作列部分)
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
// 設定Windows視窗狀態為最大化模式
this.WindowState = FormWindowState.Maximized;
// 設定Windows屬性
SetWindowLong(this.Handle, GWL_EXSTYLE, GetWindowLong(this.Handle, GWL_EXSTYLE) | WS_EX_TRANSPARENT | WS_EX_LAYERED);
SetLayeredWindowAttributes(this.Handle, 0, 128, LWA_ALPHA);
}
}
}
至於EyesBaby中給表單設定顏色部分就比較簡單了。
代碼:
// 開啟顏色選擇對話方塊 ,並分析是否選擇了對話方塊中的確定按鈕
if (this.colColorAdjust.ShowDialog() == DialogResult.OK)
{
int[] item=colColorAdjust.CustomColors;
// 將先中的顏色設定為表單的背景色
this.winAdjust.BackColor = colColorAdjust.Color;
// 儲存到設定檔
ConfigHelper.WinForeColor = this.winAdjust.BackColor.Name;
}
出處:http://yizhuqing.cnblogs.com/