Results 1 to 13 of 13

Thread: Converting C# code to VB.Net - need help with integers!

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32

    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.

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    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 .

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    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.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    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.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Maybe because VisualBasic namespace is imported by default .(just guessing) .

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    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.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I dunno about <DLLImport> thing but I usually do it the old way just like any VB6 proj and work just fine . sorry

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    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.

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If you want to get the location at specific point you should use GetCursorPos API , is this what you are after ?

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    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.

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    See this link for the API Call .http://www.mentalis.org/apilist/mouse_event.shtml

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    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
  •  



Click Here to Expand Forum to Full Width