📄 mainform.cs
字号:
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;
using System.Drawing.Imaging;
namespace Psp_wallpaper_mini
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
private void ExitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
////移动窗体
Point mouse_offset=new Point();
public void moveForm(object sender,MouseEventArgs e)
{
Point MousePos = new Point();
MousePos = mainForm.MousePosition;
MousePos.Offset(-mouse_offset.X, -mouse_offset.Y);
this .Location = MousePos;
}
private void toolStrip1_MouseDown(object sender, MouseEventArgs e)
{
mouse_offset = new Point(e.X, e.Y);
}
private void mainForm_MouseDown(object sender, MouseEventArgs e)
{
mouse_offset = new Point(e.X, e.Y);
}
private void mainForm_MouseMove_1(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
{
moveForm(sender, e);
}
}
private void toolStrip1_MouseMove_1(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
{
moveForm(sender, e);
}
}
//截屏
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public class API
{
[DllImport("user32.dll")]
public static extern bool GetCursorPos(ref Point lpPoint);
[DllImport("user32.dll")]
public static extern int WindowFromPoint(Point lpPoint);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern int GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hwnd, ref RECT rc);
[DllImport("user32.dll")]
public static extern int GetWindowDC(int hwnd);
[DllImport("Gdi32.dll")]
public static extern bool BitBlt(
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
);
[DllImportAttribute("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);
}//封装一部分api的类
//抓取句柄所指窗口
public Bitmap getscreenfromhandle(IntPtr hwnd)
{
RECT rc = new RECT();
API.GetWindowRect(hwnd, ref rc);
return getscreen(rc.left, rc.top + 25, 480, 272);
}
//获得屏幕指定区域
public Bitmap getscreen(int left, int top, int width, int height)
{
IntPtr dc1 = API.CreateDC("DISPLAY", null, null, (IntPtr)null);
Graphics newGraphics = Graphics.FromHdc(dc1);
//////////////////////////////////////////////////////////////
Bitmap img = new Bitmap(width, height, newGraphics);
Graphics thisGraphics = Graphics.FromImage(img);
//////////////////////////////////////////////////////////////
IntPtr dc2 = thisGraphics.GetHdc();
IntPtr dc3 = newGraphics.GetHdc();
API.BitBlt(dc2, 0, 0, width, height, dc3, left, top, 13369376);
////////////////////////////////////////////////////////////////
thisGraphics.ReleaseHdc(dc2);
newGraphics.ReleaseHdc(dc3);
return img;
}
//保存成各种格式
public string savepic(Bitmap bmp)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "jpg文件(*.jpg)|*.jpg|位图文件(*.bmp)|*.bmp|gif文件(*.gif)|*.gif|tiff文件(*.tiff)|*.tiff|png文件(*.png)|*.png";
saveDialog.DefaultExt = "*.jpg";
string nowdate = System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString() + "-" + System.DateTime.Now.Hour.ToString() + "-" + System.DateTime.Now.Minute.ToString() + "-" + System.DateTime.Now.Second;
saveDialog.FileName = nowdate;
if (saveDialog.ShowDialog() == DialogResult.OK)
{
string ext = saveDialog.FileName.Substring(saveDialog.FileName.Length - 4, 4);
switch (ext)
{
case ".bmp":
bmp.Save(saveDialog.FileName, ImageFormat.Bmp);
break;
case ".gif":
bmp.Save(saveDialog.FileName , ImageFormat.Gif);
break;
case ".jpg":
bmp.Save(saveDialog.FileName, ImageFormat.Jpeg);
break;
case ".png":
bmp.Save(saveDialog.FileName , ImageFormat.Png);
break;
case ".tiff":
bmp.Save(saveDialog.FileName , ImageFormat.Tiff);
break;
default:
return "";
}
return saveDialog.FileName;
}
return "";
}
//图片输出
private void CreatPicButton_Click(object sender, EventArgs e)
{
IntPtr hwnd = this.Handle;
Bitmap image = getscreenfromhandle(hwnd);
savepic(image);
}
private void aboutButton_Click(object sender, EventArgs e)
{
Psp_wallpaper_mini.About_Box aboutbox = new About_Box();
aboutbox.Show();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -