废话少说,直接上源码,都是老码农~
namespace ScreensaverCanbloom
{
public partial class Form2 : Form
{
private delegate void delegInit();
int _rowHeight = 20;
int _colWidth = 24;
Single _fontsize = 15F;
int colCnt = 0;
int rowCnt = 0;
int[] arrcol;
bool _TrueIsExit = false;
Point start = new Point(0, 0);
public Form2()
{
InitializeComponent();
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
BackColor = Color.Black;
}
private void Form2_Load(object sender, EventArgs e)
{
colCnt = Screen.PrimaryScreen.Bounds.Width / _colWidth;
rowCnt = Screen.PrimaryScreen.Bounds.Height / _rowHeight;
arrcol = new int[colCnt];
for (int i = 0; i < arrcol.Length; i++)
{
arrcol[i] = i;
}
delegInit dd = new delegInit(StartDraw);
dd.BeginInvoke(new AsyncCallback(ClearByBlack), null);
}
private void StartDraw()
{
Thread thrdcols = null;
for (int w = 0; w < colCnt; w++)
{
thrdcols = new Thread(new ThreadStart(DrawColNumber));
thrdcols.IsBackground = true;
thrdcols.Start();
}
thrdcols = null;
}
/// <summary>
/// 随机挑一列 抹黑
/// 可以改成新一个动画列 画一列
/// </summary>
/// <param name="ar"></param>
private void ClearByBlack(IAsyncResult ar)
{
if (ar.IsCompleted)
{
Thread thrdcols = null;
for (int w = 0; w < colCnt; w++)
{
thrdcols = new Thread(new ThreadStart(DrawBlack));
thrdcols.IsBackground = true;
thrdcols.Start();
}
thrdcols = null;
}
}
private void DrawBlack()
{
while (_TrueIsExit == false)
{
Thread.Sleep(1000);
while (_TrueIsExit == false)
{
int nowColIdx = new Random().Next(colCnt); //随机挑一列 抹黑
Graphics g = CreateGraphics();
for (int r = 0; r < rowCnt; r++)
{
g.FillRectangle(Brushes.Black, _colWidth * nowColIdx, _rowHeight * r, _colWidth, _rowHeight);
Thread.Sleep(20);
}
g.Dispose();
}
}
}
private void DrawColNumber()
{
while (_TrueIsExit == false)
{
int nowColIdx = getcolIndx(arrcol);
Graphics g = CreateGraphics();
for (int r = 0; r < rowCnt; r++)
{
#region 字母带头
//g.FillRectangle(Brushes.Black, _colWidth * nowColIdx, _rowHeight * r, _colWidth, _rowHeight);
//g.DrawString(((char)new Random().Next(97, 122)).ToString(),
// new Font("Arial", _fontsize), new SolidBrush(Color.FromArgb(192, 255, 136)), _colWidth * nowColIdx, _rowHeight * r);
//Thread.Sleep(1);
#endregion
g.FillRectangle(Brushes.Black, _colWidth * nowColIdx, _rowHeight * r, _colWidth, _rowHeight);
g.DrawString(new Random().Next(10).ToString(),
new Font("Arial", _fontsize), new SolidBrush(Color.FromArgb(74, 200, 47)), _colWidth * nowColIdx, _rowHeight * r);
Thread.Sleep(15);
}
g.Dispose();
//Thread.Sleep(60);
}
}
int _idx = -1;
private int getcolIndx(int[] arrcol)
{
if (_idx == arrcol.Length - 1)
{
_idx = -1;
}
_idx++;
return arrcol[_idx];
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
_TrueIsExit = true;
}
//鼠标动了则屏保中止
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
// 窗体打开时鼠标即使不动也会不停触发MouseMove事件
if (start == new Point(0, 0))
{
start = e.Location;
return;
}
else if (start != e.Location) // 判断自程序运行后,鼠标的位置是否变动
{
Cursor.Show();
Application.Exit();
};
}
}
}
关于屏保在系统屏幕保护对话框的编码问题
if (args[0].StartsWith("/c") || args[0].StartsWith("/C") || args[0].StartsWith("-c") || args[0].StartsWith("-C"))
{
//MessageBox.Show("此屏幕保护没有可供设置的选项!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmCfg());
return;
}
if (args[0].Equals("/p") || args[0].Equals("/P") || args[0].Equals("-p") || args[0].Equals("-P"))
{
int result = -1;
if (args.Length < 2) return;
if (!int.TryParse(args[1], out result)) return; // 参数/p之后有紧随另一个整数参数(句柄)
IntPtr handle = new IntPtr(result);
Form2 form2 = new Form2(); // 获取屏幕保护真实运行主窗口的句柄
Graphics graphics = Graphics.FromHwnd(handle); //根据句柄创建画对象
while (IsWindowVisible(handle)) // 在系统的"屏幕保护程序设置"的预览窗口显示输出
{
aa(graphics); //把屏保护的内容输入在系统屏幕保护对话框的窗体中的屏幕画面中
System.Threading.Thread.Sleep(100); // 间隔一定时间形成动画效果
}
return;
}
Form2是屏保程序运行的窗体,通过aa()这个函数再是一个窗体句柄,实现系统屏幕保护对话框中的显示器上预览,否则里面小屏幕是黑屏。