C#WinForm中怎么设置窗体不可以拉大和拖动,不可以拖动窗体的位置,让他固定不动啊?...

设置成无边框窗体即可.formBorderStyle设置为false。如果要用到最大化,最小化,关闭功能的话,自己再添加几个按钮就得了!

 

 

 

设置窗体不可以拉大你可以设置FormBorderStyle =FixedSingle 或 FixedToolWindow,不过可以拖动窗体...要想窗体不可以拖动...只能设置FormBorderStyle = none了

 

 

 

++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++
 

 

去掉边框
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
实现最大最小关闭
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();          
}

private void btnMin_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}    

private void btnMax_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.FormBorderStyle = FormBorderStyle.None;
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.WindowState = FormWindowState.Maximized;
}
}
因为无边框后,winform无法拖动,实现拖动
-----首先在类内创建两个成员变量
private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下


///---------------添加三个关于鼠标的事件
private void form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int xOffset;
int yOffset;

if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}

private void form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X, mouseOffset.Y);
Location = mousePos;
}
}

private void form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
     if (result == DialogResult.Yes)
     {
         e.Cancel = false;
         Application.Exit();
     }
     else
     {
         e.Cancel = true;
         this.Hide();
         this.Visible = false;
     }
}

private void ConExit_Click(object sender, EventArgs e)
{
     result=MessageBox.Show("确认退出系统?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
     Application.Exit();
}

【hbxtlhx】:
你一定要在如下的方法里添加参数来标识是不是应该真的退出程序:
private void   Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{   
     if (!this.m_allowExit)
     {
         this.WindowState = FormWindowState.Minimized;
         e.Cancel = true;           
     }
}

然后在弹出的菜单的Click事件里设置这个this.m_allowExit=true;
这样窗口就会顺利的关闭了.

【txwd0033】:
Application.Exit(true);行不?
如果不行的话设置一个变量,在formClosing中根据变量的值来设置e。Cancel的值

 

【hbxtlhx】:
在程序开始的时候设置this.m_allowExit=false,只有在菜单的退出事件代码里设置这个this.m_allowExit=true;就可以了.具体看你的思路了.

【lxcnn】:
可以加个全局变量判断一下是由哪里触发Closing事件,如果是关闭按钮就最小化,是右键菜单就关闭

【liutaoyue】:
/// <summary>
/// 重写窗体关闭事件
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int) m.WParam == SC_CLOSE)
{
this.Hide();
return;
}
base.WndProc (ref m);
}

【bejon】:
点x和使用菜单关闭程序执行不同程序

你只要在菜单那多设一个变量加以区分就OK了

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值