You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
402 lines
16 KiB
402 lines
16 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
using System.IO;
|
|
using Transitionals;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows.Forms;
|
|
using System.Threading;
|
|
using System.Drawing;
|
|
using System.Configuration;
|
|
|
|
namespace PCScreenSavers.Common
|
|
{
|
|
public static class MyConfig
|
|
{
|
|
|
|
//public static void ConfigInit()
|
|
//{
|
|
// string Filename = System.Environment.CurrentDirectory + "\\app.config";
|
|
// if (File.Exists(Filename) != true)
|
|
// {
|
|
// SetVaule("ServerIp", "192.168.1.126"); // 包含了 自动创建 的功能
|
|
// SetVaule("ServerPort", "1261");
|
|
// }
|
|
//}
|
|
|
|
///<summary>
|
|
///返回*.exe.config文件中appSettings配置节的value项
|
|
///</summary>
|
|
///<param name="strKey"></param>
|
|
///<returns></returns>
|
|
public static string GetVaule(string strKey)
|
|
{
|
|
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
|
|
file.ExeConfigFilename = "PCScreenSavers.exe.config";
|
|
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
|
|
foreach (string key in config.AppSettings.Settings.AllKeys)
|
|
{
|
|
if (key == strKey)
|
|
{
|
|
return config.AppSettings.Settings[strKey].Value.ToString();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
///<summary>
|
|
///在*.exe.config文件中appSettings配置节增加一对键值对
|
|
///</summary>
|
|
///<param name="newKey"></param>
|
|
///<param name="newValue"></param>
|
|
public static void SetVaule(string newKey, string newValue)
|
|
{
|
|
ExeConfigurationFileMap file = new ExeConfigurationFileMap();
|
|
file.ExeConfigFilename = "PCScreenSavers.exe.config";
|
|
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
|
|
bool exist = false;
|
|
foreach (string key in config.AppSettings.Settings.AllKeys)
|
|
{
|
|
if (key == newKey)
|
|
{
|
|
exist = true;
|
|
}
|
|
}
|
|
if (exist)
|
|
{
|
|
config.AppSettings.Settings.Remove(newKey);
|
|
}
|
|
config.AppSettings.Settings.Add(newKey, newValue);
|
|
config.Save(ConfigurationSaveMode.Modified);
|
|
ConfigurationManager.RefreshSection("appSettings");
|
|
}
|
|
//public static void testc()
|
|
//{
|
|
// MyConfig.SetVaule("path", "");
|
|
// // cancle.WriteLine(MyConfig.GetVaule("path"));
|
|
//}
|
|
|
|
}
|
|
public static class CommonMethod
|
|
{
|
|
/// <summary>
|
|
/// 反序列化XML为类实例
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="xmlObj"></param>
|
|
/// <returns></returns>
|
|
public static T DeserializeXML<T>(string xmlObj)
|
|
{
|
|
try
|
|
{
|
|
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
|
using (StringReader reader = new StringReader(xmlObj))
|
|
{
|
|
return (T)serializer.Deserialize(reader);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//MessageBox.Show(ex.ToString());
|
|
return default(T);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 序列化类实例为XML
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static string SerializeXML<T>(T obj)
|
|
{
|
|
try
|
|
{
|
|
using (StringWriter writer = new StringWriter())
|
|
{
|
|
new XmlSerializer(obj.GetType()).Serialize((TextWriter)writer, obj);
|
|
return writer.ToString();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换效果
|
|
/// </summary>
|
|
/// <param name="effect">百叶窗-0,遮帘 -1,淡化 - 2,渐变 -3,溶解 - 4,旋转 - 5</param>
|
|
/// <returns></returns>
|
|
public static Transition GetTransition(string effect)
|
|
{
|
|
//所有的变换
|
|
//Transitionals.Transitions.StarTransition //星
|
|
//Transitionals.Transitions.RotateTransition //3d旋转
|
|
//Transitionals.Transitions.VerticalWipeTransition//下拉
|
|
//Transitionals.Transitions.PageTransition //翻页
|
|
//Transitionals.Transitions.RollTransition //旋转出
|
|
//Transitionals.Transitions.DiamondsTransition //棋盒棱形
|
|
//Transitionals.Transitions.VerticalBlindsTransition //垂直百叶窗
|
|
//Transitionals.Transitions.HorizontalWipeTransition //左拉
|
|
//Transitionals.Transitions.FadeAndBlurTransition //淡入
|
|
//Transitionals.Transitions.ExplosionTransition //球形散开
|
|
//Transitionals.Transitions.CheckerboardTransition //棋盒方形
|
|
//Transitionals.Transitions.TranslateTransition //飞入
|
|
//Transitionals.Transitions.RotateWipeTransition //旋转擦除
|
|
//Transitionals.Transitions.MeltTransition //柱状
|
|
//Transitionals.Transitions.DiagonalWipeTransition //斜擦除
|
|
//Transitionals.Transitions.FlipTransition //单面翻书
|
|
//Transitionals.Transitions.DotsTransition //球状棋盒
|
|
//Transitionals.Transitions.FadeAndGrowTransition //淡入
|
|
//Transitionals.Transitions.DoubleRotateWipeTransition //双线擦除
|
|
//Transitionals.Transitions.DoorTransition //门状
|
|
//Transitionals.Transitions.HorizontalBlindsTransition //水平百叶窗
|
|
//Transitionals.Transitions.FadeTransition //溶解
|
|
|
|
switch (effect)
|
|
{
|
|
case "马赛克":
|
|
return new Transitionals.Transitions.CheckerboardTransition();
|
|
case "上下滑动":
|
|
return new Transitionals.Transitions.VerticalWipeTransition();
|
|
case "左右滑动":
|
|
return new Transitionals.Transitions.HorizontalWipeTransition();
|
|
case "渐入":
|
|
return new Transitionals.Transitions.FadeTransition();
|
|
case "4":
|
|
return new Transitionals.Transitions.FadeTransition();
|
|
case "5":
|
|
return new Transitionals.Transitions.RollTransition();
|
|
default:
|
|
return new Transitionals.Transitions.FadeTransition();
|
|
}
|
|
}
|
|
|
|
///// <summary>
|
|
///// 以逆时针为方向对图像进行旋转
|
|
///// </summary>
|
|
///// <param name="b">位图流</param>
|
|
///// <param name="angle">旋转角度[0,360](前台给的)</param>
|
|
///// <returns></returns>
|
|
//public Bitmap RotateImg(System.Drawing.Image b, int angle)
|
|
//{
|
|
// angle = angle % 360;
|
|
|
|
// //弧度转换
|
|
// double radian = angle * Math.PI / 180.0;
|
|
// double cos = Math.Cos(radian);
|
|
// double sin = Math.Sin(radian);
|
|
|
|
// //原图的宽和高
|
|
// int w = (int)b.Width;
|
|
// int h = (int)b.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;
|
|
|
|
// //计算偏移量
|
|
// System.Drawing.Point Offset = new System.Drawing.Point((int)(W - w) / 2, (int)(H - h) / 2);
|
|
|
|
// //构造图像显示区域:让图像的中心与窗口的中心点一致
|
|
// System.Drawing.Rectangle rect = new System.Drawing.Rectangle(Offset.X, Offset.Y, w, h);
|
|
// System.Drawing.Point center = new System.Drawing.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(b, rect);
|
|
|
|
// //重至绘图的所有变换
|
|
// g.ResetTransform();
|
|
// g.Save();
|
|
// g.Dispose();
|
|
|
|
// //保存旋转后的图片
|
|
// b.Dispose();
|
|
// //dsImage.Save("FocusPoint.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
// return dsImage;
|
|
|
|
//}
|
|
|
|
// 映射 DEVMODE 结构
|
|
// 可以参照 DEVMODE结构的指针定义:
|
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd183565(v=vs.85).aspx
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
public struct DEVMODE
|
|
{
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
|
public string dmDeviceName;
|
|
|
|
public short dmSpecVersion;
|
|
public short dmDriverVersion;
|
|
public short dmSize;
|
|
public short dmDriverExtra;
|
|
public int dmFields;
|
|
public int dmPositionX;
|
|
public int dmPositionY;
|
|
public int dmDisplayOrientation;
|
|
public int dmDisplayFixedOutput;
|
|
public short dmColor;
|
|
public short dmDuplex;
|
|
public short dmYResolution;
|
|
public short dmTTOption;
|
|
public short dmCollate;
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
|
public string dmFormName;
|
|
|
|
public short dmLogPixels;
|
|
public short dmBitsPerPel;
|
|
public int dmPelsWidth;
|
|
public int dmPelsHeight;
|
|
public int dmDisplayFlags;
|
|
public int dmDisplayFrequency;
|
|
public int dmICMMethod;
|
|
public int dmICMIntent;
|
|
public int dmMediaType;
|
|
public int dmDitherType;
|
|
public int dmReserved1;
|
|
public int dmReserved2;
|
|
public int dmPanningWidth;
|
|
public int dmPanningHeight;
|
|
};
|
|
|
|
// Win32 函数在托管环境下的声明
|
|
public class NativeMethods
|
|
{
|
|
// 平台调用的申明
|
|
[DllImport("user32.dll")]
|
|
public static extern int EnumDisplaySettings(
|
|
string deviceName, int modeNum, ref DEVMODE devMode);
|
|
[DllImport("user32.dll")]
|
|
public static extern int ChangeDisplaySettings(
|
|
ref DEVMODE devMode, int flags);
|
|
|
|
// 控制改变屏幕分辨率的常量
|
|
public const int ENUM_CURRENT_SETTINGS = -1;
|
|
public const int CDS_UPDATEREGISTRY = 0x01;
|
|
public const int CDS_TEST = 0x02;
|
|
public const int DISP_CHANGE_SUCCESSFUL = 0;
|
|
public const int DISP_CHANGE_RESTART = 1;
|
|
public const int DISP_CHANGE_FAILED = -1;
|
|
|
|
// 控制改变方向的常量定义
|
|
public const int DMDO_DEFAULT = 0;
|
|
public const int DMDO_90 = 1;
|
|
public const int DMDO_180 = 2;
|
|
public const int DMDO_270 = 3;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 改变分辨率
|
|
/// </summary>
|
|
/// <param name="displayOrientation">0:切换横屏,1:切换竖屏</param>
|
|
public static int ChangeResolution(int displayOrientation)
|
|
{
|
|
// 初始化 DEVMODE结构
|
|
DEVMODE devmode = new DEVMODE();
|
|
devmode.dmDeviceName = new String(new char[32]);
|
|
devmode.dmFormName = new String(new char[32]);
|
|
devmode.dmSize = (short)Marshal.SizeOf(devmode);
|
|
|
|
if (0 != NativeMethods.EnumDisplaySettings(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref devmode))
|
|
{
|
|
//// 改变屏幕分辨率
|
|
//int iRet = NativeMethods.ChangeDisplaySettings(ref devmode, NativeMethods.CDS_TEST);
|
|
|
|
//if (iRet == NativeMethods.DISP_CHANGE_FAILED)
|
|
//{
|
|
// //MessageBox.Show("不能执行你的请求", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
//}
|
|
//else
|
|
//{
|
|
// int temp = devmode.dmPelsWidth;
|
|
// devmode.dmPelsWidth = devmode.dmPelsHeight;
|
|
// devmode.dmPelsHeight = temp;
|
|
// if (displayOrientation == 0)
|
|
// devmode.dmDisplayOrientation = NativeMethods.DMDO_90;
|
|
// else
|
|
// devmode.dmDisplayOrientation = NativeMethods.DMDO_DEFAULT;
|
|
|
|
// iRet = NativeMethods.ChangeDisplaySettings(ref devmode, NativeMethods.CDS_UPDATEREGISTRY);
|
|
|
|
// switch (iRet)
|
|
// {
|
|
// // 成功改变
|
|
// case NativeMethods.DISP_CHANGE_SUCCESSFUL:
|
|
// {
|
|
// break;
|
|
// }
|
|
// case NativeMethods.DISP_CHANGE_RESTART:
|
|
// {
|
|
// // MessageBox.Show("你需要重新启动电脑设置才能生效", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
// break;
|
|
// }
|
|
// default:
|
|
// {
|
|
// // MessageBox.Show("改变屏幕分辨率失败", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
// break;
|
|
// }
|
|
// }
|
|
//}
|
|
}
|
|
|
|
if (devmode.dmDisplayOrientation == 0)
|
|
return 0;
|
|
else if (devmode.dmDisplayOrientation == 1)
|
|
return 90;
|
|
else if (devmode.dmDisplayOrientation == 2)
|
|
return 180;
|
|
else
|
|
return -90;
|
|
}
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern void keybd_event
|
|
(
|
|
byte bVk,// 虚拟键值
|
|
byte bScan,// 硬件扫描码
|
|
uint dwFlags,// 动作标识
|
|
IntPtr dwExtraInfo// 与键盘动作关联的辅加信息
|
|
);
|
|
|
|
/// <summary>
|
|
/// 截屏
|
|
/// </summary>
|
|
public static string PrintScreen()
|
|
{
|
|
string path = Environment.CurrentDirectory + @"\Screen.jpg";
|
|
Bitmap bit = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
|
|
Graphics g = Graphics.FromImage(bit);
|
|
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bit.Size);
|
|
|
|
try
|
|
{
|
|
if (File.Exists(path))
|
|
File.Delete(path);
|
|
bit.Save(path);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
finally
|
|
{
|
|
g.Dispose();
|
|
bit.Dispose();
|
|
}
|
|
|
|
return path;
|
|
}
|
|
}
|
|
}
|
|
|