none
Custom Button And Use PtInRect RRS feed

All replies

  • Hi,

    Do you want to check if the cursor is inside button via "PtInRect"? Here is a demo may help you.

        public class CustomButton : Button
        {
            [System.Runtime.InteropServices.DllImport("User32.dll")]
            public static extern bool PtInRect(ref Rectangle r, Point p);
            public CustomButton()
            {
                this.MouseMove += CustomButton_MouseMove;
            }
    
            private void CustomButton_MouseMove(object sender, MouseEventArgs e)
            {
                Point cursorPoint = this.PointToClient(MousePosition);
                Console.WriteLine(cursorPoint);
                Rectangle rectgle = new Rectangle(0, 0, this.Width, this.Height);
                Console.WriteLine(rectgle);
                bool ptInRect = PtInRect(ref rectgle, cursorPoint);
                if (ptInRect)
                {
                    Console.WriteLine("in");
                }
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 20, 2019 5:55 AM
  • Hi,Thanks to share it 

    it does work fine only When subclass the specified button , but if we have some controls in window and subclass this window  not specified Control , PtInRect Return NonZero in x and in allover the y also return nonzero not in specified y in Rectangle(0,0,5,5)

    it means when cursor in  x(UpperLeft[0] And UpperRight[5]) return nonzero and more than 5 return zero , but when cursor in y position (Top[0] And Bottom[5]) Return nonzero in more than 5 also return nonzero

    Example When Subclass The Window , if client Rect(hwnd no t button ) be 0,0,100,100  and the button rectangle would be 0,0,5,5  .. using ptinrect x (0 to 5) return nonzero but y (0 to 100 : top to bottom) return nonzero not in (0 To 5 : top to bottom) only





    • Edited by Grailly Ali Friday, September 20, 2019 9:21 AM
    Friday, September 20, 2019 9:02 AM
  • Hi,

    I noticed that you mentioned "subclass the window". It's a namespace in "PresentationFramework.dll". So what is the type of you project? Is it a WPF?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 23, 2019 1:38 AM
  • Hi 

    Yes , PtinRect x is for left and y for right ( in x no problem just from left to right return none zero ) but y in allover the specified window return none zero i think we have to define top and bottom also as per screen coordinate so that in mentioned top and left only return nonzero 

    I tested ptinrect rect,pt.x,pt.y 

    pt.x  pointed to left and pt.y pointed to right , if i subclass window , x coordinate is from left to right and y coordinate is  all y in the specified window and return non zero  ( this function has to retrun non zero only in x,y coordinate not in x coordinate only ) i think in subclass the window we have to define Top And Bottom Of rect .






    • Edited by Grailly Ali Monday, September 23, 2019 8:11 AM
    Monday, September 23, 2019 8:03 AM
  • Hi,

    This is a wpf project. The Rectangle and Point in the WPF default reference are not the same as Winform,add System.Drawing.Point, System.Drawing.Point/System.Drawing.Rectangle.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 25, 2019 2:38 AM
    Moderator