Creating a shape for a window

I know how to make a window a certian shape and dissable the title bar but how do you allow it to be moved in the same way as a normal window. 

Like lets say i make a window that is shaped like a cd and it has no titlebar
but i would like to click anywhere on the cd and move it on the screen


Answer this question

Creating a shape for a window

  • RStanton

    cool i will look into this when i get home

  • zapacila89

    I may have forgotten to mention this but i am new to this so i am try to learn all of the commands too
    I am going to try this when i get a chance, but what does the PointToScreen command do

    and i guess i really dont need to know based on shape just if i can click anywhere on the window and drag it around

  • ScrappyDoo

    everybody is a novice to the things he doesnot know, a piece on the help reads:

    public Point PointToScreen(Point p);

    Parameters
         p The client coordinate Point to convert. 

    Return Value
         A Point that represents the converted Point, p, in screen coordinates.

    // as to the reverse:
    public Point PointToClient(Point p);

    Parameters
        p The screen coordinate Point to convert. 
    Return Value
        A Point that represents the converted Point, p, in client coordinates.

    I hope the PTB's enable reversible drawing in Graphics, so that these conversions are not neccessairy. It is just the controls are drawn in coordinates relative to their parents, while DrawReversibleLine(..) uses screen coordinates. Always a headache of figuring out exactly the what is what and the who is who in these kind of cases. Setting breakpoint and note the different values the event parameter holds, and some puzzling on what to use, you'll probably get thing 'stable' the way you want.

    strumbling around through the help system is always helpfull to figure thing out, many a sample is in the MSDN help. (also available on line)

  • Kingsley Tagbo

    Nice to see that my ad-hockly defined code was that close, I didn't intend to do all the work. Also as you noted one thing for the PTB's to fix. Why on earth can't the fact that the mousebuton is down not simply be read off from the MouseEventArgs passed to the MouseMove eventhandler  Also the state of the special keys <ctrl> <alt> <shift> are often needed in mousehandlers, and should be readily available, without the need of special variables like the mouseDown above. (simular statement for the key-event-handler, keeping tracks of simple things is awkward programming)
  • ShimonSim

    when i change x and y into me.left and me.top the form goes nuts i cant really tell what it is trying to do it just seems to move mindlessly
  • srii

    oops, 'window' you might replace by 'this' (me I believe in basic), I don't know how you code your shape! The basic looks ok to me, but I don't use basic, so just experiment somewhat.
    Note that the code was "ad hock" and therefore "not tested", perhaps you need method <strong>PointToScreen(..)</strong> to stabilize your windows eratic behaviour by converting client coordinates to screen coordinates. As said experiment somewhat with it, somewhere along these lines it ought to work (in debugmode you might figure out what the exact values are, these might give you a clue how to stabilize your windows behaviour)

  • Paulo Aboim Pinto

    Heres all the Code you need...Aale you forgot one thing...you want to add an event handler for mouse up and keep track of when the mouse is down so in mousemove you only move the form when the mouse is actually down

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    namespace WindowsApplication2
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.ComponentModel.Container components = null;
    private bool mouseDown = false;
    private int xOffset = 0;
    private int yOffset = 0;
    public Form1()
    {
    InitializeComponent();
    MouseDown +=new MouseEventHandler(Form1_MouseDown);
    MouseUp +=new MouseEventHandler(Form1_MouseUp);
    MouseMove +=new MouseEventHandler(Form1_MouseMove);
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
    mouseDown = true;
    xOffset = e.X;
    yOffset = e.Y;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
    mouseDown = false;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
    if(mouseDown)
    {
    this.Left = e.X + Left - xOffset;
    this.Top = e.Y + Top - yOffset;
    }
    }
    }
    }

  • Obble

    alright i am trying the code you gave me but i am using vb.net so i sort of changed it but i could not find the equivalent of window

    what can i use to make this work correctly

    here is what i have so far


     Dim winx, winy, msx, msy As Integer

        Private Sub frmNPCalc_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            winx = window.X
            winy = window.Y
            msx = e.X
            msy = e.Y
        End Sub

        Private Sub frmNPCalc_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
            window.X = winx + e.X - msx
            window.Y = winy + e.Y - msy
        End Sub

  • Regis St-Gelais

    i agree aale it would definitely be nice to have those features because it would require a lot less code to do stuff like that
  • dabro

    I'd approach it like this:

    Set the Form.FormBorderStyle to None.
    Set Form.TransparencyKey to something you're not going to use in the form eg bright red. Then set the Form.BackColor to be the same.
    Add an Image Control that is a big circle (with the outside colour set to bright Red - or maybe transparent - not tried this yet).
    Send this image to the back (Send to Back).
    Add your controls on top of the circle.

    Moving the circle - not so sure (sorry!)

    I've used this technique and it works (its a work round)
    Put a very thin Panel horizontally across the top of your form.
    Make it invisible (bright red).
    Use the Panels Mouse.Enter event to set the Form.FormBorderStyle = Fixed (or other) and also enable the timer (see below).
    Add a Timer that lasts for 2 seconds. When the timer event fires set the Form.FormBorderStyle to None.

    The effect is that you have a circle with controls on it and no form border. When the mouse goes near (over) the top of the invisible form (ie goes over the long thin Panel) then form border appears (allowing form to be moved, minimised etc). After 2 seconds the form border disappears leaving the circle again.

    Hope that helps.

    Chiz.

  • sou1_pure

    it looks to me a simply matter of connecting the mouseklick and the mousemove:

    window.MouseDown += new MouseEventHandler(OnMouseDown);
    window.MouseMove += new MouseEventHandler(OnMouseMove);

    to the handlers:

    int winx,winy,msx,msy;
    private void OnMouseDown(object sender,MouseEventArgs e)
    {
    winx = window.X;
    winy = window.Y;
    msx = e.X;
    msy = e.Y;
    }
    private void OnMouseMove(object sender,MouseEventArgs e)
    {
    window.X = winx + e.X - msx;
    window.Y = winy + e.Y - msy;
    }

    or something simular (note the above I coded at hock, but something along these lines ought to work I think)

  • Creating a shape for a window