Sign in to follow this  
avianRR

[.net] get HDC from usercontrol object?

Recommended Posts

I need to get the HDC of a System::Windows::Forms::UserControl object. All I've found so far is a Handle that's a IntPtr and won't work for the dll function I need to call.

Share this post


Link to post
Share on other sites
How about something like this:
using(Graphics g = this.CreateGraphics())
{
IntPtr hdc = g.GetHdc();

// use it here...
}

If it's in an OnPaint method, you don't need to do CreateGraphics(); you can just use the Graphics property of the PaintEventArgs object.

Share this post


Link to post
Share on other sites
Actually what I need is a HDC. I need to pass the value to a function in a dll. It won't let me typecast the IntPtr to a HDC value.

Share this post


Link to post
Share on other sites
Ok, this is how I got it working.

HWND hWnd = (HWND)this->Handle.ToInt32();
hDC = GetDC(hWnd);



Maybe someday I'll get the hang of this .NET stuff...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this