How to Hide the Resize cursor in WPF?

MERUN KUMAR MAITY 636 Reputation points
2021-11-23T13:22:30.13+00:00

I have a WPF application. Which is Re sizable. But I want to hide the Resize cursor on left edges of the Window and some specific position of the window. Someone suggest me to call the Win32 API but I don't know how to do that.

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | 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.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Castorix31 91,506 Reputation points
    2021-11-23T14:44:16.987+00:00

    Another similar way is to test the return of DefWindowProc in WM_NCHITTEST

                    int nRet = DefWindowProc(hWnd, WM_NCHITTEST, 0, lParam);
                    switch (nRet)
                    {
                        case HTLEFT:
                        case HTTOP:
                        case HTBOTTOM:
                        case HTRIGHT:
                        case HTBOTTOMLEFT:
                        case HTBOTTOMRIGHT:
                        case HTTOPLEFT:
                        case HTTOPRIGHT:
                            {
                                handled = true;
                                return new IntPtr(HTNOWHERE);
                            }
                    }
    

    with :

            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            public static extern int DefWindowProc(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
    

1 additional answer

Sort by: Most helpful
  1. Viorel 125.7K Reputation points
    2021-11-23T14:28:58.173+00:00

    Try adding these functions to your window:

    protected override void OnSourceInitialized( EventArgs e )
    {
        base.OnSourceInitialized( e );
        HwndSource s = (HwndSource)PresentationSource.FromVisual( this );
        s.AddHook( WndProc );
    }
    
    private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
    {
        const int WM_NCHITTEST = 0x0084;
        const int HTNOWHERE = 0;
    
        switch( msg )
        {
        case WM_NCHITTEST:
    
            int x = lParam.ToInt32( ) & 0xffff;
            int y = lParam.ToInt32( ) >> 16;
    
            var p = new Point( x, y );
            p = PointFromScreen( p );
    
            if( p.X < SystemParameters.ResizeFrameVerticalBorderWidth )
            {
                handled = true;
    
                return new IntPtr( HTNOWHERE );
            }
            break;
        }
    
        return IntPtr.Zero;
    }
    

    It disable the resizing at left. Add more tests for p.X and p.Y (the local coordinates of mouse pointer) according to your needs.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.