现在的位置: 首页 > 综合 > 正文

winform嵌入桌面,win+d也不最小化

2013年11月22日 ⁄ 综合 ⁄ 共 1479字 ⁄ 字号 评论关闭
[DllImport("user32.dll", EntryPoint = "SetParent")] 
public static extern int SetParent(int hWndChild, int hWndNewParent); 

[DllImport("user32.dll", EntryPoint = "FindWindow")] 
public static extern int FindWindow(string lpClassName, string lpWindowName); 

public Form1() 
{ 
InitializeComponent(); 
SetParent(this.Handle.ToInt32(), FindWindow("Progman", "Program Manager")); 

} 

以上在win7以下系统基本正常,在win7里会出现界面不出的现像。

下面这段可用,进行了系统判断。

        private const int SE_SHUTDOWN_PRIVILEGE = 0x13;

        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

        public djradio()
        {
            InitializeComponent();
            MessageBox.Show(Environment.OSVersion.Version.Major.ToString());
            try
            {
                if (Environment.OSVersion.Version.Major<6)
                {
                    base.SendToBack();

                    IntPtr hWndNewParent = FindWindow("Progman", "Program Manager");
                    SetParent(base.Handle, hWndNewParent);
                }
                else
                {
                    SetWindowPos(base.Handle, 1, 0, 0, 0, 0, SE_SHUTDOWN_PRIVILEGE);
                }
            }
            catch (ApplicationException exx)
            {
                MessageBox.Show(this, exx.Message, "Pin to Desktop");
                //throw;
            }
        }

        private void djradio_Activated(object sender, EventArgs e)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                SetWindowPos(base.Handle, 1, 0, 0, 0, 0, SE_SHUTDOWN_PRIVILEGE);
            }

        }

        private void djradio_Paint(object sender, PaintEventArgs e)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                SetWindowPos(base.Handle, 1, 0, 0, 0, 0, SE_SHUTDOWN_PRIVILEGE);
            }
        }

抱歉!评论已关闭.