gpt4 book ai didi

c# - Windows 窗体 : What's the right way to allow overlapping Controls?

转载 作者:太空狗 更新时间:2023-10-29 17:57:24 26 4
gpt4 key购买 nike

我正在制作一个测试程序,看看我可以将事件添加到自定义绘制的 System.Windows.Form.Control 对象中的效果如何。如果我在这方面做得很好,那么我可以为以后做一些更高级的东西。

我遇到的问题与所附图片有关。我有意地画了两个圆圈彼此靠近。目标是让一个圆圈与另一个圆圈重叠。为了这个测试程序的目的,我不关心哪个圆重叠哪个。但是,我确实关心角落。

Wish that the two circles just overlapped properly instead of having the jutting corners.

上图显示中心圆被左圆掩埋,但左圆也在绘制角并用它们覆盖中心圆。我希望隐藏那些角落,或者至少让它们透明。我确实读过有一种方法可以make a control transparent ,但出于某种原因,在 BackColor 上使用 Color.Transparent 会给我黑色,而不是与油漆面板的颜色相匹配。

下面是GUI的代码部分(不包括设计器,但关键部分应该很明显)。

namespace PaintingFirstAttempt
{
using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

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

private void BtnClear_Click(object sender, EventArgs e)
{
Graphics g1 = this.paintPanel.CreateGraphics();
g1.Clear(this.paintPanel.BackColor);
g1.Dispose();
}

private void PaintPanel_MouseDown(object sender, MouseEventArgs e)
{
this.paintPanel.Controls.Add(new EventableCircle { Location = new Point(e.X - 16, e.Y - 16), Size = new Size(32, 32) });
}
}
}

下面是自定义圈子。

namespace PaintingFirstAttempt
{
using System;
using System.Drawing;
using System.Windows.Forms;

public class EventableCircle : Control
{
public EventableCircle()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
// this.BackColor = Color.Transparent;
}

private static SolidBrush fillColor = new SolidBrush(Color.Red);
protected override void OnClick(EventArgs e)
{
MessageBox.Show("TODO: Bring up a combo box on right click.");
}

private void DrawCircle(Pen pen)
{
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
g.FillRectangle(new SolidBrush(Color.Transparent), 0, 0, 32, 32);
g.FillEllipse(fillColor, 0, 0, 32, 32);
g.DrawEllipse(pen, 0, 0, 32, 32);
g.Dispose();

}

protected override void OnPaint(PaintEventArgs e)
{
this.DrawCircle(Pens.Black);
}

protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
this.DrawCircle(Pens.Blue);
}

protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.DrawCircle(Pens.Black);
}
}
}

考虑到这些信息,我该如何让圆角不显示,或者找到解决这个问题的方法?

最佳答案

OK 只需放入一个主窗体 form1,不需要任何代码。这是用户控件的代码,在窗体上绘制 3 或 4,以便它们向右移动时可以重叠。

然后点击它们!效率低下,但宝宝哭了我得走了,这比自己制作保留对象编辑器要好!

enter image description here

Public Class linectl   

Public Sub DrawMe(ByVal g As Graphics, ByVal otherctl As Control)
Dim where As New Rectangle(otherctl.Left - Left, otherctl.Top - Top, otherctl.Width - 1, otherctl.Height - 1)
g.FillEllipse(Brushes.Red, where)
g.DrawEllipse(Pens.Black, where)
End Sub

Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(e)
DrawMe(e.Graphics, Me)
drawneighbors()
End Sub

Private Sub linectl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Me.Left += 10
End Sub

Private Sub linectl_MoveResize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move, Me.Resize
If Parent IsNot Nothing Then
For Each c In From ctl In Parent.Controls Where TypeOf ctl Is linectl Select CType(ctl, linectl)
Using g = c.CreateGraphics
g.Clear(c.BackColor)
c.DrawMe(g, c)
c.drawneighbors()
End Using
Next
End If
End Sub

Public Sub drawneighbors()
If Parent IsNot Nothing Then
Dim ctls = (From ctl In Parent.Controls Where TypeOf ctl Is linectl Let c = CType(ctl, linectl) _
Select New With {.ctl = c, _
.rect = New Rectangle(c.Left, c.Top, c.Width, c.Height)}).ToArray.Reverse
For Each ctl In ctls
Dim ctl_unclosed = ctl
For Each ictl In (From c In ctls Where ctl_unclosed.rect.IntersectsWith(c.rect))
Using g = ictl.ctl.CreateGraphics
ictl.ctl.DrawMe(g, Me)
End Using
Using g = Me.CreateGraphics
Me.DrawMe(g, ictl.ctl)
End Using
Next
Next
End If
End Sub

End Class

关于c# - Windows 窗体 : What's the right way to allow overlapping Controls?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14048714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com