How to draw/ realize lines with the right thickness (1.5f)

youki 996 Reputation points
2021-12-12T04:33:05.947+00:00

Hi,
how can i archieve it?
I'm using my own Scrollbar Renderer and i've tried to draw the arrow like in the windows calculator

156839-image.png

by DrawLines but it's not working because there is no difference in 1 and 1.5f by GDI drawing as mentioned here:
https://stackoverflow.com/questions/45139526/how-to-change-the-thickness-of-the-line-drawn-using-system-drawing-pen

(I guess i need 1.5f because 1 is to thin and 2 is far too big)

I've also tried to draw and resize a bitmap but that looks blurry, sizing to the right size is not that easy and i assume that this won't solve it.

Should i draw by a graphic program? Is there an online tool to draw it easily by coordinates and pixel for the thickness? (I've tried it with inscape that doesn't seem to work. I can't enter one-digit values for all values, it's getting changed by the program.)

DrawLine:

  upArrowPoints = new Point[]{  
                            new Point(5, 10),  
                            new Point(8, 7),  
                             new Point(9, 7),  
                            new Point(12,10)};  
  
                using (var pen = new Pen(arrowColor,1.0f))  
                {  
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;  
                    e.Graphics.DrawLines(pen, upArrowPoints);  
                }  

Draw to bitmap:

        bitmapUpArrow = new Bitmap(6, 4);  

        using (Graphics gr = Graphics.FromImage(bitmapUpArrow))  
        {  
            gr.SmoothingMode = SmoothingMode.AntiAlias;  

            using (Pen thick_pen = new Pen(Color.Red, 1))  
            {  
                gr.DrawLine(thick_pen, 0, 3, 2, 0);  
                gr.DrawLine(thick_pen, 3, 0, 5, 3);  
            }  
        }  

Tried several resizing solutions:
https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,827 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,636 Reputation points
    2021-12-12T09:27:39.667+00:00

    Scrollbar arrows are drawn by Theme APIs

    A quick test on the main window DC :

    156903-sbp-arrowbtn.jpg

    int nX = 10;  
    int nY = 200;  
    RECT rectTest = new RECT(nX, nY, nX + 30, nY + 30);                 
    IntPtr hTheme = OpenThemeData(this.Handle, "ScrollBar");  
    if (hTheme != IntPtr.Zero)  
    {  
        IntPtr hDC = GetDC(this.Handle);  
        DrawThemeBackground(hTheme, hDC, (int)SCROLLBARPARTS.SBP_ARROWBTN, (int)ARROWBTNSTATES.ABS_UPNORMAL, ref rectTest, IntPtr.Zero);  
        rectTest.left += 40;  
        rectTest.right += 40;  
        DrawThemeBackground(hTheme, hDC, (int)SCROLLBARPARTS.SBP_ARROWBTN, (int)ARROWBTNSTATES.ABS_UPHOT, ref rectTest, IntPtr.Zero);  
        rectTest.left += 40;  
        rectTest.right += 40;  
        DrawThemeBackground(hTheme, hDC, (int)SCROLLBARPARTS.SBP_ARROWBTN, (int)ARROWBTNSTATES.ABS_UPPRESSED, ref rectTest, IntPtr.Zero);  
        CloseThemeData(hTheme);  
        ReleaseDC(this.Handle, hDC);  
    }  
    

    with declarations :

        [DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        public static extern IntPtr OpenThemeData(IntPtr hwnd, string pszClassList);  
    
        [DllImport("UxTheme.dll", SetLastError = true)]  
        public static extern IntPtr GetWindowTheme(IntPtr hwnd);  
    
        [DllImport("UxTheme.dll", SetLastError = true)]  
        public static extern HRESULT CloseThemeData(IntPtr hTheme);  
    
        [DllImport("UxTheme.dll", SetLastError = true)]  
        public extern static HRESULT DrawThemeBackground(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, ref RECT pRect, IntPtr pClipRect);  
    
        public enum SCROLLBARPARTS  
        {  
            SBP_ARROWBTN = 1,  
            SBP_THUMBBTNHORZ = 2,  
            SBP_THUMBBTNVERT = 3,  
            SBP_LOWERTRACKHORZ = 4,  
            SBP_UPPERTRACKHORZ = 5,  
            SBP_LOWERTRACKVERT = 6,  
            SBP_UPPERTRACKVERT = 7,  
            SBP_GRIPPERHORZ = 8,  
            SBP_GRIPPERVERT = 9,  
            SBP_SIZEBOX = 10,  
            SBP_SIZEBOXBKGND = 11,  
        };  
    
        public enum ARROWBTNSTATES  
        {  
            ABS_UPNORMAL = 1,  
            ABS_UPHOT = 2,  
            ABS_UPPRESSED = 3,  
            ABS_UPDISABLED = 4,  
            ABS_DOWNNORMAL = 5,  
            ABS_DOWNHOT = 6,  
            ABS_DOWNPRESSED = 7,  
            ABS_DOWNDISABLED = 8,  
            ABS_LEFTNORMAL = 9,  
            ABS_LEFTHOT = 10,  
            ABS_LEFTPRESSED = 11,  
            ABS_LEFTDISABLED = 12,  
            ABS_RIGHTNORMAL = 13,  
            ABS_RIGHTHOT = 14,  
            ABS_RIGHTPRESSED = 15,  
            ABS_RIGHTDISABLED = 16,  
            ABS_UPHOVER = 17,  
            ABS_DOWNHOVER = 18,  
            ABS_LEFTHOVER = 19,  
            ABS_RIGHTHOVER = 20,  
        };  
          
        [StructLayout(LayoutKind.Sequential)]  
        public struct RECT  
        {  
            public int left;  
            public int top;  
            public int right;  
            public int bottom;  
            public RECT(int Left, int Top, int Right, int Bottom)  
            {  
                left = Left;  
                top = Top;  
                right = Right;  
                bottom = Bottom;  
            }  
        }  
          
       [DllImport("User32", ExactSpelling = true, CharSet = CharSet.Auto)]  
       public static extern IntPtr GetDC(IntPtr hWnd);  
    
       [DllImport("User32", ExactSpelling = true, CharSet = CharSet.Auto)]  
       public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. youki 996 Reputation points
    2021-12-12T23:24:14.163+00:00

    Hi Castorix,
    after a second look, i realized it's really that (system) arrow but much thinner.
    Is it possible to make it thinner that it looks like in a WPF/ Win10 default scrollbar?

    (I would like to use it also in a context menu later, if i can, for a uniform look )

    If it's possible to look like a WPF scrollbar, have you used your code with a VScrollBar?


  2. youki 996 Reputation points
    2021-12-14T18:33:09.923+00:00

    Could anybody explain, what he means?

    What can i do with this code?
    Doesn't do anything with a VScrollbar?
    Doesn't have any effect here!

    0 comments No comments