【问题标题】:Re-sizing border-less form with a custom border使用自定义边框重新调整无边框表单的大小
【发布时间】:2014-04-07 00:55:01
【问题描述】:

问题不在于我不知道如何使无边框表单重新调整大小,或者不知道如何绘制边框。问题是当您使用该自定义边框重新调整表单大小时会发生什么。

这里是截图,因为不知道怎么解释:

这是我创建边框的方式(目前):

private void Form1_Paint(object sender, PaintEventArgs e)
{
    int width = 1;
    Rectangle rec = this.ClientRectangle;
    ButtonBorderStyle bbs = ButtonBorderStyle.Solid;
    Color clr = Color.Gray;
    ControlPaint.DrawBorder(e.Graphics, rec, clr, width, bbs, clr, width, bbs, clr, width, bbs, clr, width, bbs);
}

至于调整无边框表单的大小;我为该项目创建了一个存储库。
Resize Custom Border - Bitbucket

我不知道为什么会发生这种情况,所以我不知道从哪里开始。我只需要在不这样做的情况下绘制边框。我也试过其他的画法,结果都是一样的。

希望这个和存储库对任何尝试这样做的人有用。

感谢您抽出宝贵时间阅读。

【问题讨论】:

  • 如果您需要大量自定义,您应该考虑 WPF。学习曲线很小,但值得。
  • 我宁愿不使用 WPF。

标签: c# winforms


【解决方案1】:

尝试使用Graphics.DrawRectangle 而不是DrawBorder

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Single fWidth = 5.0f;
    Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
    e.Graphics.DrawRectangle(new Pen(Color.Gray, fWidth), r);
}

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    this.Invalidate();
}

【讨论】:

  • 您在OnResize 事件中给this.Invalidate() 打过电话吗? @Raxdiam
  • 是的,我不敢相信我没有想到这一点。感谢您的帮助。
【解决方案2】:

使用图形库:

第 1 步:覆盖主窗体的 OnPaint 处理程序

第 2 步:定义一个覆盖当前表单的矩形

第 3 步:绘制定义的矩形

protected override void OnPaint(PaintEventArgs e)
{
  Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
  e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}

您也可以使用如下条件语句来实现:

this.form.Resize += // some handler1

//in hadler1
{
   this.form.Paint += // Your new paint handler2
}

//in handler2
{
   Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
   e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多