实现在控件容器中加入可以激活标题栏的FORM

一.运行时效果

二.引子

我们通常要么在MDI窗体中处理类似的问题,要么采用设置toplevel = false的方式,将FORM做为control来add到容器中。不过,如果我们在add 到控件容器中的时候,form的标题栏是不能够激活的(也就是FORM被选中时,并没有高亮显示标题栏),那么如何可以达到这种效果呢,我想到了使用API函数SetParent,本文就是创建一个这样的控件容器。由于昨天写的匆忙,竟然没有粘贴到源代码,现在补上.特此道歉!呵呵.

三.源代码

1)控件代码

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Drawing;
using  System.Data;
using  System.Text;
using  System.Windows.Forms;

namespace  JcsExpLibary.JcsFormContainer
{
    
    
public partial class JcsFormContainer :Panel    
    
{
        
private List<Form> FormList;
        
public JcsFormContainer()
        
{
            InitializeComponent();
            FormList 
= new List<Form>();
            
this.AutoScroll = true;
            
this.HScroll = true;
            
this.VScroll = true ;
        }

       
        
public void SetParentContainer(Control c)
        
{
            
if (c!=null && c is Form)
            
{
                Form frm 
= c as Form;
                frm.WindowState 
= FormWindowState.Normal;
                frm.FormBorderStyle 
= FormBorderStyle.FixedToolWindow;
                WIN32.SetParent(frm.Handle, 
this.Handle);
                
this.FormList.Add(frm);
                frm.FormClosed 
+= new FormClosedEventHandler(frm_FormClosed);
                
//c.MaximumSize = new Size(this.ClientRectangle.Width ,this.ClientRectangle.Height );
                c.Show();
                
            }

        }


        
void frm_FormClosed(object sender, FormClosedEventArgs e)
        
{
            
this.FormList.Remove(sender as Form);
        }

        
//protected override void OnResize(EventArgs e)
        
//{
        
//    base.OnResize(e);
        
//    //foreach (Form frm in this.FormList)
        
//    //{
        
//    //    //frm.MaximumSize = new Size(this.ClientRectangle.Width, this.ClientRectangle.Height);
        
//    //}
        
//}
        
    }

}

 

2)类代码

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Runtime.InteropServices;

namespace  JcsExpLibary.JcsFormContainer
{
    
public class WIN32
    
{
        
/// <summary>
        
/// 设置控件/窗体的父级容器
        
/// </summary>
        
/// <param name="childWindow"></param>
        
/// <param name="newParent"></param>
        
/// <returns></returns>

        [DllImport("user32.dll", SetLastError = true)]
        
public static extern IntPtr SetParent(IntPtr childWindow, IntPtr newParent);
    }

}

 

3)调用测试代码

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  WindowsApplication1
{
    
public partial class Form11 : Form
    
{
        
public Form11()
        
{
            InitializeComponent();
            
//Form9 frm = new Form9();
            
//frm.MdiParent = this;
            
//frm.Show();
            Form10 frm = new Form10();
            Form10 frm1 
= new Form10();
            
this.jcsFormContainer1.SetParentContainer(frm);
            
this.jcsFormContainer1.SetParentContainer(frm1);
        }

    }

}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值