C# I'd like a transparent form on the back...

Asked 2 years ago, Updated 2 years ago, 117 views

Thank you for your help.

I tried to make a desktop accessory with C# but I failed.
Set the TransparencyKey on the form to BackColor on the form and
Make BorderStyle None, and even mascot characters
It's done to make it.

After that, I always wanted to display it on the back, so I looked at the sample net and saw
I added it as follows.

public class Form 1: System.Windows.Forms.Form
{

    // FindWindow function
    [DllImport("USER32.DLL", CharSet=CharSet.Auto)]
    private static external System.IntPtr FindWindow(
        string lpClassName,
        US>string lpWindowName
    );


    // SetParent function
    [DllImport("USER32.DLL", CharSet=CharSet.Auto)]
    private static external System.IntPtr SetParent(
        System.IntPtrWndChild,
        System.IntPthWndNewParent
    );


    private void button1_Click (object sender, System.EventArgse)
    {
        // Obtain Program Manager Handle
        System.IntPathProgramManagerHandle=FindWindow(null, "Program Manager");

        // If successful, set the Program Manager to the parent window.
        if(!hProgramManagerHandle.Equals(System.IntPtr.Zero))
        {
            SetParent (this.Handle, hProgramManagerHandle);
        }
    }
}

After executing this code, the form itself is completely
rather than displaying it on the back. It will be hidden.Moreover, it will no longer appear on the taskbar, so you can manually
You will not be able to close it.

If the form is transparent, will it be possible to specify it as the rear?

If there is a way to make it happen, could you please let me know?

Thank you for your cooperation.

c# winforms

2022-09-30 20:13

1 Answers

In Windows 8.1 C# Windows Form.NetFramework 4.5, we were able to place the transparent form on the back with the following code:

public partial class Form 1:Form
{
    public delete bool EnumWindowCallBack (IntPtr hwnd, IntPtrParam);

    DllImport("USER32.DLL", EntryPoint="FindWindowEx")]
    private static external IntPtr FindWindowEx (IntPtrWnd1, IntPtrWnd2, string lpsz1, string lpsz2);

    // SetParent function
    [DllImport("USER32.DLL", CharSet=CharSet.Auto)]
    private static external System.IntPtr SetParent(
        System.IntPtrWndChild,
        System.IntPthWndNewParent
    );

    [DllImport("USER32.Dll")]
    static external EnumWindows (EnumWindowCallBack x, IntPty);

    DllImport("User32.Dll", CharSet=CharSet.Unicode)
    public static external int GetClassName (IntPtrWnd, StringBuilder, intnMaxCount);


    public Form 1()
    {
        InitializeComponent();
        this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None;
    }

    protected override void OnPaintBackground (PaintEventArgse)
    {
    }

    private void button1_Click(object sender, EventArgse)
    {
        EnumWindows (EnumerateWindow, IntPtr.Zero);
    }

    // CALLBACK METHOD FOR ENumerating WINDOWS
    private bool EnumerateWindow (IntPtrhWnd, IntPtrParam)
    {
        StringBuilder sbClassName = new StringBuilder(256);
        result=GetClassName(hWnd, sbClassName,256);
        string className = sbClassName.ToString();
        if(className.Equals("WorkerW")||className.Equals("Progman")
        {
            IntPtrDt=FindWindowEx(hWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
            if(hDt!=null&hDt!=IntPtr.Zero)
            {
                SetParent(this.Handle,hDt);
            }
        }
        return true;
    }
}

It doesn't appear in the taskbar, so why don't you right-click the controls on the form to display and close the context menu?


2022-09-30 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.