.NET 中 对窗口中某一控件全屏显示的方法二则

示例一:

        采用API的形式调用,同时将API的调用封装成一个库,废话不说,直接上代码:

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

namespace FullScreenActiveX
{
    public class FullScreenHelper
    {
        bool m_bFullScreen = false;
        IntPtr m_OldWndParent = IntPtr.Zero;
        WINDOWPLACEMENT m_OldWndPlacement = new WINDOWPLACEMENT();
        Control m_control = null;
        public FullScreenHelper(Control c)
        {
            m_control = c;
        }
        [DllImport("User32.dll")]
        static extern bool LockWindowUpdate(IntPtr hWndLock);

        struct POINT
        {
            int x;
            int y;
        } ;
        struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        } ;

        [DllImport("User32.dll")]
        static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
        [DllImport("User32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("User32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll")]
        static extern IntPtr GetDesktopWindow();
        [DllImport("User32.dll")]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
        [DllImport("User32.dll")]
        static extern int GetSystemMetrics(int nIndex);

        public void FullScreen()
        {
            if (m_bFullScreen)
            {
                LockWindowUpdate(m_control.Handle);
                SetParent(m_control.Handle, m_OldWndParent);
                SetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);
                SetForegroundWindow(m_OldWndParent);
                LockWindowUpdate(IntPtr.Zero);
            }
            else
            {
                GetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);
                int nScreenWidth = GetSystemMetrics(0);
                int nScreenHeight = GetSystemMetrics(1);
                m_OldWndParent = m_control.Parent.Handle;
                SetParent(m_control.Handle, GetDesktopWindow());

                WINDOWPLACEMENT wp1 = new WINDOWPLACEMENT();
                wp1.length = (uint)Marshal.SizeOf(wp1);
                wp1.showCmd = 1;
                wp1.rcNormalPosition.left = 0;
                wp1.rcNormalPosition.top = 0;
                wp1.rcNormalPosition.right = nScreenWidth;
                wp1.rcNormalPosition.bottom = nScreenHeight;
                SetWindowPlacement(m_control.Handle, ref wp1);
                SetForegroundWindow(GetDesktopWindow());
                SetForegroundWindow(m_control.Handle);
            }

            m_bFullScreen = !m_bFullScreen;
        }
        struct WINDOWPLACEMENT
        {
            public uint length;
            public uint flags;
            public uint showCmd;
            public POINT ptMinPosition;
            public POINT ptMaxPosition;
            public RECT rcNormalPosition;
        } ;
    }
}

示例二,根据C#中的类,查看系统中有几个显示器,然后,将控件的大小和定位设置到相应的尺寸,该功能可以满足多显示器下的使用


private void FillScreenDisplay(Control control, bool fill)
{
	if(fill)
	{
		control.Dock = DockStyle.None;
		control.Left = 0;
		control.Top = 0;
		control.Width = Screen.PrimaryScreen.Bounds.Width;
		control.Height = Screen.PrimaryScreen.Bounds.Height;
		SetParent(this[pos].Handle,IntPtr.Zero);
		base.Parent.Hide();	
	}
	else
	{
		SetParent(control,this.Handle);
		base.Parent.Show();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值