如题呐.新人刚刚学习的C#表示....然后想做一个界面.去掉标题栏窗口也可以自由的移动.
然后就去网上搜索相差的代码.
但是....发现非常无语的是..我找到了代码.却不知道怎么用呢....
表示付上我找到的一段代码..
还前辈们指点下..应该如何使用呢? // 洪星 2006年11月14日
// www.hongcing.com
// QQ 219402
// 获取窗体的屏幕坐标和鼠标光标的位置(屏幕坐标)
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
mousePoint = Control.MousePosition;
formPoint = this.Location;
}
// 鼠标光标的屏幕坐标
Point mousePoint;
// 窗体的屏幕坐标
Point formPoint;
// 响应鼠标移动,并移动窗口
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
}
}
然后就去网上搜索相差的代码.
但是....发现非常无语的是..我找到了代码.却不知道怎么用呢....
表示付上我找到的一段代码..
还前辈们指点下..应该如何使用呢? // 洪星 2006年11月14日
// www.hongcing.com
// QQ 219402
// 获取窗体的屏幕坐标和鼠标光标的位置(屏幕坐标)
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
mousePoint = Control.MousePosition;
formPoint = this.Location;
}
// 鼠标光标的屏幕坐标
Point mousePoint;
// 窗体的屏幕坐标
Point formPoint;
// 响应鼠标移动,并移动窗口
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
}
}
解决方案 »
- 求大神指导~~
- 会c# 的来帮我啊
- 项目的webconfig里没有sessionState标签,sessionState的默认属性都是什么? 还是没有那些属性? cookieless设置为true和false有什么区别
- C#中ArrayList如何一次传递多值到另一个类中
- VS2005中datalist绑定的问题????
- socket发送邮件问题
- webfrom下用c#做曲线图的问题,图已经画出来了,但是。。。。。。
- 多线程
- 能不能用数字组成控件的名称?
- substring截取字符串问题,大家看下!
- 我要崩溃了,这到底是什么错误,关于winform的窗体设计文件。救下我吧!被折磨的不行了!
- tree控件问题
// 获取窗体的屏幕坐标和鼠标光标的位置(屏幕坐标)
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
mousePoint = Control.MousePosition;
formPoint = this.Location;
}
这个方法是窗体鼠标按下的事件
// 响应鼠标移动,并移动窗口
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
}
}
这个是鼠标在窗体上移动的事件,
套学会看事件
MainForm_MouseMove(object sender, MouseEventArgs e)
其中MainForm是控件名,MouseMove是事件名sender响应事件的控件,e 键
以上是我本人个人的理解,有什么地方不对的请随意指出
const int HTLEFT = 10;
const int HTRIGHT = 11;
const int HTTOP = 12;
const int HTTOPLEFT = 13;
const int HTTOPRIGHT = 14;
const int HTBOTTOM = 15;
const int HTBOTTOMLEFT = 0x10;
const int HTBOTTOMRIGHT = 17;protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0084:
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)HTTOPLEFT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)HTBOTTOMLEFT;
else m.Result = (IntPtr)HTLEFT;
else if (vPoint.X >= ClientSize.Width - 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)HTTOPRIGHT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)HTBOTTOMRIGHT;
else m.Result = (IntPtr)HTRIGHT;
else if (vPoint.Y <= 5)
m.Result = (IntPtr)HTTOP;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)HTBOTTOM;
break;
case 0x0201://鼠标左键按下的消息
m.Msg = 0x00A1;//更改消息为非客户区按下鼠标
m.LParam = IntPtr.Zero;//默认值
m.WParam = new IntPtr(2);//鼠标放在标题栏内
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void WndProc(ref Message m){
const int WM_LBUTTONDOWN = 513;
const int WM_NCLBUTTONDOWN = 161;
const int HTCAPTION = 2;
switch (m.Msg){
case WM_LBUTTONDOWN:
PostMessage(this.handle, WM_NCLBUTTONDOWN, HTCAPTION, m.lParam);
return ;
default:
base.WndProc(ref m);
break;
}
}
自己修改一下,没有编译过
using System.Runtime.InteropServices;
然后在窗体类的空白处贴上这个
const int WM_LBUTTONDOWN = 513;
const int WM_NCLBUTTONDOWN = 161;
const int HTCAPTION = 2; [DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam); protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_LBUTTONDOWN:
PostMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, m.LParam);
return;
default:
base.WndProc(ref m);
break;
}
}
最终的代码类似这个:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} const int WM_LBUTTONDOWN = 513;
const int WM_NCLBUTTONDOWN = 161;
const int HTCAPTION = 2; [DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam); protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_LBUTTONDOWN:
PostMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, m.LParam);
return;
default:
base.WndProc(ref m);
break;
}
} }
}