473,587 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hex question

I wrote a control in which I overrode WndProc to implement a custom
MouseMove. I caught the MouseMove message and passed it to a function which
raises a custom event. In this function I get the x and y from the LParam of
the message; x being the low order word, y being the high order word. I use
two functions to get the LoWord and HiWord from the LParam

Private Shared Function HiWord(ByVal Number As Integer) As Integer
Return ((Number >> 16) And &HFFFF)
End Function

Private Shared Function LoWord(ByVal Number As Integer) As Integer
Return (Number And &HFFFF)
End Function

This gets me x and y correctly except when x or y is negative.
Example:

LParam.ToInt32: 5636101 (&H00560005)
x: 5 (&H0005)
y: 86 (&H0056)

LParam.ToInt32: 5636100 (&H00560004)
x: 4 (&H0004)
y: 86 (&H0056)

LParam.ToInt32: 5636099 (&H00560003)
x: 3 (&H0003)
y: 86 (&H0056)

LParam.ToInt32: 5570562 (&H00550002)
x: 2 (&H0002)
y: 85 (&H0055)

LParam.ToInt32: 5570561 (&H00550001)
x: 1 (&H0001)
y: 85 (&H0055)

LParam.ToInt32: 5570560 (&H00550000)
x: 0 (&H0000)
y: 85 (&H0055)

LParam.ToInt32: 5636095(&H0055F FFF)
x: 65535 (&HFFFF) <-- This should be -1!!!!
y: 85 (&H0055)

If this were VB6, &HFFFF would produce -1 and &HFFFF& would produce 65535.
In VB.Net &HFFFF and &HFFFF& are the same, both producing 65535. Negative 1
is represented by &HFFFFFFFF. How can I make my calculation aware that
&HFFFF is like vb6, using a 2-byte integer instead of a 4-byte integer.

By the way, I used Lutz Roeder's Reflector to see how Controls handle this
by default. I understand that it is not a perfect decompiler already,
because it works backwards from MSIL. It would seem that Controls get x and
y like this:

x = CType(m.LParam. ToInt32, Short)
y = (m.LParam.ToInt 32) >> 16)

If I were to try this, I would get an OverflowExcepti on when LParam.ToInt32
equaled 5636095 (way more than a Short can hold)

How can I extract x and y properly?

--

Any help is appreciated.
Thanks in advance.

Hong Kong Phooey
Nov 21 '05 #1
5 4073

"Hong Kong Phooey" <NO****@NOSPAM. com> wrote in message
news:eZ******** ******@TK2MSFTN GP15.phx.gbl...
I wrote a control in which I overrode WndProc to implement a custom
MouseMove. I caught the MouseMove message and passed it to a function
which
raises a custom event. In this function I get the x and y from the LParam
of
the message; x being the low order word, y being the high order word. I
use
two functions to get the LoWord and HiWord from the LParam

Private Shared Function HiWord(ByVal Number As Integer) As Integer
Return ((Number >> 16) And &HFFFF)
End Function

Private Shared Function LoWord(ByVal Number As Integer) As Integer
Return (Number And &HFFFF)
End Function


That code is only correct for unsigned integers. For signed integers the
sign bit gets shifted to the high order bit of the low order word by the
HiWord function. The clearest and simplest way to do this in VB.NET is to
use the BitConverter. When you want to extract types embedded in some
opaque byte structure, use BitConverter.

Private Shared Function HiWord(ByVal Number As Int32) As Int16
Return BitConverter.To Int16(BitConver ter.GetBytes(Nu mber), 0)
End Function
Private Shared Function LoWord(ByVal Number As Int32) As Int16
Return BitConverter.To Int16(BitConver ter.GetBytes(Nu mber), 2)
End Function

If you need to optimize this:

Private Sub ExtractWords(By Val Number As Int32, ByRef HiWord As Int16, ByRef
LoWord As Int16)
Dim b As Byte() = BitConverter.Ge tBytes(Number)
HiWord = BitConverter.To Int16(b, 0)
LoWord = BitConverter.To Int16(b, 2)
End Sub

To do this with bit shifting, you would need to pull off the sign bit, do
the shifting, and replace the sign bit in its original location. Which is
more bit twiddling than I usually care to do.

David
Nov 21 '05 #2
Hi,
LParam.ToInt32: 5636095(&H0055F FFF)
x: 65535 (&HFFFF) <-- This should be -1!!!!
y: 85 (&H0055)
This is because your functions return Integer values. &HFFFF is an
Integer literal so it is actually 0000FFFF and thus 65535. You want to
get Short values, so you can either use the following functions:

~
Private Function HiWord(ByVal Number As Integer) As Short
Return CShort(Number >> 16)
End Function

Private Function LoWord(ByVal Number As Integer) As Short
Return CShort(Number And 65535)
End Function
~

which will require to remove integer overflow checks (Project -> <Name>
Properties... -> Configuration Properties -> Optimizations -> Remove
integer overflow checks) or use BitConverter approach suggested by
David.
If this were VB6, &HFFFF would produce -1 and &HFFFF& would produce 65535. In VB.Net &HFFFF and &HFFFF& are the same, both producing 65535.


Well, they are not the same.

&HFFFF and &HFFFF% and &HFFFFI are Integer literals. They equal 0000FFFF
or 65535 (Integer value).
&HFFFF& and &HFFFFL are Long literals. They equal 000000000000FFF F or
65535 (Long value).

The one you are looking for is &HFFFFS which is Short literal. It equals
FFFF or -1 (Short value).

I hope this helps to clarify the situation.
Roman
Nov 21 '05 #3
Coolness. I'll use the BitConverter class.
Thanks

Hong Kong Phooey

"David Browne" <davidbaxterbro wne no potted me**@hotmail.co m> wrote in
message news:OR******** ******@TK2MSFTN GP14.phx.gbl...

"Hong Kong Phooey" <NO****@NOSPAM. com> wrote in message
news:eZ******** ******@TK2MSFTN GP15.phx.gbl...
I wrote a control in which I overrode WndProc to implement a custom
MouseMove. I caught the MouseMove message and passed it to a function
which
raises a custom event. In this function I get the x and y from the LParam of
the message; x being the low order word, y being the high order word. I
use
two functions to get the LoWord and HiWord from the LParam

Private Shared Function HiWord(ByVal Number As Integer) As Integer
Return ((Number >> 16) And &HFFFF)
End Function

Private Shared Function LoWord(ByVal Number As Integer) As Integer
Return (Number And &HFFFF)
End Function

That code is only correct for unsigned integers. For signed integers the
sign bit gets shifted to the high order bit of the low order word by the
HiWord function. The clearest and simplest way to do this in VB.NET is to
use the BitConverter. When you want to extract types embedded in some
opaque byte structure, use BitConverter.

Private Shared Function HiWord(ByVal Number As Int32) As Int16
Return BitConverter.To Int16(BitConver ter.GetBytes(Nu mber), 0)
End Function
Private Shared Function LoWord(ByVal Number As Int32) As Int16
Return BitConverter.To Int16(BitConver ter.GetBytes(Nu mber), 2)
End Function

If you need to optimize this:

Private Sub ExtractWords(By Val Number As Int32, ByRef HiWord As Int16,

ByRef LoWord As Int16)
Dim b As Byte() = BitConverter.Ge tBytes(Number)
HiWord = BitConverter.To Int16(b, 0)
LoWord = BitConverter.To Int16(b, 2)
End Sub

To do this with bit shifting, you would need to pull off the sign bit, do
the shifting, and replace the sign bit in its original location. Which is
more bit twiddling than I usually care to do.

David

Nov 21 '05 #4
Damn, I didn't know that about the Hex literals; that you could append
something other than a (&) to the end. Where did you ever find that
information? I've never seen that before.

Thanks
Hong Kong Phooey

"Dragon" <no@spam.please > wrote in message
news:%2******** *******@TK2MSFT NGP15.phx.gbl.. .
Hi,
LParam.ToInt32: 5636095(&H0055F FFF)
x: 65535 (&HFFFF) <-- This should be -1!!!!
y: 85 (&H0055)


This is because your functions return Integer values. &HFFFF is an
Integer literal so it is actually 0000FFFF and thus 65535. You want to
get Short values, so you can either use the following functions:

~
Private Function HiWord(ByVal Number As Integer) As Short
Return CShort(Number >> 16)
End Function

Private Function LoWord(ByVal Number As Integer) As Short
Return CShort(Number And 65535)
End Function
~

which will require to remove integer overflow checks (Project -> <Name>
Properties... -> Configuration Properties -> Optimizations -> Remove
integer overflow checks) or use BitConverter approach suggested by
David.
If this were VB6, &HFFFF would produce -1 and &HFFFF& would produce

65535.
In VB.Net &HFFFF and &HFFFF& are the same, both producing 65535.


Well, they are not the same.

&HFFFF and &HFFFF% and &HFFFFI are Integer literals. They equal 0000FFFF
or 65535 (Integer value).
&HFFFF& and &HFFFFL are Long literals. They equal 000000000000FFF F or
65535 (Long value).

The one you are looking for is &HFFFFS which is Short literal. It equals
FFFF or -1 (Short value).

I hope this helps to clarify the situation.
Roman

Nov 21 '05 #5

"Hong Kong Phooey" <NO****@NOSPAM. com> сообщил/сообщила в новостях
следующее: news:#c******** ******@TK2MSFTN GP09.phx.gbl...
Damn, I didn't know that about the Hex literals; that you could append
something other than a (&) to the end. Where did you ever find that
information? I've never seen that before.

Thanks
Hong Kong Phooey


He he, there are much more 8=]

% & @ ! # $ are type characters. Not all of them can belong to
hexademical literals, though.

See
http://msdn.microsoft.com/library/en...BSpec2_2_1.asp
for info on them.

S I L F R D C don't have a specific name AFAIK. You can find info on
them scattered around Literals chapter in VB language specification:

http://msdn.microsoft.com/library/en...fVBSpec2_4.asp

Read about integer literals here:

http://msdn.microsoft.com/library/en...BSpec2_4_2.asp

Roman
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3092
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
5021
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2649
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
3
3075
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
10
3411
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a database or continue to process on to the next page. I am now trying to learn ASP to see if we can replace some of our applications that were written in...
10
3701
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server Error in '/QuickStartv20' Application. -------------------------------------------------------------------------------- Configuration Error...
53
4043
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
4744
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
4265
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from less to more for example). I have a question object that has two properties that contain the collections Options and Ratings. now I want this kind...
3
2544
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div class="not-required-question"><p>Question Text</p><input /></div>
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprй who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.