Tuesday 28 April 2015

Move parent and child window together in windows / WPF application



Hello guys,
Today I am sharing my experience to move parent and child window together. Suppose you have a parent window and on the top of that one child window you need to open.
Now if you move parent window then child must be move together.
Another thing child must be shown under the parent window area. Whenever you move or resize the parent window, the child window must not leave its position. That is the primary requirement.

In Window:
Create one project in that you will get one main form. Open that form and add one button. Now double click on button, so you’ll get its button click event. Now insert the below code to achieve our goal.



private void button1_Click(object sender, EventArgs e)
        {
            Form mainWindow = Form.ActiveForm;

            Form wnd = new Form();
            wnd.Height = 30;
            wnd.Width = mainWindow.Width;
            wnd.Top = (mainWindow.Top + mainWindow.Height) - 50;
            wnd.Left = mainWindow.Left; //0;
            wnd.FormBorderStyle = FormBorderStyle.None;
            wnd.Name = "hi";
            wnd.Owner = mainWindow;
            wnd.ShowInTaskbar = false;
            wnd.BackColor = Color.Red;
            wnd.StartPosition = FormStartPosition.Manual;

            Label lbl = new Label();
            lbl.Text = "lasjdflaj lk asldfj alsk fd;las dfasldjf aslkfj las;kjdf l;aks dflas fd";

            wnd.Controls.Add(lbl);
            wnd.Show();

        }

No insert the below code.

public Form1()
        {
            InitializeComponent();

            this.SizeChanged += new EventHandler(Window_SizeChanged);
            this.LocationChanged += new EventHandler(Window_LocationChanged);
        }

private void Window_SizeChanged(object sender, EventArgs e)
        { ChildRelocate(); }
               
        private void Window_LocationChanged(object sender, EventArgs e)
        { ChildRelocate(); }

        private void ChildRelocate()
        {
            foreach (Form item in Application.OpenForms)
            {
                if (item.Name == "hi")
                {
                    Form mainWindow = Form.ActiveForm;
                    item.Width = mainWindow.Width;
                    item.Top = (mainWindow.Top + mainWindow.Height) - 50;
                    item.Left = mainWindow.Left;
                    item.StartPosition = FormStartPosition.Manual;
                    mainWindow = null;
                }
            }
        }


In WPF:

Same as window create on project in wpf and follow the instructions.
You’ll get one main form and in that add one button and create its click event.
Now insert the below code.
public MainWindow()
        {
            InitializeComponent();
            SizeChanged += new SizeChangedEventHandler(Window_SizeChanged);
            StateChanged += new EventHandler(Window_StateChanged);
            LocationChanged += new EventHandler(Window_LocationChanged);
        }

private void btnMarquee_Click(object sender, RoutedEventArgs e)
        {
            //Marquee marquee = new Marquee();
            //marquee.ShowDialog();

            Window mainWindow = Application.Current.MainWindow;
           
            Window wnd = new Window();
            Grid grid = new Grid();
            wnd.Height = 30;
            wnd.Width = mainWindow.Width; 
            wnd.Top = (mainWindow.Top + mainWindow.Height) - 50;
            wnd.Left = mainWindow.Left;
            wnd.WindowStyle = System.Windows.WindowStyle.None;
            wnd.AllowsTransparency = true;
            wnd.Content = grid;
            wnd.Title = "hi";
            wnd.Owner = this;
            wnd.ShowInTaskbar = false;

           
            System.Windows.Controls.Canvas cn = new Canvas();
            cn.Background = Brushes.Red;
            cn.Height = 30;
           
            System.Windows.Controls.TextBlock txtMarqee = new TextBlock();
            txtMarqee.FontSize = 20;
            cn.Children.Add(txtMarqee);
            txtMarqee.Text = "This is basic example of marquee This is basic example of marquee This is basic example of marquee";

            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = -(System.Windows.SystemParameters.PrimaryScreenWidth);
            doubleAnimation.To = System.Windows.SystemParameters.PrimaryScreenWidth;
            doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(15));
            txtMarqee.BeginAnimation(Canvas.RightProperty, doubleAnimation);

            grid.Children.Add(cn);

              wnd.Show();

        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        { ChildRelocate(); }

        private void Window_StateChanged(object sender, EventArgs e)
        { ChildRelocate(); }

        private void Window_LocationChanged(object sender, EventArgs e)
        { ChildRelocate(); }

        private void ChildRelocate()
        {
            foreach (Window item in System.Windows.Application.Current.Windows)
            {
                if (item.Title == "hi")
                {
                    Window mainWindow = Application.Current.MainWindow;
                    item.Width = mainWindow.Width;
                    item.Top = (mainWindow.Top + mainWindow.Height) - 50;
                    item.Left = mainWindow.Left;
                    mainWindow = null;
                }
            }
        }

In WPF I have created an example which is similar to marqee in HTML.
Enjoy guys. Happy coading…
 

No comments:

Post a Comment