Results 1 to 9 of 9

Thread: [RESOLVED] Form metrics issue

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [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)

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  3. #3

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Form metrics issue

    Quote Originally Posted by si_the_geek View Post
    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.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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).

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Form metrics issue

    Quote Originally Posted by si_the_geek View Post
    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?

  7. #7

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Form metrics issue

    Quote Originally Posted by si_the_geek View Post
    ...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)

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Form metrics issue

    Quote Originally Posted by krtxmrtz View Post
    ...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)

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Form metrics issue

    It can be an easy thing to miss, I'm glad you got it sorted.
    Quote Originally Posted by MartinLiss View Post
    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
  •  



Click Here to Expand Forum to Full Width