GDI+

I am not sure if its a right place for this thread, sorry, move it if needed and was possible.

I am using Drawing namespace and learning GDI+. I want to draw some shapes and paths on the screen. The problem is that a window is always needed to create graphics object. It is just like a prison for the shapes. What is the solution I want my shapes to be seen on the desktop and on the wallpaper for example, not in a window.

Thanks.




Answer this question

GDI+

  • GUYO

    Hi,

    You need to get the mouse offset first

    private void Form1_MouseDown(object sender,
    System.Windows.Forms.MouseEventArgs e)
    {
    mouse_offset = new Point(-e.X, -e.Y);
    }

    private void Form1_MouseMove(object sender,
    System.Windows.Forms.MouseEventArgs e)
    {
    if (e.Button == MouseButtons.Left)
    {
    Point mousePos = Control.MousePosition;
    mousePos.Offset(mouse_offset.X, mouse_offset.Y);
    Location = mousePos;
    }
    }

    Cheers



  • VGIN

    Thank you. It was much simpler than I thought.

    But I have another problem with it. I want it to be movable by user (by mouse).

    private: System::Void gdiform_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {

    if(e->Button == Windows::Forms::MouseButtons::Left) this->Location=Drawing::Point(e->Location.X,e->Location.Y);

    }

    This code wont work. When I try to move it it goes crazy.



  • Priyank Gajera

    Hi,

    You need to get the handle from the desktop. You need to call the unmanaged method "GetDesktopWindow". Check the following example:

    [DllImport("user32.dll")]
    static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    static extern IntPtr GetDCEx(IntPtr hwnd, IntPtr hrgn, uint flags);

    IntPtr hdc = GetDCEx(GetDesktopWindow(), IntPtr.Zero, 1027);
    using(Graphics g = Graphics.FromHdc(hdc))
    {
    g.FillEllipse(Brushes.Red, 0, 0, 400, 400);
    }

    Hope you find the post useful,

    Best regards



  • ImGivingUp

    Hi,

    Yes, they are really cool, I think that are windows with bitmap shapes with the AlwaysOnTop property.

    Cheers, and enjoy!



  • Bishnupada Mukhopadhyay

    Yes it works.

    Why my code didn't worked What is the mouse offset



  • tattoo

    SalvaPatuel wrote:

    HI,

    What if you create a borderless window that is not shown on the taskbar Will this work for you

    Cheers

    I don't know. But I think there is a usual way to do it. Have you seen mp3dancer or virtuagirl



  • summerwind

    Search google for draw on desktop getdc, there are loads of code samples online.

  • Mystagogue

    I am using C++/CLI and I can't merge managed and unmanaged code.

    It fails to use dll:
    #using "C:\\Windows\\System32\\user32.dll"

    I think it is because that dll is not in CL.

    It also wont link when I include <windows.h>



  • Haacked

    AndrewVos wrote:
    To give you a very rough idea of how it's done, open virtuagirl.exe up in notepad... If it's not to small you should easily be able to find the api calls... For a veery rough idea :)

    Open a binary code in notepad

    Borderless window is a good idea, i just don't know how to refresh its background to show the things below it.



  • Kennon2005

    I solved the problem by giving user32.lib to linker. But GetDestopWindow() is not what I want. It really paints desktop, but I want an invisible layer (window) on the desktop to be painted.

  • osamaT

    Hi,

    The mouse offset is the difference between the cursor and the form location. Otherwise you only take the form location no matter where your mouse is.

    Hope this helps, mark it as an answer so we can close the post and the solution will appear on the VS help.

    Happy Coding and good luck!



  • Tom B

    Hi my friend,

    Here you can find an example of how to do borderless with custom shapes (so the background is visible).

    Hope this helps http://www.thecodeproject.com/csharp/CustomForms.asp

    Cheers



  • Jo-Jo

    HI,

    What if you create a borderless window that is not shown on the taskbar Will this work for you

    Cheers



  • sharyl

    To give you a very rough idea of how it's done, open virtuagirl.exe up in notepad... If it's not to small you should easily be able to find the api calls... For a veery rough idea :)

  • GDI+