首页 > Windows开发 > 详细

将winform窗体钉在桌面上

时间:2016-02-18 22:55:08      阅读:459      评论:0      收藏:0      [点我收藏+]

本来想做一个日历小工具的,第一点是将工具钉在桌面上,第二是将窗体透明,为了好看嘛,可经过网上大量的查阅资料,最终两者都能一一实现,但不能一块儿实现,唉实在是遗憾,最后不得不选择了WPF。如果有哪位高手看到了我写的博文,有实现的方式的话,请指教!

但是查了半天,不能白查,所以把这些代码拿出来记上,万一那天要用呢。代码关键部分不算原创,都是从网上摘的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CalendarLog.Core;
using System.Runtime.InteropServices;

namespace CalendarLog
{
    public partial class CalendarForm : Form
    {
        LogForm logForm = null;
        public CalendarForm()
        {
            InitializeComponent();
            SetToDeskTop();
        }

        private void CalendarForm_Load(object sender, EventArgs e)
        {
            DateTime nowTime = DateTime.Now;
            this.numericUpDownYear.Value = nowTime.Year;
            this.numericUpDownMonth.Value = nowTime.Month;

            DrawCalendar();
        }

        private void numericUpDownYear_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void numericUpDownMonth_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void numericUpDownYear_ValueChanged(object sender, EventArgs e)
        {
            DrawCalendar();
        }

        private void numericUpDownMonth_ValueChanged(object sender, EventArgs e)
        {
            DrawCalendar();
        }

        private void DrawCalendar()
        {
            this.panelCalendar.Controls.Clear();
            this.panelCalendar.Controls.Add(CalendarManager.GetInstance().GetCalendar(Convert.ToInt32(numericUpDownYear.Value), Convert.ToInt32(numericUpDownMonth.Value)));
        }

        private void CalendarForm_MouseDown(object sender, MouseEventArgs e)
        {
            Win32.ReleaseCapture();
            Win32.SendMessage(this.Handle, Win32.WM_SYSCOMMAND, Win32.SC_MOVE + Win32.HTCAPTION, 0);//窗体移动
        }

        #region 将窗体钉在桌面上
        private void SetToDeskTop()
        {
            try
            {
                if (Environment.OSVersion.Version.Major < 6)
                {
                    base.SendToBack();
                    IntPtr hWndNewParent = Win32.FindWindow("Progman", null);
                    Win32.SetParent(base.Handle, hWndNewParent);
                }
                else
                {
                    IntPtr desktopHwnd = GetDesktopPtr();
                    IntPtr ownHwnd = base.Handle;
                    IntPtr result = Win32.SetParent(ownHwnd, desktopHwnd);

                }

            }
            catch (ApplicationException exx)
            {
                MessageBox.Show(this, exx.Message, "Pin to Desktop");
            }
        }

        private IntPtr GetDesktopPtr()
        {
            //http://blog.csdn.net/mkdym/article/details/7018318
            // 情况一
            IntPtr hwndWorkerW = IntPtr.Zero;
            IntPtr hShellDefView = IntPtr.Zero;
            IntPtr hwndDesktop = IntPtr.Zero;
            IntPtr hProgMan = Win32.FindWindow("ProgMan", null);
            if (hProgMan != IntPtr.Zero)
            {
                hShellDefView = Win32.FindWindowEx(hProgMan, IntPtr.Zero, "SHELLDLL_DefView", null);
                if (hShellDefView != IntPtr.Zero)
                {
                    hwndDesktop = Win32.FindWindowEx(hShellDefView, IntPtr.Zero, "SysListView32", null);
                }
            }
            if (hwndDesktop != IntPtr.Zero) return hwndDesktop;

            // 情况二
            while (hwndDesktop == IntPtr.Zero)
            {//必须存在桌面窗口层次  
                hwndWorkerW = Win32.FindWindowEx(IntPtr.Zero, hwndWorkerW, "WorkerW", null);//获得WorkerW类的窗口  
                if (hwndWorkerW == IntPtr.Zero) break;//未知错误
                hShellDefView = Win32.FindWindowEx(hwndWorkerW, IntPtr.Zero, "SysListView32", null);
                if (hShellDefView == IntPtr.Zero) continue;
                hwndDesktop = Win32.FindWindowEx(hShellDefView, IntPtr.Zero, "SysListView32", null);
            }
            return hwndDesktop;
        }


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

        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {

            if (Environment.OSVersion.Version.Major >= 6)
            {
                Win32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, Win32.SE_SHUTDOWN_PRIVILEGE);
            }
        }
        #endregion

        private void labelOpenLog_Click(object sender, EventArgs e)
        {
            if (logForm == null)
            {
                logForm = new LogForm();
            }
            if (!logForm.Visible)
            {
                logForm.Show(this);
                SetLogFormLocation();
            }
        }

        private void CalendarForm_LocationChanged(object sender, EventArgs e)
        {
            SetLogFormLocation();
        }

        private void SetLogFormLocation()
        {
            logForm.Location = new Point(this.Location.X + this.Width + 1, this.Location.Y);
        }
    }
}

Win32类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CalendarLog.Core
{
    public class Win32
    {
        public const int SE_SHUTDOWN_PRIVILEGE = 0x13;
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;//无边框窗体移动
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx,
            int cy, uint uFlags);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
    }
}

 

将winform窗体钉在桌面上

原文:http://www.cnblogs.com/zoudiaogangqin/p/5199355.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!