界面布局如下:
窗体中添加一个PictureBox控件
有边框窗体
无边框窗体
代码实现:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
#region 创建无边框,任意样式窗体
private void Form2_Load(object sender, EventArgs e)
{
this.TransparencyKey = Color.White; //设置默认透明色
this.BackColor = this.TransparencyKey; //设置当前窗体的背景色为透明
this.FormBorderStyle = FormBorderStyle.None; //隐藏窗体边框
}
#endregion
#region 控制无边框窗体的移动
//using System.Runtime.InteropServices;
[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 void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//常量
int WM_SYSCOMMAND = 0x0112;
//窗体移动
int SC_MOVE = 0xF010;
int HTCAPTION = 0x0002;
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
常量
//int WM_SYSCOMMAND = 0x0112;
改变窗体大小
//int WMSZ_LEFT = 0xF001;
//int WMSZ_RIGHT = 0xF002;
//int WMSZ_TOP = 0xF003;
//int WMSZ_TOPLEFT = 0xF004;
//int WMSZ_TOPRIGHT = 0xF005;
//int WMSZ_BOTTOM = 0xF006;
//int WMSZ_BOTTOMLEFT = 0xF007;
//int WMSZ_BOTTOMRIGHT = 0xF008;
//ReleaseCapture();
//SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_BOTTOM, 0);
//SendMessage(this.Handle, WM_SYSCOMMAND, WMSZ_TOP, 0);
#endregion
}
png图像资源