[RESOLVED] Change from VB.NET to C#.NET plaese?
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:
//**************************************
//
// Name: Most professional way of moving
// a borderless form without APIs and witho
// ut mouse pointer coordinates
// Description:Want to move a form by cl
// ick and dragging on the client area beca
// use your form is bordless/captionless? T
// his IS the way to do it at low level pro
// gramming without the use of APIs... full
// y .NET-wise.
// By: Rodolfo Gonzalez Ruiz
//
// Assumes:Basically the code uses the W
// ndProc and changes the window message to
// make it believe that you are clicking on
// the form/window title bar.
//
// Side Effects:User must click on the f
// orm client area, if a control is placed
// on top, clicking on it will not drag the
// window.
//
//This code is copyrighted and has // limited warranties.Please see http://
// [url]www.Planet-Source-Code.com/vb/scripts/Sh[/url]
// owCode.asp?txtCodeId=3965&lngWId=10 //for details. //**************************************
//
'Place this code anywhere on your form code
#Region " ClientAreaMove Handling "
Const WM_NCHITTEST As Integer = &H84
Const HTCLIENT As Integer = &H1
Const HTCAPTION As Integer = &H2
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_NCHITTEST
MyBase.WndProc(m)
If m.Result = HTCLIENT Then m.Result = HTCAPTION
'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.
Case Else
'Make sure you pass unhandled messages back to the default message handler.
MyBase.WndProc(m)
End Select
End Sub
#End Region
Re: Change from VB.NET to C#.NET plaese?
Code:
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.ToInt32() == HTCLIENT) m.Result = new IntPtr(HTCAPTION);
break;
//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.
default:
//Make sure you pass unhandled messages back to the default message handler.
base.WndProc(ref m);
break;
}
}
Re: Change from VB.NET to C#.NET plaese?
And for future needs you can use this site to convert from vb > C# and C# > vb.
http://www.developerfusion.co.uk/uti...btocsharp.aspx
Re: [RESOLVED] Change from VB.NET to C#.NET plaese?
That converter doesn't do a very good job on the original poster's sample however.
Re: [RESOLVED] Change from VB.NET to C#.NET plaese?
Heres the result.
VB Code:
const int WM_NCHITTEST = 132;
const int HTCLIENT = 1;
const int HTCAPTION = 2;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_NCHITTEST) {
base.WndProc(m);
if (m.Result == HTCLIENT) {
m.Result = HTCAPTION;
}
} else {
base.WndProc(m);
}
}
Re: [RESOLVED] Change from VB.NET to C#.NET plaese?
2 adjustments per 10 lines converted is not great (the missing 'ref' on two method calls). Converting a 20,000 line project that way is going to be not much faster than rewriting from scratch. Throw in some generics and you'll really see interesting results from on-line converters.
(ours has 0 adjustments for that sample).
Re: [RESOLVED] Change from VB.NET to C#.NET plaese?
I guess the difference is an understanding of the class library and not just the language. There is nothing in the VB code to suggest that the "ref" is required unless you have some knowledge of the method that's being called. On-line converters are unlikely to have that knowledge. I'd say that $179 is a good price if you did want to convert a major project, but for the most part we are converting snippets.
Re: [RESOLVED] Change from VB.NET to C#.NET plaese?
But the demo is free... (if you're just converting small snippets anyway).
Knowledge of the class library is critical since you're not really converting C# code to VB if you don't know where the 'ref's go. There are many other cases converting in either direction where an aspect is invisible in one language, but visible in another. For example, there is no syntax clue in C# about whether x is a class or an interface in "class foo : x", but it's critical to convert this to "... Inherits x" if it's a class and "... Implements x" if it's an interface when converting C# to VB.
So if a converter leaves off the 'ref', what the converter is really doing is assuming (not even guessing) that all the VB method parameters are 'ByVal'. The on-line converters do a lot of this - there is not even an attempt at heuristics (which we incorporate in converting partial code fragments).
And it's not even just the class library that the on-line converters overlook. If you type in methods with 'ByRef' parameters that are called elsewhere in your sample, you'll see that the 'ref' is still not added. So even the information in the code sample is being overlooked.
Re: [RESOLVED] Change from VB.NET to C#.NET plaese?
Well for a beginner in the .net language the little conversion site is a good help but with learning .net you learn a basic of OOP and the .NET Framework so its all just a matter of learning another languages syntax. I dont see the need to purchase a conversion utility unless you dont want to learn how to program.