how to resize child window?

From: assaf (assafwo_at_hotmail.com)
Date: 10/13/04


Date: Wed, 13 Oct 2004 12:30:47 +0200

hi all

i set my window to be the parent of a 'Notepad' window.
when the parent window's size is changed,
i set the child winodow size as well.

this causes the parent to lose focus,
and the resizing stops after just changing a bit.

what can i do in order to allow my window resizing
to succeed?

assaf

here is my C#.NET code:

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Notepad
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.CheckBox checkBoxParent;
  private System.Windows.Forms.Button buttonFindWindow;
  private System.Windows.Forms.Label labelHwnd;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.checkBoxParent = new System.Windows.Forms.CheckBox();
   this.buttonFindWindow = new System.Windows.Forms.Button();
   this.labelHwnd = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // checkBoxParent
   //
   this.checkBoxParent.Location = new System.Drawing.Point(8, 40);
   this.checkBoxParent.Name = "checkBoxParent";
   this.checkBoxParent.TabIndex = 0;
   this.checkBoxParent.Text = "&Parent";
   this.checkBoxParent.CheckedChanged += new
System.EventHandler(this.checkBoxParent_CheckedChanged);
   //
   // buttonFindWindow
   //
   this.buttonFindWindow.Location = new System.Drawing.Point(8, 8);
   this.buttonFindWindow.Name = "buttonFindWindow";
   this.buttonFindWindow.Size = new System.Drawing.Size(128, 23);
   this.buttonFindWindow.TabIndex = 1;
   this.buttonFindWindow.Text = "&Find Window";
   this.buttonFindWindow.Click += new
System.EventHandler(this.buttonFindWindow_Click);
   //
   // labelHwnd
   //
   this.labelHwnd.Location = new System.Drawing.Point(144, 8);
   this.labelHwnd.Name = "labelHwnd";
   this.labelHwnd.TabIndex = 2;
   this.labelHwnd.Text = "0";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.labelHwnd);
   this.Controls.Add(this.buttonFindWindow);
   this.Controls.Add(this.checkBoxParent);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Resize += new System.EventHandler(this.Form1_Resize);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  [DllImport("user32")]
  private static extern
   int FindWindow(string lpClassName, int lpWindowName);

  [DllImport("user32")]
  private static extern
   int SetParent(int hWndChild, IntPtr hWndNewParent);

  [DllImport("user32")]
  private static extern
   bool SetWindowPos(
   int hWnd,
   int hWndInsertAfter,
   int X,
   int Y,
   int cx,
   int cy,
   uint uFlags
   );

  private void checkBoxParent_CheckedChanged(object sender, System.EventArgs
e)
  {
   if(this.checkBoxParent.Checked == true)
   {
    int j = SetParent(this._hwnd, this.Handle);
   }
   else
   {
    int j = SetParent(this._hwnd, IntPtr.Zero);
   }
  }

  int _hwnd;

  private void buttonFindWindow_Click(object sender, System.EventArgs e)
  {
   this._hwnd = FindWindow("Notepad", 0);
   this.labelHwnd.Text = this._hwnd.ToString();
  }

  private void Form1_Resize(object sender, System.EventArgs e)
  {
   bool b = SetWindowPos(this._hwnd, 0, 0, 0, this.Width, this.Height, 0);
   if(b == false)
   {
    int hr = Marshal.GetHRForLastWin32Error();
    Marshal.ThrowExceptionForHR(hr);
   }
   this.Focus();
  }
 }
}