Results 1 to 3 of 3

Thread: [RESOLVED] Convert this from VB.NET to C#.NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Resolved [RESOLVED] Convert this from VB.NET to C#.NET

    Hi,
    Found a "professional way" to drag a borderless form but the sample is written in VB.NET.

    Can anyone change it to C# language please?
    VB Code:
    1. //**************************************
    2.     //    
    3.     // Name: Most professional way of moving
    4.     //     a borderless form without APIs and witho
    5.     //     ut mouse pointer coordinates
    6.     // Description:Want to move a form by cl
    7.     //     ick and dragging on the client area beca
    8.     //     use your form is bordless/captionless? T
    9.     //     his IS the way to do it at low level pro
    10.     //     gramming without the use of APIs... full
    11.     //     y .NET-wise.
    12.     // By: Rodolfo Gonzalez Ruiz
    13.     //
    14.     // Assumes:Basically the code uses the W
    15.     //     ndProc and changes the window message to
    16.     //     make it believe that you are clicking on
    17.     //     the form/window title bar.
    18.     //
    19.     // Side Effects:User must click on the f
    20.     //     orm client area, if a control is placed
    21.     //     on top, clicking on it will not drag the
    22.     //     window.
    23.     //
    24.     //This code is copyrighted and has    // limited warranties.Please see http://
    25.     //     [url]www.Planet-Source-Code.com/vb/scripts/Sh[/url]
    26.     //     owCode.asp?txtCodeId=3965&lngWId=10    //for details.    //**************************************
    27.     //    
    28.    
    29.     'Place this code anywhere on your form code
    30.     #Region " ClientAreaMove Handling "
    31.     Const WM_NCHITTEST As Integer = &H84
    32.     Const HTCLIENT As Integer = &H1
    33.     Const HTCAPTION As Integer = &H2
    34.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    35.     Select Case m.Msg
    36.     Case WM_NCHITTEST
    37.     MyBase.WndProc(m)
    38.     If m.Result = HTCLIENT Then m.Result = HTCAPTION
    39.     'If m.Result.ToInt32 = HTCLIENT Then m.Result = IntPtr.op_Explicit(HTCAPTION) 'Try this in VS.NET 2002/2003 if the latter line of code doesn't do it... thx to Suhas for the tip.
    40.     Case Else
    41.     'Make sure you pass unhandled messages back to the default message handler.
    42.     MyBase.WndProc(m)
    43.     End Select
    44.     End Sub
    45.     #End Region
    Thanks

    Tomexx.

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Convert this from VB.NET to C#.NET

    Code:
        //Place this code anywhere on your form code
        const int WM_NCHITTEST = 0x84;    
        const int HTCLIENT = 0x01;
        const int HTCAPTION 0x02;
    
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            switch(m.Msg)
            {
                case WM_NCHITTEST:
                    base.WndProc(m);
                    if (m.Result == HTCLIENT)
                        m.Result = HTCAPTION;
                    break;
                default:
                    base.WndProc(m);
            }
        }
    C'mon now, I bet you could have done that yourself

    Also, there are a lot of VB.NET <-> C# converters (that work with varying degrees of success) floating around for free on the internet. I bet one of them would have nailed this simple snippet right on, while I probably made some subtle, catastrophic mistake that will cause your monitor to explode. Or maybe just get a syntax error somewhere.

    HTH!
    Last edited by sunburnt; Jun 28th, 2006 at 01:20 PM.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: [RESOLVED] Convert this from VB.NET to C#.NET

    Almost ... (but I'm being picky)

    Code:
    #region  ClientAreaMove Handling 
    private const int WM_NCHITTEST = 0X84;
    private const int HTCLIENT = 0X1;
    private const int HTCAPTION = 0X2;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
    	switch (m.Msg)
    	{
    	case WM_NCHITTEST:
    		base.WndProc(ref m);
    		if (m.Result == HTCLIENT)
    			m.Result = HTCAPTION;
    		break;
    	default:
    		//Make sure you pass unhandled messages back to the default message handler.
    		base.WndProc(ref m);
    		break;
    	}
    }
    #endregion
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

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