开发者

How To Compensate For Margin From Control.PointToScreen?

开发者 https://www.devze.com 2023-02-11 18:01 出处:网络
I\'m using .PointToScreen(Point.Empty); to determine the location of controls relative to my entire screen.The only problem that I\'m facing is that the coordinates are always off by a little if they\

I'm using .PointToScreen(Point.Empty); to determine the location of controls relative to my entire screen. The only problem that I'm facing is that the coordinates are always off by a little if they're inside of a form. I开发者_如何转开发t seems to me that what's happening is that the form's margin is not accounted for and causes this error.

http://min.us/mvnZhCJ

I'm using this to take screenshots of entire forms or controls inside of the form. When I do a full form screenshot, what happens is that the margins are again not accounted for. The coordinates given by .PointToScreen(Point.Empty); is the top left corner of the form, but it's inside of the form so when I take a screenshot from this point, it goes past the right and bottom border.

Is there an automatic built-in way to fix this or do I have to manually compensate for this margin error?

static public void PrintForm(Control form) {
    Bitmap image = new Bitmap(form.Width, form.Height);
    Graphics g = Graphics.FromImage(image);
    g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
    g.CopyFromScreen(form.PointToScreen(Point.Empty).X, form.PointToScreen(Point.Empty).Y, 0, 0, new Size(form.Width, form.Height), CopyPixelOperation.SourceCopy);
    PrintDocument document = new PrintDocument();
    document.PrintPage += (sender, e) => Document_PrintImage(e, image);
    document.Print(); 
}


Use the Forms Bounds property. This provides the rectangle which it occupies on the screen.

For an individual control you can use the Parent.RectangleToScreen( Bounds ) to get the screen rectangle.

This will include all stuff within the bounds of the control.


You should be able to use the form's ClientRectangle and ClientSize properties for this purpose.

0

精彩评论

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