[英]C# winforms unable to paint in middle of screen, even with the correct screen height and width
我正在嘗試編寫一個程序,將十字准線放置在透明背景下的屏幕中間,但是,即使放置的坐標正確,十字准線也總是以偏移量繪制。
繪制十字准線的方法:
public void drawSquareCrosshair(Graphics gfx, Color color) {
SolidBrush lightChrosshairColor = new SolidBrush(color);
SolidBrush transparentColor = new SolidBrush(Color.Fuchsia);
// Crosshair Bounds
int width = 20;
int height = 20;
int x = (SystemInformation.VirtualScreen.Width / 2)- (width / 2);
int y = (SystemInformation.VirtualScreen.Height / 2) - (height / 2);
Console.WriteLine("X: " + x);
Console.WriteLine("Y: " + y);
gfx.FillRectangle(lightChrosshairColor, x, y, width, height);
}
邏輯是,屏幕的寬度除以2,然后減去十字准線的寬度除以2。 高度也一樣。 我已經設置了圖形對象,以在固定在表單內所有面上的面板上創建圖形,但是面板的高度和寬度仍為1920x1080,與表單一樣。 我將表單設置為最大化的方式是使用form.FormBorderStyle = FormBorderStyle.None;
和form.WindowState = FormWindowState.Maximized;
但是,情況似乎並非如此,因為十字准線仍放置在類似於屏幕中間的坐標上(在1920x1080屏幕上為X:960,Y:540)。 我在photoshop屏幕中間創建了一個十字形的桌面背景(圖像也是1920x1080)。 外觀如下: 正方形是我的應用程序繪制的十字准線,紅色十字是牆紙
有人遇到過這個問題嗎?
可以通過將SystemInformation.VirtualScreen
替換為ClientRectangle
來解決此問題,請參見以下代碼。 ClientRectangle返回窗口的可繪制區域的邊界。
int x = (ClientRectangle.Width / 2) - (width / 2);
int y = (ClientRectangle.Height / 2) - (height / 2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.