If the screen has not been resized, but the form has been, then try to use the this.ClientSize.Width and this.ClientSize.Height.
Why in winforms, after form resize does 'screen.getworkingarea( this) .height' return the same value
Attempting to draw a grid on a winforms form. Draws ok. If I resize the form at all the screen.getworkingarea returns the original values and redraws the grid to the same size with the resized section blank. Having stepped through the code I can see that the screen working area for (this) the form remains the same though the form is now larger.
As can be seen from the code I have tried to isolate the problem but can see no reason for it, other than (this) in the screen.getworkingarea. Is there a way to tell the form that it has a new working area? I have yet to find it.
private void DrawGrid(Boolean Resize)
{
int NumOfYLines;
int NumOfXLines;
int GridHeight;
int GridWidth;
Pen ThisPen = new Pen(Color.LightSlateGray, 1);
if (!Resize)
{
GridHeight = this.Height;
GridWidth = this.Width;
}
else
{
GridHeight = Screen.GetWorkingArea(this).Height;
GridWidth = Screen.GetWorkingArea(this).Width;
ThisPen = new Pen(Color.DarkRed, 2);
//the pen color change was to check for the redraw being over the same lines
}
NumOfYLines = GridHeight / GridSize;
NumOfXLines = GridWidth / GridSize;
int x1; int y1; int x2; int y2;
for (int y = 0; y < NumOfYLines; ++y)
{
x1 = 0; y1 = y * GridSize; x2 = NumOfYLines * GridSize; y2 = y * GridSize;
Misc.writeLog("added in DrawGrid Y lines:" + x1.ToString() + " " + y1.ToString() + " " + x2.ToString() + " " + y2.ToString() + " " + Resize.ToString(), true);
TheseGraphics.DrawLine(ThisPen, 0, y * GridSize, GridWidth, y * GridSize);
}
for (int x = 0; x < NumOfXLines; ++x)
{
x1 = x * GridSize; y1 = 0; x2 = x * GridSize; y2 = NumOfXLines * GridSize;
Misc.writeLog("added in DrawGrid X lines:" + x1.ToString() + " " + y1.ToString() + " " + x2.ToString() + " " + y2.ToString() + " " + Resize.ToString(), true);
TheseGraphics.DrawLine(ThisPen, x * GridSize, 0, x * GridSize, NumOfXLines * GridSize);
}
}
2 answers
Sort by: Most helpful
-
-
Jiale Xue - MSFT 48,966 Reputation points Microsoft Vendor
2025-02-21T08:45:00.01+00:00 Hi @Peter Reilly , Welcome to Microsoft Q&A,
This.ClientSize returns the size of the form's client area, not including borders or title bars, which is ideal for dynamically adjusting the grid drawing range. When resizing, you can tell the form to redraw the entire client area by calling Invalidate. By using the Graphics object (e.Graphics) provided by PaintEventArgs, you can ensure that the drawing area is correct and avoid potential problems.
using System; using System.Drawing; using System.Windows.Forms; namespace xxx { public partial class Form1 : Form { private int GridSize = 20; // Set the size of the grid cell public Form1() { InitializeComponent(); // Initialize the form this.Text = "Grid drawing example"; this.BackColor = Color.White; this.Size = new Size(800, 600); // Register Paint and Resize events this.Paint += MainForm_Paint; this.Resize += MainForm_Resize; } // Triggered when redrawing private void MainForm_Paint(object sender, PaintEventArgs e) { DrawGrid(e.Graphics, false); } // Triggered when resizing the window private void MainForm_Resize(object sender, EventArgs e) { this.Invalidate(); // Notify the form that it needs to be redrawn } // Method for drawing the grid private void DrawGrid(Graphics g, bool resize) { int numOfYLines; int numOfXLines; int gridHeight; int gridWidth; Pen thisPen = new Pen(Color.LightSlateGray, 1); if (resize) { gridHeight = this.ClientSize.Height; // Use the height of the client area of the form gridWidth = this.ClientSize.Width; // Use the width of the client area of the form thisPen = new Pen(Color.DarkRed, 2); // Mark the grid when resizing with red lines } else { gridHeight = this.ClientSize.Height; gridWidth = this.ClientSize.Width; } numOfYLines = gridHeight / GridSize; // Calculate the number of vertical lines numOfXLines = gridWidth / GridSize; // Calculate the number of horizontal lines // Draw horizontal lines for (int y = 0; y <= numOfYLines; ++y) { int yPos = y * GridSize; g.DrawLine(thisPen, 0, yPos, gridWidth, yPos); } // Draw vertical lines for (int x = 0; x <= numOfXLines; ++x) { int xPos = x * GridSize; g.DrawLine(thisPen, xPos, 0, xPos, gridHeight); } } } }
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.