Search code examples
winapigdi

Win32 - Is there a way to get the margin or border size around a scrollbar?


How do you get the area between the contents displayed in the client area (say notepad) and the scrollbar. There is a slight gap / border in there. How is that determined?

TIA!!

Here's an example: Note part of the small "j" shows but there is a border between that and the scrollbar on the right. How do you know that gap because the client RECT includes the gap?

enter image description here


Solution

  • Notepad uses standard EDIT control for its text area. EDIT control implements two messages: EM_GETMARGINS and EM_GETRECT. Values returned by EM_GETMARGINS and EM_GETRECT depend on selected font.

    For Courier New at 96 DPI:

    height   client rect EM_GETMARGINS EM_GETRECT  right margin
             left right  left right    left right  by EM_GETRECT
    10pt     0    489    2    1        3    487    2
    20pt     0    489    5    3        6    485    4
    

    For Lucida Console at 96 DPI:

    height   client rect EM_GETMARGINS EM_GETRECT  right margin
             left right  left right    left right  by EM_GETRECT
    10pt     0    489    0    0        1    488    1
    20pt     0    489    0    1        1    487    2
    

    For Consolas at 96 DPI:

    height   client rect EM_GETMARGINS EM_GETRECT  right margin
             left right  left right    left right  by EM_GETRECT
    10pt     0    489    3    3        5    485    4
    20pt     0    489    7    6        8    482    7
    

    For some reason EM_GETMARGINS and EM_GETRECT return slightly different margins. Inspecting them visually it seems that EM_GETRECT produces more correct results.

    These are default values. Notepad can modify them but on Windows 10 values are as specified above. Beware that on Windows 10 one white pixel belongs to vertical scroll bar (it could be verified by hovering over it, witch result in scroll bar's thumb highlight and changing cursor from i-beam to arrow).

    It looks that you are using Consolas and get default margins.