|
-
May 10th, 2003, 09:29 PM
#1
Thread Starter
Member
Converting C# code to VB.Net - need help with integers!
[I have also posted this in the C# forum as it applies to both languages - apologies if you've already seen this post!]
Hi,
I have a piece of C# code I found on the web to get a color at the pixel under the mouse pointer on the screen. It uses GDI32.dll and returns an integer which holds a windows COLORREF. For example if the pointer is over a pure white pixel the integer returned is 16777215 (0xFFFFFF). It works fine in C# but when I try to run the same code (after converting it to VB.Net of course) I always get the value -1. Why is this? Surely an integer in C# is the same as an integer in VB.Net - they both derive from the same class in the CLR don't they?
Anyway, the C# code to get the color is as follows:
<code>// The following goes in the declarations section...
[DllImport("gdi32.dll")]
public static extern int GetPixel(IntPtr hdc, int x, int y);
// ...then later I do this to get the colour under the current pixel.
int cr = GetPixel(hdcScreen, pt.X, pt.Y);</code>
The same code in VB.Net looks like this:
<code> 'Declare call the GDI32.DLL GetPixel function. Should return a valid integer.
<DllImport("gdi32.dll")> _
Public Shared Function GetPixel(ByRef hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
...etc...
'Get the colour at the current pixel.
Dim cr As Integer = GetPixel(hdcScreen, pt.X, pt.Y)
End Function</code>
Can anybody tell me why the VB code returns -1 and the C# code returns the correct value of 16777215? Do I need to do something else in my VB code like a cast or something?
Pretty confused here so any help would be appreciated!
TIA...
Mike.
-
May 10th, 2003, 10:59 PM
#2
The picture isn't missing
try using an api call:
Public Declare Function GetPixel Lib "gdi32.dll" (ByRef hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
Remember, if someone's post was not helpful, you can always rate their post negatively  .
-
May 10th, 2003, 11:30 PM
#3
Thread Starter
Member
I changed my code to an API call in the style you had... didn't make any difference I'm afraid.
I've also tried making the return value from GetPixel an int32, a Short and a Long - it just returns -1 every time!
Thanks for the suggestion though...
Mike.
-
May 11th, 2003, 08:00 AM
#4
I wonder how many charact
An integer is the same in C# and VB, I have a few suggestions:
1. Make sure you use Option Strict On.... it might point out some awry conversions.
2. Check out your import statement.... do a google for using API calls in Vb. Net.... I don't have time to look at it thoroughly, but it doesn't seem right to me somehow.
-
May 11th, 2003, 04:47 PM
#5
Thread Starter
Member
Now here's a bizarre thing - when I tried changing the call from a <DLLImport> to a direct call a la VB6, ie: public shared function GetPixel lib "gdi32.dll" ( etc ), it started working... someone explain *that* to me!!
Thanks for the suggestions folks!
M.
-
May 11th, 2003, 07:12 PM
#6
Sleep mode
Maybe because VisualBasic namespace is imported by default .(just guessing) .
-
May 11th, 2003, 07:22 PM
#7
Thread Starter
Member
Pirate,
Not sure if I know what you mean - I know the VB namespace is imported by default but why would a call to a GDI function not return a proper value via a <DLLImport> - the official .Net way of doing it - but give me the correct result when doing it the "old" way? Am I missing something here?
Mike.
-
May 11th, 2003, 07:26 PM
#8
Sleep mode
I dunno about <DLLImport> thing but I usually do it the old way just like any VB6 proj and work just fine . sorry
-
May 11th, 2003, 07:45 PM
#9
Thread Starter
Member
I agree with you - I think from now on I'll just do it the old way!
BTW, do you know how to detect a left mouse button event using the Win32 API? I've only just starting calling API functions and I need to detect when a user clicks outside the .Net form so I can get the colour and location of that pixel... I know I can look it up but if you know what it is already I'd love to know!
TIA...
Mike.
-
May 11th, 2003, 08:02 PM
#10
Sleep mode
If you want to get the location at specific point you should use GetCursorPos API , is this what you are after ?
-
May 11th, 2003, 09:05 PM
#11
Thread Starter
Member
I guess I should make it clearer what I'm doing... basically I'm writing (or trying to write!) a routine that will allow the user to click anywhere on the screen and get the colour at that position.
At the moment I'm successsfully getting the actual cursor position and colour using a call to GetPixel but that's in a timer and it does a GetPixel whenever the mouse moves - I only want to do that when the user clicks the left button outside of the form... so it's the actual mouse click event I want to catch so I can *then* do a GetPixel call.
Maybe I can still use a .Net mouse event handler for this... but I assume with the .Net classes I can only get the position of the cursor (pointer) when it's in the form. If this is the case then I suspect I need to call the Win32 API to catch the click event since it will (nearly) always happen outside the form.
Sorry, bit of newbie at API stuff so am probably getting a bit confused about what I can do with .Net classes and what I need to call the API for... probably need to do a bit of reading up about it all before posting my Q's!
Cheers,
Mike.
-
May 11th, 2003, 09:48 PM
#12
Sleep mode
-
May 11th, 2003, 10:09 PM
#13
Thread Starter
Member
Thanks Pirate - that appears to be what I'm looking for... will give it a try! There are also some examples which should help too.
Mike.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|