开发者

One pixel out - OnPaint - System::Drawing::Rectangle rectangle = this->ClientRectangle;

开发者 https://www.devze.com 2023-02-16 04:16 出处:网络
I keep being 1 pixel out when drawing in the OnPaint method.I don\'t understand why. However, I\'m not sure it\'s me that can\'t count!

I keep being 1 pixel out when drawing in the OnPaint method. I don't understand why.

However, I'm not sure it's me that can't count!

I've gone back to the drawing board with 1 label in 1 panel as I'm sure I'm counting those correctly, I have tracing galore, and I'm stepping through the statements one at a time, checking absolutely everything.

I did find a couple of errors unrelated to this, so it obviously was worth doing just for that.

I've found out that the following code does write a single pixel border to the ClientRectangle :-

    System::Drawing::Rectangle rectangle = this->ClientRectangle;
    rectangle.Inflate(-1,-1);
    e->Graphics->DrawRectangle(blackPen, rectangle);

So far so good, but why -1 at all?

Anyway, my confusion deepens when I step through the code.

I fill the entire contents of th开发者_开发问答e rectangle apart from this 1 pixel border with the following (2 was found by trial and error and colour coding!) :-

    System::Drawing::Pen^ violetPen = gcnew System::Drawing::Pen( Brushes::Violet );
    for(int y=2; y< e->ClipRectangle.Bottom-1; y++)
    {
        e->Graphics->DrawLine( violetPen, e->ClipRectangle.Left+2, y,
            this->ClientRectangle.Right-2, y);
    }

So, the ClientRectangle is the writable area, but the indexing to it starts at (1,1).

Now this is a Panel with no border, and according to MSDN a ClientRectangle is as follows:-

The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus. Because client coordinates are relative to the upper-left corner of the client area of the control, the coordinates of the upper-left corner of the rectangle returned by this property are (0,0). You can use this property to obtain the size and coordinates of the client area of the control for tasks such as drawing on the surface of the control.

Now my control has no scroll bars, borders, title bars, and menus, so I think the indexing should start at (0,0) which is where I have been counting from.

and a Panel

The Panel control is displayed by default without any borders.

Can someone please help me understand what I'm missing here?


Hmm, are you sure the problem point is actually the top-left corner, rather than the bottom-right?

The RECT structure used internally by Windows controls is exclusive, meaning that the pixel with the coordinates (right, bottom) lies immediately outside of the rectangle.

Deflating the rectangle corresponding to the client area by 1 pixel, as you've shown, solves this problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消