在 Windows 窗体上将按钮添加到窗口的一侧 [英] Adding Buttons To Side Of Window On Windows Form

查看:35
本文介绍了在 Windows 窗体上将按钮添加到窗口的一侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 C# (在外面) 上向我的窗口窗体的一侧添加按钮.当窗口移动时,按钮应该一起移动.

I would like to add buttons to side of my windows form on C# (in outside). The buttons should move together as soon as when the window was moved.

例如:

推荐答案

我看到两个选项:

  • 或者将按钮放在一个单独的表单中,并通过编写 MoveResize 事件使两个表单粘在一起.

  • Either put the buttons in a separate form and make both forms stick together by coding the Move and maybe Resize events.

或者,更简单的是,使表单 透明 并删除 BorderTitle 区域.我会选择这个选项.

Or, simpler, make the Form transparent and remove Border and Title area. I would go for this option.

给你:

首先您通过以下方式设置表单样式:

First you style the Form by:

  • 设置 this.ControlBox = false;
  • 设置 this.MaximizeBox = false;
  • 设置this.MinimizeBox = false;
  • 设置this.Text="";
  • 设置 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  • 设置this.BackColor = System.Drawing.Color.Fuchsia;
  • 设置 this.TransparencyKey = this.BackColor;

现在添加到Form

  • 一个Panel,它填充Form
  • 的右侧、主要部分
  • 你想要附加的Button
  • 在主 Panel 中添加一个 Label (label1),填充顶部并保持表单的标题文本
  • 一个 Tab 控件等.
  • a Panel that fills the right, main portion of the Form
  • the Button you want attached
  • a Label (label1) inside the main Panel, filling the top and holding the form's title text
  • a Tab control etc..

最后我们要添加代码使表单可移动:

Finally we want to add code to make the form moveable:

using System.Runtime.InteropServices;
..

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

private void label1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
}

您也可以查找代码以使窗口变大..

You can look up code to make the window sizeable, too..

您可以查找代码以使用区域为按钮提供非矩形形状.请注意,您需要在此处避免使用抗锯齿像素,否则紫红色会透过.

You can look up code to give the button a non-rectangular shape using a region. Note that you need to avoid anti-aliased pixels here or else the Fuchsia will shine through.

这篇关于在 Windows 窗体上将按钮添加到窗口的一侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆