Justine Krejcha
Justine Krejcha

Reputation: 2176

Drawing Windows Form inside Window

In some environments, like the Office macros environment, or Visual Studio, there are designers which can show a preview, which you can drag and drop items on to it. They are rendered using the same theme as the rest of the OS. I'm not trying to create an MDI, but rather draw a windows form in the same form.

Example:

VBA drawing form

In C#, how would I go about implementing a similar functionality?

Upvotes: 1

Views: 1061

Answers (2)

TaW
TaW

Reputation: 54453

You can use this nice method to make a form2 draw itself onto a target bitmap:

form2.DrawToBitmap(tagetBmp, new Rectangle(0, 0, form2.Size.Width, form2.Size.Height));

To make it work you would create a form to show and call this method on it. It will include all its controls etc but if you want it empty you would need just an empty form of the right proportions..

Here is a test, which draws a window onto itself:

private void button1_Click(object sender, EventArgs e)
{
    Size oldSize = this.Size;
    Size newSize = new Size(this.Width / 4,  this.Height / 4);
    Bitmap targetBmp = new Bitmap(newSize.Width, newSize.Height);
    this.Size = newSize;
    button1.Hide();
    this.DrawToBitmap(targetBmp, new Rectangle(Point.Empty, newSize));
    this.Size = oldSize;
    using (Graphics G = this.CreateGraphics()) 
    { 
        G.DrawImage(targetBmp, 0, 0); 
    }
    button1.Show();
}

Upvotes: 1

Scoregraphic
Scoregraphic

Reputation: 7210

Maybe you should have a look at this article: http://msdn.microsoft.com/en-us/magazine/cc163871.aspx

The article discusses:

  1. Design-time environment fundamentals
  2. Forms designer architecture
  3. The implementation of the forms designer in Visual Studio .NET
  4. Services you need to implement to write a forms designer for your own application

Upvotes: 3

Related Questions