catch the close button event on a windows form

In order to catch the close button event of the form we need to override the WndProc method.

Code:

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
    }

    protected override void WndProc(ref Message M)
    {
        const int WM_SYSCOMMAND = 0x112;
        const int SC_CLOSE = 0xf060;

        if (M.Msg == WM_SYSCOMMAND & M.WParam.ToInt32() == SC_CLOSE)
        {
            // User clicked the X - close button
            // Any code handling goes here:

            return;
        }

        base.WndProc(ref M);
    }
}
Advertisements

Balls bouncing and colliding on the screen

Just a small graphical application i made in my free time.
The program has a ball form which is called in a loop from the void main method.

We catch the paint event which is where the ball is drawn:

private void BallForm_Paint(object sender, PaintEventArgs e)
{
    Width = Convert.ToInt32(radios * 2); //24;
    Height = Convert.ToInt32(radios * 2); //24;

    Graphics g = e.Graphics;
    g.Clear(Color.Cyan);
    g.FillPie(new SolidBrush(col), 0, 0, Width - 1, Height - 1, 0, 360);
    g.DrawArc(new Pen(Color.Black,2), 0, 0, Width - 1, Height - 1, 0, 360);
}

Gravity:
the balls are under the force of gravity there for always pulled down, to get this effect every tick  the Y vector decreased in just a bit which simulated gravity and can be modified to have a stronger gravitational pull if increased

double gravity = 0.1;
moveY += gravity;

also the X vector is changed to simulate fraction
moveX *= 0.75;

Collision:
balls that collide with one each other are calculated to bounce away in the opposite direction of the impact, in this case some physics is needed to calculate the direction of 2 collisions using the radios of the circles equation to find if one ball entered another balls radios space

double distIn = Math.Sqrt(Math.Pow((int)x1 - (int)x2, 2) + Math.Pow((int)y1 - (int)y2, 2));
m = (y2 - y1) / (x2 - x1);
double b = y1 - (m * x1);

and the velocity of the ball which absorbs the velocity from the ball it collided with
double difX = Math.Abs(oBall.moveX – moveX);
double difY = Math.Abs(oBall.moveY – moveY);

if right clicking on a ball a small menu will open and allow some functionality like make it bounce again(which will any way happen when the ball stops movement) the tricky part is to catch the right click event exactly when the pointer is over the ball good luck 🙂

here is the source code:

Visual studio 2005: BouncingBall-SRC.zip

set the Tab Key in Infragistics UltraTabWorkspace

In the Infragistics.Practices.CompositeUI.WinForms.UltraTabWorkspace workspace.
one of the projects i worked on we needed to have the Tab item key property set with a unique key, unfortunately the key property is not set when the tab is created(actually it was never set and always resolved to be null).

first of all create a custom SmartPartInfo that will hold an extra property TabKey.

public class CustomTabSmartPartInfo : Infragistics.Practices.CompositeUI.WinForms.UltraTabSmartPartInfo
{
    private string _TabKey;
    public string TabKey
    {
        get { return _TabKey; }
        set { _TabKey = value; }
    }
}

next inherit from the UltraTabWorkspace control and override two methods that will help set the key “manually”.

/// <summary>
/// inherited UltraTabWorkspace object in order to get access to the override methods
/// </summary>
/// <remarks>the reason is to set the TabKey property when the a workItem is created</remarks>
public class CustomUltraTabWorkspace : Infragistics.Practices.CompositeUI.WinForms.UltraTabWorkspace
{
    private string addTabKey;

    protected override void OnShow(System.Windows.Forms.Control smartPart, Infragistics.Practices.CompositeUI.WinForms.UltraTabSmartPartInfo smartPartInfo)
    {
        if ((smartPartInfo) is CustomTabSmartPartInfo)
        {
            if (Strings.Len(((CustomTabSmartPartInfo)smartPartInfo).TabKey) > 0)
            {
                addTabKey = ((CustomTabSmartPartInfo)smartPartInfo).TabKey;
            }
        }
        base.OnShow(smartPart, smartPartInfo);
        addTabKey = string.Empty;
    }

    protected override void OnControlAdded(System.Windows.Forms.ControlEventArgs e)
    {
        base.OnControlAdded(e);
        if (Strings.Len(addTabKey) > 0)
        {
            if ((e.Control) is Infragistics.Win.UltraWinTabControl.UltraTabPageControl)
            {
                ((Infragistics.Win.UltraWinTabControl.UltraTabPageControl)e.Control).Tab.Key = addTabKey;
            }
        }
    }
}

when OnShow() is called for the first time OnControlAdded() will be called as well, this is the point to set the Tab Key which is extracted from the Custom smartPartInfo created earlier.

CustomTabSmartPartInfo spi = new CustomTabSmartPartInfo();

spi.Title = tabText;
spi.TabKey = mSessionId + mName + ":" + viewId; ;
spi.ActivateTab = activateTab;

MyWorkspace.Show(thisView, spi);

All set the Tab’s Key property is populated and we have control over the content of the key, in my case it had to reflect the active session and the view id.