I want to have a small window like thing on the desktop that's always on the desktop and it would just have two buttons(that does some work) as the desktop gadgets are discontinued from windows 8.Any Other Hard-Code alternatives to do that?
Asked
Active
Viewed 468 times
1 Answers
2
Yes it's possible, but it does require a bit of work. To overview the process:
- Set the form's
ShowInTaskbar
property to false, so it doesn't show up in the taskbar. - Set the form's
BorderStyle
property to None. (Which also conveniently happens to remove the caption bar, minimize/maximize buttons, etc.) - Override the window procedure for the form, and handle the
WM_WINDOWPOSCHANGING
message, so that you can keep the window at the bottom of the Z-Order. - Handle the
WM_NCHITTEST
message for your form so that you can move the form by dragging it by its background. ReturnHTCAPTION
so that the system acts like your mouse is in the caption bar of the form.
As a simple demo, create a form, with the ShowInTaskbar
property set to false, and BorderStyle
set to None. Then add the following code:
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
private const int WM_NCHITTEST = 0x0084;
private const int WM_WINDOWPOSCHANGING = 0x0046;
private const int HTCAPTION = 2;
private const int HTCLIENT = 1;
private struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public uint flags;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
m.Result = new IntPtr(HTCAPTION);
return;
}
else if (m.Msg == WM_WINDOWPOSCHANGING)
{
WINDOWPOS posInfo = Marshal.PtrToStructure<WINDOWPOS>(m.LParam);
posInfo.hwndInsertAfter = HWND_BOTTOM;
Marshal.StructureToPtr(posInfo, m.LParam, true);
}
base.WndProc(ref m);
}

theB
- 6,450
- 1
- 28
- 38