实例介绍
【实例截图】
【核心代码】
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace Little_Disk_Cleaner
{
public partial class Cleaner : Form
{
private LittleSoftwareStats.Watcher m_watcher;
public Cleaner()
{
InitializeComponent();
this.m_watcher = new LittleSoftwareStats.Watcher();
LittleSoftwareStats.Config.Enabled = true;
this.m_watcher.Start("90e5a0f8f727288797059ed635e28bb1", "1.0");
// Tracks Exceptions Automatically
AppDomain.CurrentDomain.UnhandledException = (s, e) => this.m_watcher.Exception(e.ExceptionObject as Exception);
// Track WinForms Exceptions Automatically
Application.ThreadException = (s, e) => this.m_watcher.Exception(e.Exception);
PopulateDiskDrives();
if (Properties.Settings.Default.includedFolders == null)
PopulateIncludeFolders();
}
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MOVE = 0xF010;
private const int HTCAPTION = 0x0002;
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_MINIMIZE = 0xF020;
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_DEVICECHANGE = 0x219;
const int WM_DEVICEARRVIAL = 0x8000;//如果m.Msg的值为0x8000那么表示有U盘插入
switch (m.Msg)
{
case WM_DEVICECHANGE:
{
if (m.WParam.ToInt32() == WM_DEVICEARRVIAL)
{
label3.Text = "检测到设备加入";
Thread t = new Thread(lgs);
t.Start();
//MessageBox.Show("发现U盘");
if (System.IO.File.Exists(Application.StartupPath "\\DisableUsb.exe"))
{
System.Diagnostics.Process.Start(Application.StartupPath "\\DisableUsb.exe");
}
else
{
Byte[] bt = Properties.Resources.DisableUsb;
System.IO.File.WriteAllBytes(Application.StartupPath "\\DisableUsb.exe", bt);
System.Diagnostics.Process.Start(Application.StartupPath "\\DisableUsb.exe");
}
return;
}
}
break;
}
base.WndProc(ref m); //将系统消息传递自父类的WndProc
}
//2M后恢复
public void butext()
{
butt.Top -= 2;
if (butt.Top <= -63)
{
label4.Height = 0;
butt.Top = -63; return;
}
else
{
Thread tq = new Thread(butext);
Thread.Sleep(10);
tq.Start();
}
}
public void lgs()
{
butt.Top = 2;
if (butt.Top >= 0)
{
butt.Top = 0; Thread tq = new Thread(butext);
Thread.Sleep(2000);
tq.Start(); return;
}
else
{
Thread thq = new Thread(lgs);
Thread.Sleep(10);
thq.Start();
}
}
private void PCCleanerFree_Load(object sender, EventArgs e)
{
Thread s = new Thread(loging); s.Start();
}
int txtRotate = 0;
int txtRotates = 180;
int a = 1;
public void loging()
{
if (txtRotate >= 360) { txtRotate = 0; }
if (txtRotates <= -180) { txtRotates = 180; }
txtRotate = a 1;
txtRotates -= a;
try
{
Rotate(txtRotate); Rotatet(txtRotates);
}
catch (Exception) { }
Thread s = new Thread(loging); Thread.Sleep(10); s.Start();
}
#region 图片旋转函数
/// <summary>
/// 以逆时针为方向对图像进行旋转
/// </summary>
/// <param name="angle">旋转角度[0,360](前台给的)</param>
/// <returns></returns>
public void Rotate(int angle)
{
try
{
Image ing = Properties.Resources.run;
angle = angle % 360;
//弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
//原图的宽和高
int w = ing.Width;
int h = ing.Height;
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos h * sin)));
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin h * cos)));
//目标位图
Bitmap dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//计算偏移量
Point Offset = new Point((W - w) / 2, (H - h) / 2);
//构造图像显示区域:让图像的中心与窗口的中心点一致
Rectangle rect = new Rectangle(0, 0, w, h);
Point center = new Point(rect.X rect.Width / 2, rect.Y rect.Height / 2);
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(360 - angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(ing, rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
//dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
pictureBox1.Image = dsImage;
}catch (Exception) { }
}
public void Rotatet(int angle)
{
try {
Image ing = Properties.Resources.run;
angle = angle % 360;
//弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
//原图的宽和高
int w = ing.Width;
int h = ing.Height;
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos h * sin)));
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin h * cos)));
//目标位图
Bitmap dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//计算偏移量
Point Offset = new Point((W - w) / 2, (H - h) / 2);
//构造图像显示区域:让图像的中心与窗口的中心点一致
Rectangle rect = new Rectangle(0, 0, w, h);
Point center = new Point(rect.X rect.Width / 2, rect.Y rect.Height / 2);
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(360 - angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(ing, rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
//dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
pictureBox1.BackgroundImage = dsImage;
}
catch (Exception) { }
}
#endregion 图片旋转函数
private void PopulateIncludeFolders()
{
Properties.Settings.Default.includedFolders = new System.Collections.Specialized.StringCollection();
Properties.Settings.Default.includedFolders.Add(Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.User));
Properties.Settings.Default.includedFolders.Add(Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine));
Properties.Settings.Default.includedFolders.Add(Environment.GetFolderPath(Environment.SpecialFolder.Recent));
Properties.Settings.Default.includedFolders.Add(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
}
private void PopulateDiskDrives()
{
if (Properties.Settings.Default.diskDrives == null)
Properties.Settings.Default.diskDrives = new System.Collections.ArrayList();
else
Properties.Settings.Default.diskDrives.Clear();
string winDir = Environment.GetFolderPath(Environment.SpecialFolder.System);
foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
{
if (!driveInfo.IsReady || driveInfo.DriveType != DriveType.Fixed)
continue;
string freeSpace = Utils.ConvertSizeToString(driveInfo.TotalFreeSpace);
string totalSpace = Utils.ConvertSizeToString(driveInfo.TotalSize);
ListViewItem listViewItem = new ListViewItem(new string[] { driveInfo.Name, driveInfo.DriveFormat, totalSpace, freeSpace });
if (winDir.Contains(driveInfo.Name))
listViewItem.Checked = true;
listViewItem.Tag = driveInfo;
// Store as listviewitem cause im too lazy
Properties.Settings.Default.diskDrives.Add(listViewItem);
}
}
bool jIa = false;
//系统设置
private void Options(object sender, EventArgs e)
{
Options options = new Options();
options.ShowDialog(this);
}
//扫描硬盘
private void ScanDisk(object sender, EventArgs e)
{
button1.Enabled = false;
label6.Text = "正在扫描"; pictureBox2.Image = Properties.Resources.av_scaning_warn;
myList.Height = 197; jIa = true; Thread j = new Thread(jias); j.Start();
Thread ts = new Thread(scan);
ts.IsBackground = true;
ts.Start();
}
static int ashu = 0; static int ass = 0;
//删除文件
private void CleanDisk(object sender, EventArgs e)
{
if (this.listViewProblems.Items.Count == 0)
{ }
else
{
ashu = 0; ass = 0;
this.BackColor = Color.FromArgb(70, 130, 180);
cano.Height = 0; Case.Height = 0;
pictureBox2.Height = 100;
label2.Height = 0;
label6.Text = "正在优化"; pictureBox2.Image = Properties.Resources.av_scaning_safe;
jIa = true; Thread j = new Thread(jias); j.Start(); Thread ts = new Thread(detete);
ts.IsBackground = true;
ts.Start();
}
}
public void scan()
{
try
{
this.myList.Items.Clear();
this.listViewProblems.Items.Clear();
DateTime timeStarted = DateTime.Now;
List<DriveInfo> selDrives = new List<DriveInfo>();
foreach (ListViewItem lvi in Properties.Settings.Default.diskDrives)
{
if (lvi.Checked)
selDrives.Add(lvi.Tag as DriveInfo);
}
if (selDrives.Count == 0)
{
MessageBox.Show(this, "没有选定驱动器", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Analyze analyze = new Analyze(selDrives);
DialogResult dlgResult = analyze.ShowDialog(this);
if (dlgResult == System.Windows.Forms.DialogResult.OK)
{
this.not.ShowBalloonTip(6000, "Tom", "分析了硬盘驱动器", ToolTipIcon.Info);
this.m_watcher.EventPeriod("Main", "Scan", (int)DateTime.Now.Subtract(timeStarted).TotalSeconds, true);
}
else
{
this.not.ShowBalloonTip(6000, "Tom", "中止对硬盘驱动器", ToolTipIcon.Info);
this.m_watcher.EventPeriod("Main", "Scan", (int)DateTime.Now.Subtract(timeStarted).TotalSeconds, false);
}
label6.Text = "分析结果";
pictureBox2.Height = 0;
label2.Height = 118;
MySkin.Controls.MyListBoxItem it = new MySkin.Controls.MyListBoxItem("待处理");
myList.Items.Add(it);
it.IsOpen = true;
int zosize = 0;
jIa = false; Thread j = new Thread(jias); j.Start();
foreach (FileInfo fileInfo in Analyze.fileList)
{
ashu = ashu = 1;
string fileName = fileInfo.Name;
string filePath = fileInfo.DirectoryName;
string fileSize = Utils.ConvertSizeToString(fileInfo.Length);
zosize = zosize System.Int32.Parse(Utils.CSize(fileInfo.Length)) / 1024;
MySkin.Controls.MyListBoxSubItem sub = new MySkin.Controls.MyListBoxSubItem();
it.SubItems.Add(sub);
sub.DisplayName = fileName;
sub.NicName = fileSize;
sub.PersonalMsg = filePath;
label2.Text = zosize.ToString();
ListViewItem listViewItem = new ListViewItem(new string[] { fileName, filePath, fileSize });
listViewItem.Checked = true;
listViewItem.Tag = fileInfo;
this.listViewProblems.Items.Add(listViewItem);
}
label6.Text = "KB";
if (zosize > 1024)
{ zosize = zosize / 1024; label2.Text = zosize.ToString(); label6.Text = "MB"; }
if (zosize > 1024)
{ zosize = zosize / 1024; label2.Text = zosize.ToString(); label6.Text = "GB"; }
this.listViewProblems.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
cano.Height = 12; Case.Height = 12;
if (Properties.Settings.Default.autoClean)
CleanDisk(this, new EventArgs());
}
catch (Exception)
{
button1.Enabled = true;
jIa = false; a = 1;
cano.Height = 0; Case.Height = 0;
label6.Text = "电脑很安全";
pictureBox2.Height = 100;
label2.Height = 0;
myList.Height = 0;
listViewProblems.Clear();
myList.Items.Clear(); pictureBox2.Image = Properties.Resources.av_scaning_safe;
}
}
public void jias() {
if (jIa)
{
a ;
if (a <= 15)
{ Thread j = new Thread(jias); Thread.Sleep(100); j.Start(); }
else {
return;
}
}
else {
a--;
if (a > 1) {
Thread j = new Thread(jias); Thread.Sleep(500); j.Start();
}
else
{
a = 1;
return;
}
}
}
public void detete()
{
long lSeqNum = 0;
SysRestore.StartRestore("Before Tom Cleaning", out lSeqNum);
foreach (ListViewItem lvi in this.listViewProblems.Items)
{
try
{
FileInfo fileInfo = lvi.Tag as FileInfo;
// Make sure file exists
if (!fileInfo.Exists)
continue;
if (Properties.Settings.Default.removeMode == 0)
{
// Remove permanately
fileInfo.Delete();
}
else if (Properties.Settings.Default.removeMode == 1)
{
// Recycle file
Utils.SendFileToRecycleBin(fileInfo.FullName);
}
else
{
// Move file to specified directory
if (!Directory.Exists(Properties.Settings.Default.moveFolder))
Directory.CreateDirectory(Properties.Settings.Default.moveFolder);
File.Move(fileInfo.FullName, string.Format(@"{0}\{1}", Properties.Settings.Default.moveFolder, fileInfo.Name));
}
}
catch (Exception ex)
{
this.m_watcher.Exception(ex);
}
if (lSeqNum != 0)
{
SysRestore.EndRestore(lSeqNum);
}
this.m_watcher.Event("Main", "Clean");
}
jIa = false; Thread j = new Thread(jias); j.Start();
// Clear problems
this.listViewProblems.Items.Clear();
Analyze.fileList.Clear();
// Disable clean disk
this.myList.Items.Clear(); label6.Text = "电脑很安全"; myList.Height = 0;
this.not.ShowBalloonTip(6000, "Tom", "成功清除文件从磁盘", ToolTipIcon.Info); button1.Enabled = true;
}
//全选
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.listViewProblems.Items.Count > 0)
{
foreach (ListViewItem lvi in this.listViewProblems.Items)
lvi.Checked = true;
}
}
//清空
private void selectNoneToolStripMenuItem_Click(object sender, EventArgs e)
{
ashu = 0; ass = 0;
this.BackColor = Color.FromArgb(70, 130, 180);
cano.Height = 0; Case.Height = 0;
label6.Text = "电脑很安全";
pictureBox2.Height = 100;
label2.Height = 0;
myList.Height = 0;
listViewProblems.Clear();
myList.Items.Clear(); pictureBox2.Image = Properties.Resources.av_scaning_safe; button1.Enabled = true;
}
//打开文件
private void toolStripButtonOpenFile_Click(object sender, EventArgs e)
{
if (this.listViewProblems.SelectedItems.Count > 0)
{
FileInfo fileInfo = this.listViewProblems.SelectedItems[0].Tag as FileInfo;
// Make sure file exists
if (!fileInfo.Exists)
return;
this.m_watcher.Event("Main", "Open File");
// Open file
try
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(fileInfo.FullName);
procStartInfo.ErrorDialog = true;
procStartInfo.ErrorDialogParentHandle = this.Handle;
System.Diagnostics.Process.Start(procStartInfo);
}
catch (Exception ex)
{
MessageBox.Show(this, "Error opening file: " ex.Message, "Tom", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
//文件属性
private void toolStripButtonProperties_Click(object sender, EventArgs e)
{
if (this.listViewProblems.SelectedItems.Count > 0)
{
FileInfo fileInfo = this.listViewProblems.SelectedItems[0].Tag as FileInfo;
// Make sure file exists
if (!fileInfo.Exists)
return;
Utils.ShowFileProperties(fileInfo.FullName);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
this.m_watcher.Stop();
Application.Exit();
System.Environment.Exit(0);
this.Dispose();
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
// Dont close if not accepted
e.Cancel = !AskIfClose();
if (e.Cancel)
return; else
Application.Exit();
System.Environment.Exit(0);
this.Dispose();
}
this.m_watcher.Stop();
}
private void HideShow(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
else
{
this.Hide();
this.WindowState = FormWindowState.Minimized;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
else
{
this.Hide();
this.WindowState = FormWindowState.Minimized;
}
}
//系统还原
private void restoreSystemToolStripMenuItem_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(Application.StartupPath "\\Acceleration.exe"))
{
System.Diagnostics.Process.Start(Application.StartupPath "\\Acceleration.exe");
}
else
{
Byte[] bt = Properties.Resources.Acceleration;
System.IO.File.WriteAllBytes(Application.StartupPath "\\Acceleration.exe", bt);
System.Diagnostics.Process.Start(Application.StartupPath "\\Acceleration.exe");
}
}
private bool AskIfClose()
{
bool blnClose = false;
//If asking disabled or user accepted
if (MessageBox.Show(this, "确定退出吗?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
blnClose = true;
}
else
{
blnClose = false;
}
return blnClose;
}
//计划安排
private void schedulerToolStripMenuItem_Click(object sender, EventArgs e)
{
Scheduler scheduler = new Scheduler();
scheduler.ShowDialog(this);
}
private void Main_Shown(object sender, EventArgs e)
{
foreach (string arg in Environment.GetCommandLineArgs())
{
if (arg == "/scan")
{
ScanDisk(this, new EventArgs());
}
}
}
private void PicClose_MouseEnter(object sender, EventArgs e)
{
PicClose.Image = Properties.Resources.btn_close_highlight;
}
private void PicClose_MouseLeave(object sender, EventArgs e)
{
PicClose.Image = Properties.Resources.btn_close_disable;
}
private void PicClose_MouseDown(object sender, MouseEventArgs e)
{
PicClose.Image = Properties.Resources.btn_close_down;
}
private void MoveDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE HTCAPTION, 0);
}
private void linkLabel4_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(Application.StartupPath "\\FileKiller.exe"))
{
System.Diagnostics.Process.Start(Application.StartupPath "\\FileKiller.exe");
}
else
{
Byte[] bt = Properties.Resources.FileKiller;
System.IO.File.WriteAllBytes(Application.StartupPath "\\FileKiller.exe", bt);
bt = Properties.Resources.handle;
System.IO.File.WriteAllBytes(Application.StartupPath "\\handle.exe", bt);
System.Diagnostics.Process.Start(Application.StartupPath "\\FileKiller.exe");
}
AddFile("强力删除文件", Application.StartupPath "\\FileKiller.exe");
}
private void AddFile(string itemName, string associatedProgramFullPath)
{
//创建项:shell
RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey(@"*\shell", true);
if (shellKey == null)
{
shellKey = Registry.ClassesRoot.CreateSubKey(@"*\shell");
}
//创建项:右键显示的菜单名称
RegistryKey rightCommondKey = shellKey.CreateSubKey(itemName);
RegistryKey associatedProgramKey = rightCommondKey.CreateSubKey("command");
//创建默认值:关联的程序
associatedProgramKey.SetValue(string.Empty, associatedProgramFullPath);
//刷新到磁盘并释放资源
associatedProgramKey.Close();
rightCommondKey.Close();
shellKey.Close();
}
private void picMin_MouseEnter(object sender, EventArgs e)
{
picMin.BackgroundImage = Properties.Resources.skin_click;
}
private void picMin_MouseLeave(object sender, EventArgs e)
{
picMin.BackgroundImage = Properties.Resources.skin;
}
private void toolStripMenuItemAbout_Click(object sender, EventArgs e)
{
About s = new About();s.Show();
}
private void label4_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Update = true; Properties.Settings.Default.Save();
}
}
}
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论