none
c#中如何使程序嵌入到桌面,成为桌面背景,和一些日历程序类似? RRS feed

  • 问题

  • 我从网上抄袭了一份程序,但是在2003系统和windows7系统下都无效,XP下没有试验过,现在把代码贴上,看看怎么回事,请高手赐教,如何才能使程序嵌入到桌面,成为桌面背景,和一些日历程序类似。

    下面是实现代码:

    首先添加引用

    using System.Runtime.InteropServices;

    接着定义方法

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

    [DllImport("user32")]
    private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    最好在窗体的 Load 事件里加入

    IntPtr hDeskTop = FindWindow("Progman", "Program Manager");
    SetParent(this.Handle, hDeskTop);


    全国最大的免费电子书下载基地http://www.52eshu.com
    2010年5月17日 10:33

答案

  • 我做了个窗体然后做了鼠标穿透,下面是代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Drawing.Imaging;


    namespace WindowsApplication3

    {

        public partial class Form1 : Form
        {
            


            private const uint WS_EX_LAYERED = 0x80000;
            private const int WS_EX_TRANSPARENT = 0x20;
            private const int GWL_STYLE = (-16);
            private const int GWL_EXSTYLE = (-20);
            private const int LWA_ALPHA = 0x2;

            [DllImport("user32", EntryPoint = "SetWindowLong")]
            private static extern uint SetWindowLong(
            IntPtr hwnd,
            int nIndex,
            uint dwNewLong
            );

            [DllImport("user32", EntryPoint = "GetWindowLong")]
            private static extern uint GetWindowLong(
            IntPtr hwnd,
            int nIndex
            );

            [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
            private static extern int SetLayeredWindowAttributes(
            IntPtr hwnd,
            int crKey,
            int bAlpha,
            int dwFlags
            );

            

            public Form1()
            {
                
                InitializeComponent();
                this.CanPenetrate();
               
            }
              

            private void Form1_Load(object sender, EventArgs e)
            {

              
               button1.Focus();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            public void CanPenetrate()
            {
                uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
                uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
                SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
            }

           

            private void pictureBox1_Click(object sender, EventArgs e)
            {

            }
        }

    }


    全国最大的免费电子书下载基地http://www.52eshu.com
    2010年5月18日 15:26

全部回复

  • 1 用Active Desktop在桌面上放一个网页,网页里面可以放ActiveX,不过Active Desktop在现代的Windows操作系统中不适用。

    2 SideBar,不过这是基于HTML的



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    2010年5月17日 21:17
    版主
  • Windows API: SetWindowPos 可以把窗体设置成最底层。

    参考:http://www.pinvoke.net/default.aspx/user32.SetWindowPos

    其实参考中sample没有写到设置窗体为BottomMost的,但你可以试试加上这段代码:

    static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

    SetWindowPos(form.Handle, HWND_BOTTOM, 0, 0, 0, 0, TOPMOST_FLAGS);

    最后,你可以把窗体的FormBorberStyle = None,ShowInTaskbar = False,这样你的程序看起来就像是嵌入在桌面上的了。

    2010年5月18日 1:38
  • 我做了个窗体然后做了鼠标穿透,下面是代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Drawing.Imaging;


    namespace WindowsApplication3

    {

        public partial class Form1 : Form
        {
            


            private const uint WS_EX_LAYERED = 0x80000;
            private const int WS_EX_TRANSPARENT = 0x20;
            private const int GWL_STYLE = (-16);
            private const int GWL_EXSTYLE = (-20);
            private const int LWA_ALPHA = 0x2;

            [DllImport("user32", EntryPoint = "SetWindowLong")]
            private static extern uint SetWindowLong(
            IntPtr hwnd,
            int nIndex,
            uint dwNewLong
            );

            [DllImport("user32", EntryPoint = "GetWindowLong")]
            private static extern uint GetWindowLong(
            IntPtr hwnd,
            int nIndex
            );

            [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
            private static extern int SetLayeredWindowAttributes(
            IntPtr hwnd,
            int crKey,
            int bAlpha,
            int dwFlags
            );

            

            public Form1()
            {
                
                InitializeComponent();
                this.CanPenetrate();
               
            }
              

            private void Form1_Load(object sender, EventArgs e)
            {

              
               button1.Focus();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            public void CanPenetrate()
            {
                uint intExTemp = GetWindowLong(this.Handle, GWL_EXSTYLE);
                uint oldGWLEx = SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
                SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA);
            }

           

            private void pictureBox1_Click(object sender, EventArgs e)
            {

            }
        }

    }


    全国最大的免费电子书下载基地http://www.52eshu.com
    2010年5月18日 15:26