我正在尝试在表单上绘制一个可变的颜色/宽度边框(对于“整个房间”的状态报告),除非在非主要显示屏上最大化时才会正常...
当在第二个显示器上最大化时,我没有得到最左边的边缘,如图所示here(为调试添加了绿色矩形,并且总是显示为绘制正确)。
我尝试了很多绘制顺序的排列,+ / - 1像素在这里和那里,改变笔属性等..但似乎没有任何效果,并且它无处不在最大化并在主屏幕上最大化的事实使我认为它是一些更微妙的东西。
要重现的完整代码是(单个尺寸的单个按钮形式):
Public Class Form2
Private DrawHighlightingRectangle As Boolean = False
Private HighlightingRectangleColour As Color = Color.Red
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If DrawHighlightingRectangle Then
Dim myPen As New System.Drawing.Pen(HighlightingRectangleColour, 8)
myPen.Alignment = Drawing2D.PenAlignment.Inset
e.Graphics.Clear(Me.BackColor)
e.Graphics.DrawRectangle(myPen, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
myPen.Color = Color.Green
' e.Graphics.DrawLine(myPen, 0, 0, 0, 100)
e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100)
myPen.Dispose()
End If
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Me.Invalidate()
Me.Update()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DrawHighlightingRectangle = Not DrawHighlightingRectangle
Me.Invalidate()
Me.Update()
End Sub
End Class
调试x,y和Me.ClientRectangle(我应该使用ClientRectangle,对吗?)尺寸确认它们是正确的。
有什么想法吗?
编辑:
将Me.ClientRectangle.Width更改为var以进行更深入的调试,新的完整代码为;
Public Class Form2
Private DrawHighlightingRectangle As Boolean = False
Private HighlightingRectangleColour As Color = Color.Red
Private MyWidth = 1900
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If DrawHighlightingRectangle Then
Dim myPen As New System.Drawing.Pen(HighlightingRectangleColour, 8)
e.Graphics.Clear(Me.BackColor)
e.Graphics.DrawRectangle(myPen, 0, 0, MyWidth, Me.ClientRectangle.Height)
myPen.Color = Color.Green
e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100)
myPen.Dispose()
End If
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Me.Invalidate()
Me.Update()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DrawHighlightingRectangle = Not DrawHighlightingRectangle
Me.Invalidate()
Me.Update()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MyWidth += 1
Me.Invalidate()
Me.Update()
End Sub
End Class
(请记住从1900更改MyWidth以匹配您的设置!)
使用这种方法我可以增加宽度并观察矩形增长,但是当接近正确的全宽度时,左边缘消失 - 改变笔宽度会影响何时它将消失(我删除了Pen.Alignment = Inset在案件中这是一个问题)但无论哪种方式我都无法得到我想要的输出...
答案 0 :(得分:0)
看起来像个错误。要解决此问题,当您在非主屏幕上进行完全筛选时,您应该拥有height
。在所有其他情况下,矩形需要TopLeft=Pen.Width-1
。确保相应地调整矩形的宽度
另外,我注意到你不能用宽于1像素的笔画到左边距。使用以下示例代码
TopLeft=CInt(Math.Floor(Pen.Width/2))