C#做的无框窗体只能在电脑屏幕范围内移动

拖动一个C#做的无框窗体在电脑屏幕范围内移动,不管分辨率是多少,都不能移动到屏幕外面去,这样的代码要怎么写呢?代码越少越好,不过不要全部都是文字解释这个问题,不然的话我问... 拖动一个C#做的无框窗体在电脑屏幕范围内移动,不管分辨率是多少,都不能移动到屏幕外面去,这样的代码要怎么写呢?代码越少越好,不过不要全部都是文字解释这个问题,不然的话我问这个问题就没意义了,谢谢 展开
 我来答
chxaitz
2011-11-18 · 超过15用户采纳过TA的回答
知道答主
回答量:35
采纳率:0%
帮助的人:38.3万
展开全部
在Form窗口中添加一个Move事件
private void Form_Move(object sender,EventArgs e)
{
Rectangle r=Screen.GetWorkingArea(this);
if( (this.Right>=r.Right)||(this.Left<=0)||(this.Top<=0)||(this.Bottom>=r.Bottom))
this.Location = new Point(r.Right - this.Width, r.Bottom - this.Height);
}
就是检测,当上下左右超出位置时,自动恢复,代码是手敲的,不免有错误之处,希望楼主采纳~o(∩_∩)o 哈哈
百度网友e1b1222
2011-11-18 · TA获得超过956个赞
知道小有建树答主
回答量:470
采纳率:66%
帮助的人:343万
展开全部
经测试可用,环境:VS2005,当拖出屏幕外面的时候,会把窗口定位到当前屏幕中间。
private void Form1_Move(object sender, EventArgs e)
{
Point p = this.Location;
Rectangle r = Screen.GetWorkingArea(this);
if(p.X<0||p.Y<0||p.X>r.Width-this.Size.Width||p.Y>r.Height-this.Size.Height)
{
this.Location = new Point((r.Width - this.Size.Width) / 2, (r.Height - this.Size.Height) / 2);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
不想起名字了2333
2011-11-18 · TA获得超过2447个赞
知道大有可为答主
回答量:1931
采纳率:50%
帮助的人:1885万
展开全部
C# 窗体 可移动的五角形
Resources文件夹里面有一张 4859402.JPEG图片,作为五角星的背景图片~~~~~~~~

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 可移动的五角形
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point MousePos; // 记录鼠标指针的坐标
private bool bMouseDown = false;//记录鼠标是否按下

private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath myGraphicsPath = new GraphicsPath(); //实例化五角形的线
//定义五个 点数组
Point[] myPoint = {
new Point(130,100),
new Point(300,000),
new Point(470,100),
new Point(400,300),
new Point(200,300),
};
myGraphicsPath.AddPolygon(myPoint); //绘制五角形
this.Region = new Region(myGraphicsPath);//控件关联的窗口区域
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MousePos.X = -e.X - SystemInformation.FrameBorderSize.Width;
MousePos.Y = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
bMouseDown = true;
}
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (bMouseDown)
{
Point CurrentPos = Control.MousePosition;
CurrentPos.Offset(MousePos.X,MousePos.Y);
Location = CurrentPos;
}
}

private void Form1_DoubleClick(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
}
}

namespace 可移动的五角形
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = global::可移动的五角形.Properties.Resources._4859402;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(492, 469);
this.Name = "Form1";
this.Text = "可移动的五角形";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false);

}

#endregion
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式