将winform窗体钉在桌面上
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
将winform窗体钉在桌⾯上
本来想做⼀个⽇历⼩⼯具的,第⼀点是将⼯具钉在桌⾯上,第⼆是将窗体透明,为了好看嘛,可经过⽹上⼤量的查阅资料,最终两者都能⼀⼀实现,但不能⼀块⼉实现,唉实在是遗憾,最后不得不选择了WPF。
如果有哪位⾼⼿看到了我写的博⽂,有实现的⽅式的话,请指教!
但是查了半天,不能⽩查,所以把这些代码拿出来记上,万⼀那天要⽤呢。
代码关键部分不算原创,都是从⽹上摘的。
using System;
using System.Collections.Generic;
using ponentModel;
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()
{
///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);
}
}。