当前位置:编程学习 > C#/ASP.NET >>

C#移动无边框的窗体怎么写。

我想做一个类似QQ聊天窗口的东西,我把原来的标题栏弄掉了。窗口的最大化,最小化,还有窗口拖动都没有了。

我想自己写一个拖动窗口怎么写?

答案:下面是一个公用类,只用把你想作为无标题栏的窗体作为参数传递进来就可以了。你可以测试下。使用有问题,请追问

/*
* (C) wyj_slsoft
* Author: SLsoft
* Description:拥有统一属性的登录窗体(或需要实现以后功能的窗体),包括无标题栏移动窗体,以及进入、退出特效,验证错误窗体抖动、窗体透明等
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;

namespace SLsoft
{
/// <summary>
/// 登陆窗体
/// </summary>
public class LoginForm
{
#region 全局变量
private int mouseX = 0;
private int mouseY = 0;
private Form login;
public Timer timer_shake;
int times = 0;
const int AW_HOR_POSITIVE = 0x0001;
const int AW_HOR_NEGATIVE = 0x0002;
const int AW_VER_POSITIVE = 0X0004;
const int AW_VER_NEGATIVE = 0x0008;
const int AW_CENTER = 0x0010;
const int AW_HIDE = 0x10000;
const int AW_ACTIVATE = 0x20000;
const int AW_SLIDE = 0x40000;
const int AW_BLEND = 0x80000;
#endregion

[DllImport("user32")]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
private static extern int GetWindowLong(HandleRef hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
private static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

/// <summary>
/// 登陆窗体构造函数
/// </summary>
/// <param name="form">需要进行处理的窗体</param>
public LoginForm(Form form)
{
login = form;
login.Load+=new EventHandler(login_Load);
login.FormClosing += new FormClosingEventHandler(Login_FormClosing);
login.MouseDown += new MouseEventHandler(Login_MouseDown);
login.MouseMove += new MouseEventHandler(Login_MouseMove);
login.BackColor = Color.Blue;
login.TransparencyKey = Color.Blue;
login.FormBorderStyle = FormBorderStyle.None;

timer_shake = new Timer();
timer_shake.Interval = 100;
timer_shake.Tick+=new EventHandler(timer_shake_Tick);
}

private void Login_MouseDown(object sender, MouseEventArgs e)
{
mouseX = e.X;
mouseY = e.Y;
}

private void Login_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
login.Location = new Point(Control.MousePosition.X - mouseX, Control.MousePosition.Y - mouseY);
}
}

private void login_Load(object sender, EventArgs e)
{
AnimateWindow(login.Handle, 550, AW_CENTER | AW_ACTIVATE);
int WS_SYSMENU = 0x00080000; int windowLong = (GetWindowLong(new HandleRef(login, login.Handle), -16));
SetWindowLong(new HandleRef(login, login.Handle), -16, windowLong | WS_SYSMENU);
SetWindowLong(new HandleRef(login, login.Handle), -16, WS_SYSMENU | WS_SYSMENU);//屏蔽最大化
// SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU); //屏蔽最小化
}

private void Login_FormClosing(object sender, FormClosingEventArgs e)
{
AnimateWindow(login.Handle, 1000, AW_SLIDE | AW_HIDE | AW_VER_NEGATIVE);
Application.Exit();
}

private void timer_shake_Tick(object sender, EventArgs e)
{
times++;
if (times < 8)
{
for (int i = 0; i < 10; i++)
{
login.Location = new Point(login.Location.X + 1, login.Location.Y);
}
for (int i = 0; i < 10; i++)
{
login.Location = new Point(login.Location.X - 1, login.Location.Y);
}
}
else
{
times = 0;
timer_shake.Stop();
}

}
}
}

以鼠标左键单击窗体移动为例:

private bool isMouseDown = false;//标记
private Point FormLocation; //form的location
private Point mouseOffset; //鼠标的按下位置

private int oldHeight = 0;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = true;
FormLocation = this.Location;
mouseOffset = Control.MousePosition;
}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
int _x = 0;
int _y = 0;
if (isMouseDown)
{
Point pt = Control.MousePosition;
_x = mouseOffset.X - pt.X;
_y = mouseOffset.Y - pt.Y;

this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
}
}


protected override void WndProc(ref Message m)
{
const int WM_NCLBUTTONDBLCLK = 0x00A3;
switch (m.Msg)
{
case WM_NCLBUTTONDBLCLK:
if (oldHeight <= 0)
{
oldHeight = ClientSize.Height;
ClientSize = new Size(ClientSize.Width, 0);
}
else
{
ClientSize = new Size(ClientSize.Width, oldHeight);
oldHeight = 0;
}
return;
}
base.WndProc(ref m);
}

bool IsLeftPressd = false;
int MouseLeft, MouseRight, oldX, oldY;//分别保存鼠标当前点击的x,y坐标,和当前窗体的x,y
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
IsLeftPressd = true;//当鼠标左键按下设置为真,松开为假
oldX =this.Location .X ;//这些都是保存当前窗易做图置的X
oldY = this.Location.Y;//这些都是保存当前窗易做图置的X
MouseLeft = MousePosition;//这些都是保存当前鼠标位置的X
MouseRight = MousePosition .Y ;//这些都是保存当前鼠标位置的Y

}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
IsLeftPressd = false;//松开鼠标后设置为假
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (IsLeftPressd)//判断是否按下了鼠标左键,且没有松开
{
this.Location = new Point(oldX + MousePosition.X - MouseLeft, oldY + MousePosition.Y - MouseRight);//将以前的窗体X,Y分别加上鼠标的偏移量!
}
}

如果还有疑问可以问我!

祝你成功!

上一个:C#中 sql查询语句该怎么写啊?
下一个:C#实现下拉菜单取值再截取符号

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,