Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » handles and native code
handles and native code [message #437973] Mon, 14 June 2004 15:01 Go to next message
Mark Freiheit is currently offline Mark FreiheitFriend
Messages: 30
Registered: July 2009
Member
Hi all...

I have a need to use an existing C++ DLL that draws graphics onto
a user provided HDC. I wish to encapsulate this drawing to a
particular portion of the screen where the rest contain SWT controlling
widgets.

I wish to create a Canvas on a Composite, and provide its handle to
the C++ code to do the rendering as in the following pseudo-code.

- java -

myComposite.addPaintListener( new PaintListener() {
public void paintControl(PaintEvent e) {
drawDocument(e.gc.handle);
} });

- C++ -

JNIEXPORT jint JNICALL Java_drawDocument (jint myHWND)
HDC myHdc = GetDC((HWND)myHWND);
drawMe(myHDC);

This does not work, and I am curious precisely why. It may have
something to do with the native DLL not "owning" the handle passed in.

If I use the examples provided in the
"Create your own widget" article, I observed that the author
creates a child window from their handle, thus...

JNIEXPORT jint JNICALL Java_drawDocument (jint myHWND)
HWND child = ::CreateWindowEx(....., (HWND)myHWND, ...);
HDC myHdc = GetDC((HWND)child);
drawMe(myHDC);

Does indeed draw something. But it loses all context of the parent.
That is, it appears to be drawing to the manager window, and is not
constrained to the original Composite that I provided.

1) Is there some way I can draw directly to the Composite I provide?
2) Is there a better way in general to accomplish my goal?

Thanks a bunch -- Mark
Re: handles and native code [message #437976 is a reply to message #437973] Mon, 14 June 2004 15:48 Go to previous message
Ivan Markov is currently offline Ivan MarkovFriend
Messages: 61
Registered: July 2009
Member
e.gc.handle is of type HDC.
You are treating this HDC handle as HWND which is wrong.

You can instead just change drawDocument to:
JNIEXPORT jint JNICALL Java_drawDocument (jint myHDC)
drawMe((HDC)myHDC);

Or alternatively, you can try passing to drawDocument *not* e.gc.handle, but
e.gc.data.drawable.handle (whgich is of type HWND)

The first approach is more correct though, because e.gc.handle has some
niceties which GetDC(gc.data.drawable.handle) doesn't have, like correct
clipping rectangle, fgnd/bgnd colors, etc.

/Ivan


"Mark" <freiheit@speakeasy.net> wrote in message
news:cakeke$fdu$1@eclipse.org...
> Hi all...
>
> I have a need to use an existing C++ DLL that draws graphics onto
> a user provided HDC. I wish to encapsulate this drawing to a
> particular portion of the screen where the rest contain SWT controlling
> widgets.
>
> I wish to create a Canvas on a Composite, and provide its handle to
> the C++ code to do the rendering as in the following pseudo-code.
>
> - java -
>
> myComposite.addPaintListener( new PaintListener() {
> public void paintControl(PaintEvent e) {
> drawDocument(e.gc.handle);
> } });
>
> - C++ -
>
> JNIEXPORT jint JNICALL Java_drawDocument (jint myHWND)
> HDC myHdc = GetDC((HWND)myHWND);
> drawMe(myHDC);
>
> This does not work, and I am curious precisely why. It may have
> something to do with the native DLL not "owning" the handle passed in.
>
> If I use the examples provided in the
> "Create your own widget" article, I observed that the author
> creates a child window from their handle, thus...
>
> JNIEXPORT jint JNICALL Java_drawDocument (jint myHWND)
> HWND child = ::CreateWindowEx(....., (HWND)myHWND, ...);
> HDC myHdc = GetDC((HWND)child);
> drawMe(myHDC);
>
> Does indeed draw something. But it loses all context of the parent.
> That is, it appears to be drawing to the manager window, and is not
> constrained to the original Composite that I provided.
>
> 1) Is there some way I can draw directly to the Composite I provide?
> 2) Is there a better way in general to accomplish my goal?
>
> Thanks a bunch -- Mark
>
Previous Topic:Can I disable a section in the StyledText (customized editor)
Next Topic:How do I contribute SWT port of JoGL
Goto Forum:
  


Current Time: Thu Apr 25 14:38:20 GMT 2024

Powered by FUDForum. Page generated in 0.02903 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top