Problems getting the handle to a PictureBox as a HWND

Hi,

I have some code which casts a handle to a Picture Box to HWND. Sometimes it works fine, and at other times it generates undefined value, and I can't see why.

What I am doing is:

At startup, before the GUI is displayed, I get the address of a Forms.PictureBox control using the Handle method, which returns a System::IntPtr. According to the docs, this should be valid at this stage, and a number is always returned.

I pass this in to a method, which casts it into an HWND for passing in to an unmanaged C++ library (which generates an image to put up in the PictureBox), as follows:

System::ntPtr hwnd;

HWND phwnd;

phwnd=(HWND)hwnd.ToPointer();

Sometimes this works, but just as often it generates {HWND_) (unused=<undefined value>}

I can't find any documentation on unused or undefined value, and when this is returned it doesn't seem to be able to be trapped by a throw/catch.

Has anyone got any ideas on what is going on here Am I doing the cast wrong, or is the value of hwnd incorrect And if it is incorrect, why could this be, and what can I do about it

Any help gratefully received

Tim



Answer this question

Problems getting the handle to a PictureBox as a HWND

  • jwaters

    Hi,

    Is your problem solved

    Thank you,
    Bhanu.



  • StoneTao

    You'll probably want to attach an handler to the HandleCreated event of the PictureBox, and grab the HWND at that point.

    Steve


  • Problems getting the handle to a PictureBox as a HWND