FormImage.cs

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace WinFormImage
{
    public partial class FormImage : Form
    {
        #region
        private Point position;
        private OpenFileDialog openFile;

        [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
        private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
        #endregion

        public FormImage()
        {
            #region
            InitializeComponent();
            openFile = new OpenFileDialog();
            openFile.Filter = "ImageFormat (*.BMP;*.GIF;*.JPG;*.PNG)|*.bmp;*.gif;*.jpg;*.png";
            this.AllowDrop = true; // 允许拖放操作。
            this.DoubleBuffered = true; // 双缓冲绘图。
            this.ShowInTaskbar = false; // 在 Windows 任务栏中隐藏窗体。
            this.TopMost = true; // 前端显示。
            this.BackColor = Color.Black;
            this.BackgroundImageLayout = ImageLayout.Zoom; // 图像按其原有的大小比例缩放。
            this.FormBorderStyle = FormBorderStyle.None; // 窗体无边框。
            this.StartPosition = FormStartPosition.CenterScreen; // 在桌面居中显示。
            this.Text = Environment.UserName;
            #endregion
        }

        #region DragEnter
        protected override void OnDragEnter(DragEventArgs e)
        {
            base.OnDragEnter(e);
            this.Activate(); // 激活窗体并给予它焦点。
            DataObject data = e.Data as DataObject;
            if (data.ContainsFileDropList())
            {
                string filePath = data.GetFileDropList()[0];
                if (Regex.IsMatch(Path.GetExtension(filePath), @".(bmp|gif|jpg|png)", RegexOptions.IgnoreCase)) // 指定不区分大小写的匹配。
                    using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(filePath)))
                    {
                        this.BackgroundImage = Image.FromStream(ms);
                        this.Size = BackgroundImage.Size;
                        Size ws = Screen.GetWorkingArea(this).Size - this.Size;
                        this.Location = new Point(ws.Width / 2, ws.Height / 2); // 在桌面居中显示。
                    }
            }
        }
        #endregion

        #region PreviewKeyDown
        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            switch (e.KeyData)
            {
                case (Keys.Control | Keys.C):
                    Clipboard.SetImage(this.BackgroundImage); // 复制图片。
                    break;
                case (Keys.Control | Keys.V):
                    if (Clipboard.ContainsImage())
                    {
                        this.BackgroundImage = Clipboard.GetImage(); // 粘贴图片。
                        this.Size = BackgroundImage.Size;
                        Size ws = Screen.GetWorkingArea(this).Size - this.Size;
                        this.Location = new Point(ws.Width / 2, ws.Height / 2);
                    }
                    break;
                case (Keys.Control | Keys.O):
                    if (openFile.ShowDialog(this) == DialogResult.OK)
                        using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(openFile.FileName)))
                        {
                            this.BackgroundImage = Image.FromStream(ms); // 加载图片。
                            this.Size = BackgroundImage.Size;
                            Size ws = Screen.GetWorkingArea(this).Size - this.Size;
                            this.Location = new Point(ws.Width / 2, ws.Height / 2);
                        }
                    break;
                case (Keys.Control | Keys.S):
                    Environment.CurrentDirectory = Application.StartupPath;
                    BackgroundImage.Save("Image.png", BackgroundImage.RawFormat); // 保存图片。
                    System.Diagnostics.Process.Start("Image.png");
                    this.WindowState = FormWindowState.Minimized;
                    break;
                case (Keys.Control | Keys.D):
                    if (this.WindowState == FormWindowState.Normal)
                        this.WindowState = FormWindowState.Minimized;
                    else
                        this.WindowState = FormWindowState.Normal;
                    this.Activate();
                    break;
                case Keys.X:
                    BackgroundImage.RotateFlip(RotateFlipType.RotateNoneFlipX); // 水平对称图片。
                    this.Refresh(); // 刷新图片。
                    break;
                case Keys.Y:
                    BackgroundImage.RotateFlip(RotateFlipType.RotateNoneFlipY); // 垂直对称图片。
                    this.Refresh(); // 刷新图片。
                    break;
                case Keys.Left:
                    BackgroundImage.RotateFlip(RotateFlipType.Rotate90FlipXY); // 逆时针旋转图片90°
                    this.Size = BackgroundImage.Size;
                    this.Refresh(); // 刷新图片。
                    break;
                case Keys.Right:
                    BackgroundImage.RotateFlip(RotateFlipType.Rotate90FlipNone); // 顺时针旋转图片90°
                    this.Size = BackgroundImage.Size;
                    this.Refresh(); // 刷新图片。
                    break;
            }
        }
        #endregion

        #region FormLoad
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            AnimateWindow(this.Handle, 1000, 0x20010); // 居中逐渐显示。
        }
        #endregion

        #region FormClosing
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (MessageBox.Show(this, "确实要退出图片管理系统吗?", "系统提示!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    AnimateWindow(this.Handle, 1000, 0x10010); // 居中逐渐隐藏。
                else
                    e.Cancel = true;
            }
        }
        #endregion

        #region FormMove
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
                position = e.Location;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left)
            {
                Point dl = this.Location;
                dl.Offset(e.X - position.X, e.Y - position.Y);
                this.Location = dl;
            }
        }
        #endregion
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值