Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6234629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:31:11+00:00 2026-05-24T10:31:11+00:00

I create a GC on the display, and then I do some drawing. My

  • 0

I create a GC on the display, and then I do some drawing. My question is how do I un-draw?

The code looks like this:

final GC gc = new GC(display);
gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
gc.setLineWidth(5);
gc.drawRectangle(rectangle);
gc.dispose();

Context:
I need to let users select a window from other applications. The behavior I expect can be seen here: http://tools.tortoisesvn.net/SendMessage.html Instead, All my screen is filled with red rectangles.

It is OK for me even if it is a Windows-only solution.

EDIT: sorry, red garbage remains even after I close my application.

EDIT2: The working example:

public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);

        shell.addListener(SWT.MouseMove, new Listener() {
            @Override
            public void handleEvent(Event event) {
                final Point displayPoint = display.map(shell, null, event.x, event.y);
                final POINT point = new POINT();
                point.x = displayPoint.x;
                point.y = displayPoint.y;
                final int windowHandle = OS.WindowFromPoint(point);
                if (windowHandle != 0 && windowHandle != shell.handle) {
                    RECT rect = new RECT();
                    if (OS.GetWindowRect(windowHandle, rect)) {
                        Rectangle rectangle = new Rectangle(rect.left, rect.top, rect.right - rect.left,
                                rect.bottom - rect.top);
                        final GC gc = new GC(display);
                        gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                        gc.setLineWidth(5);
                        gc.drawRectangle(rectangle);
                        gc.dispose();
                    }
                }
            }
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

To use it, start a mouse drag from the shell (not the title bar) and hover it over an application that uses real windows controls (not swing, QT, XUL). A good example of target application is Total Commander. You will see that the screen becomes full of red rectangles. Ideally I would like to have only one red rectangle visible.

I know I could make a new shell with regions that will simulate the red rectangle, but if the mouse jumps over that, I’m stuck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:31:13+00:00Added an answer on May 24, 2026 at 10:31 am

    I make some code. It’s not perfect solution, cause after many tries I’m not able to make transparency everything except the “window” border, so I’m just making the whole shell (which covers the “window” area) partially transparent (and it makes nice effect though).

    Here’s the code

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.PaintEvent;
    import org.eclipse.swt.events.PaintListener;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.internal.win32.OS;
    import org.eclipse.swt.internal.win32.POINT;
    import org.eclipse.swt.internal.win32.RECT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Canvas;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Shell;
    
    public class ShellBorder {
    
        private Display display = new Display();
        private Shell shell = new Shell(display);
        private RECT currRect = null;
        private Shell paintShell = null;
    
        public ShellBorder() {
    
            shell.addListener(SWT.MouseUp, new Listener() {
    
                @Override
                public void handleEvent(Event event) {
                    paintShell.dispose();
    
                    // do whatever you need
                    // ...
    
                    currRect = null;
                }
            });
    
            shell.addListener(SWT.MouseMove, new Listener() {
                @Override
                public void handleEvent(Event event) {
                    final Point displayPoint = display.map(shell, null, event.x, event.y);
                    final POINT point = new POINT();
                    point.x = displayPoint.x;
                    point.y = displayPoint.y;
                    if(currRect == null) {
                        getWindowAndDrawBorder(point);
                    } else {
                        // cursor is outside the current rectangle
                        if (point.x < currRect.left || point.x > currRect.right || point.y < currRect.top || point.y > currRect.bottom) {
                            currRect = null;
                            paintShell.dispose();
                            getWindowAndDrawBorder(point);
                        }
                    }
                }
    
                private void getWindowAndDrawBorder(POINT point) {
                    long windowHandle = OS.WindowFromPoint(point);
                    if (windowHandle != 0 && windowHandle != shell.handle) {
                        RECT rect = new RECT();
                        if (OS.GetWindowRect(windowHandle, rect)) {
                            currRect = rect;
    
                            paintShell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
                            paintShell.setLocation(currRect.left, currRect.top);
                            paintShell.setSize(currRect.right - currRect.left, currRect.bottom - currRect.top);
                            paintShell.setLayout(new FillLayout());
                            paintShell.setAlpha(50);
    
                            Canvas canvas = new Canvas(paintShell, SWT.NO_BACKGROUND);
                            canvas.addPaintListener(new PaintListener() {
                                public void paintControl(PaintEvent e) {
                                    GC gc = e.gc;
                                    gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
                                    gc.setLineWidth(5);
                                    gc.drawRectangle(new Rectangle(0, 0, paintShell.getSize().x, paintShell.getSize().y));
                                }
                            });
                            paintShell.open();
                        }
                    }
                }
            });
    
            shell.pack();
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
            display.dispose();
    
        }
    
        public static void main(String[] args) {
            new ShellBorder();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I need to create a module in drupal to display some data, not
I've been trying to create some php code that would let me take input
I want to use jQuery to consume an RSS feed, and then create some
I'm trying to use jQuery and RaphaelJS to: Create circles Display some information when
I am trying to create a page to display a list of links for
I'm trying to create an array to display the last 5 products a customer
I want to create application which would display spatial data on Map. I'm thinking
I'm trying to create a layout with display: table and alike, but it seems
I want to create fixed status bar in by web form to display various
I'm trying to create a SharePoint web part that will display all the users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.