|
-
Apr 12th, 2011, 09:27 AM
#1
[RESOLVED] Form metrics issue
I have a picturebox the contents of which I want to copy to another picturebox. If I do a direct BitBlt other controls sitting on top of the first picturebox are not copied so what I do is a screen capture and then BitBlt from the selected area (scales are all in pixels):
Code:
picDest.Cls
picDest.Width = picSrc.Width
picDest.Height = picSrc.Height
'Capture screen
Ret = GetDC(GetDesktopWindow)
'Select area of interest and copy it to picDest
BitBlt picDest.hdc, 0, 0, picSrc.Width, picSrc.Height, Ret, _
Me.Left + picSrc.Left + 2 * Me.BorderStyle, _
Me.Top + Me.Height - Me.ScaleHeight + picSrc.Top, SRCCOPY
So I've found (by trial and error) that I must add twice the borderstyle property in order to take into account the form's left border. Now, as for the vertical coordinate of picSrc's top I'm not so sure what to do. Apparently from the result I should substract some pixels but I don't know how many and why.
Both picSrc and picDst have Appearance = 0 (flat) and BorderStyle = 0 (none)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Apr 12th, 2011, 09:52 AM
#2
Re: Form metrics issue
Using the borderstyle like that is very unreliable, because it depends on various display settings. If anything you should use (Form.Width - Form.ScaleWidth)/2 , but that isn't entirely safe (and doesn't help with the vertical either).
As pictureboxes have hWnd properties, you can use API's to get their screen location, such as GetWindowRect and ClientToScreen
-
Apr 12th, 2011, 10:26 AM
#3
Re: Form metrics issue
I don't really understand what you are trying to do. Are you trying to duplicate everything in picSrc (including controls drawn on picSrc) and place it in picDest?
-
Apr 12th, 2011, 10:29 AM
#4
Re: Form metrics issue
 Originally Posted by si_the_geek
Using the borderstyle like that is very unreliable, because it depends on various display settings....
I don't think that's true. As long as the poster doesn't refer to the border width as a hard-coded value he should be OK.
-
Apr 12th, 2011, 10:39 AM
#5
Re: Form metrics issue
That's half of the problem - the .BorderStyle property is a hard coded value (it is an enum that has values between 0 and 5), and the pixel size of the borders for each of those styles changes based on theme settings etc (using default Windows settings, they will be smaller on Windows XP than on Vista/7).
-
Apr 12th, 2011, 10:49 AM
#6
Re: Form metrics issue
 Originally Posted by si_the_geek
That's half of the problem - the .BorderStyle property is a hard coded value (it is an enum that has values between 0 and 5), and the pixel size of the borders for each of those styles changes based on theme settings etc (using default Windows settings, they will be smaller on Windows XP than on Vista/7).
Yes for sure but won't everything else change proportionally as well?
-
Apr 12th, 2011, 10:50 AM
#7
Re: Form metrics issue
 Originally Posted by si_the_geek
...you can use API's to get their screen location, such as GetWindowRect...
Thanks for the advice, here's what I've done:
Code:
'Capture whole screen
Ret = GetDC(GetDesktopWindow)
'Get picturebox coordinates relative to the screen
rec = GetPictureBoxCoordinates(picSrc)
BitBlt picDest.hdc, 0, 0, rec.Right - rec.Left, rec.Bottom - rec.Top, Ret, rec.Left, rec.Top, SRCCOPY
'...
'In a module:
Public Function GetPictureBoxCoordinates(pic As PictureBox) As RECT
Dim Rec As RECT
GetWindowRect pic.hwnd, Rec
GetPictureBoxCoordinates = Rec
End Function
I'm almost done. Only, the left and top coordinates should be one pixel less, for the captured image misses the first row and column. Any reason for that?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Apr 12th, 2011, 11:01 AM
#8
Re: Form metrics issue
 Originally Posted by krtxmrtz
...the left and top coordinates should be one pixel less, for the captured image misses the first row and column...
Sorry folks, I had miscounted the pixels and had to use a desktop magnifier to realize the result was after all correct. My mistake.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Apr 12th, 2011, 11:11 AM
#9
Re: Form metrics issue
It can be an easy thing to miss, I'm glad you got it sorted. 
 Originally Posted by MartinLiss
Yes for sure but won't everything else change proportionally as well?
Generally no.
While some display based settings are proportional, others (such as most theme settings) are not proportional at all. The default theme on XP has borders that are about half the size (proportionally and in pixels) of the default theme for Vista/7 - and the themes can be changed on any of them to be significantly different (eg: you could have a theme that has 1 pixel borders, and another with 30 pixel borders).
The .ScaleWidth based method I suggested (but is incomplete, it needs ScaleX in there too) deals with the vast majority of the issues, but it does assume that the left and right borders are the same size - which I presume is not guaranteed.
In addition to that, even with the same display based settings the .BorderStyle property is very wrong - because "Fixed Toolwindow"(4) is generally much thinner than "Sizable" (2).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|