惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Heimdal Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
V
V2EX
WordPress大学
WordPress大学
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
AI
AI
小众软件
小众软件
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
B
Blog RSS Feed
Forbes - Security
Forbes - Security
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Project Zero
Project Zero
H
Hacker News: Front Page
Y
Y Combinator Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 恭喜发财

android 电话状态的监听(来电和去电) (转)Xml DML - 恭喜发财 - 博客园 (转)sql:variable() binding and modify method of xquery: insert XML DML 在WinForm中控制GIF动画的启停的一种方法(转) SQL Server Backup file in standard Zip format Working with binary large objects (BLOBs) c# 得到局域网中可用SqlServer服务器列表(转) C#实现UDP协议(转) 健康大讲堂—凡膳皆药 寓医于食 ReportView(RDSL)参考资料 How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6) 汇总c#.net常用函数和方法集 在多线程中如何调用Winform 使用C#在进度条中显示复制文件的进度(转) c#将大文件读取或写入到数据库(带进度条的源码)(转) 写C#自定义控件的心得 Finalize(析构函数)、Dispose、Close 的区别与使用 如何在WinForm中对DataGrid进行分页显示(转) c#连接各类数据库大全
如何用C#做一个类似于桌面插件的程序(转)
恭喜发财 · 2008-06-17 · via 博客园 - 恭喜发财

最近看到有人问如何做一个“桌面天气秀”类似的软件,开始我以为做这个东西很复杂,因为觉得至少要传递MessageDesktop。但事实上,一试之后发现做这个程序竟然很简单。

以下就把做的步骤列出来,供大家参考。

首先,需要设置窗体样式,这里面要设置的有:

l         设置FormBorderStyleNone

l         设置TopMostfalse

l         设置ShowInTaskbarfalse

l         为了能穿透桌面,要把BackColor设为White,在把TransparentKey设为White

这样,窗体的基本设置就完成了,为了显示要画的内容,则需要在窗体的Paint事件中去做,我这里所画的内容是显示当前月的所有天,相当于一个小日历。

private void Draw( Graphics g )

{

    const float REG_HEIGHT = 30f;

    const float START_POS_X = 0f;

    const float START_POS_Y = 0f;

    const int BLANK_SPACE_NUM = 5;

    // Draw day of week signal

    RectangleF recRegion = new RectangleF( START_POS_X, START_POS_Y,

        (float)(this.Width), REG_HEIGHT );

    const string FORMAT_STRING = "{0}{1}{2}{3}{4}{5}{6}";

    string strDraw = string.Format( FORMAT_STRING,

        "SUN".PadLeft( BLANK_SPACE_NUM, ' ' ),

        "MON".PadLeft( BLANK_SPACE_NUM, ' ' ),

        "TUE".PadLeft( BLANK_SPACE_NUM, ' ' ),

        "WED".PadLeft( BLANK_SPACE_NUM, ' ' ),

        "THU".PadLeft( BLANK_SPACE_NUM, ' ' ),

        "FRI".PadLeft( BLANK_SPACE_NUM, ' ' ),

        "SAT".PadLeft( BLANK_SPACE_NUM, ' ' ) );

    Font myFont = new Font( "宋体", 11, FontStyle.Bold );

    StringFormat sfDraw = new StringFormat();

    sfDraw.Alignment = StringAlignment.Near;

    sfDraw.LineAlignment = StringAlignment.Far;

    Brush brDraw = new SolidBrush( Color.Wheat );

    g.DrawString( strDraw, myFont, brDraw, recRegion, sfDraw );

    DateTime dtFirstDate = DateTime.Now.AddDays( 1 - DateTime.Now.Day );

    int nStartIndex = (int)(dtFirstDate.DayOfWeek);

    nStartIndex++;

    TimeSpan tsDays = dtFirstDate.AddMonths( 1 ) - dtFirstDate;

    // Draw every day in this month

    strDraw = "";

    int i = 0;

    for( ; i < tsDays.Days; i++ )

    {

        switch( dtFirstDate.AddDays( i ).DayOfWeek )

        {

            case DayOfWeek.Sunday:

            case DayOfWeek.Monday:

            case DayOfWeek.Tuesday:

            case DayOfWeek.Wednesday:

            case DayOfWeek.Thursday:

            case DayOfWeek.Friday:

                strDraw += (i+1).ToString().PadLeft( BLANK_SPACE_NUM, ' ');

                break;

            case DayOfWeek.Saturday:

                strDraw += (i+1).ToString().PadLeft( BLANK_SPACE_NUM, ' ');

                strDraw = strDraw.PadLeft( BLANK_SPACE_NUM * 7, ' ' );

                recRegion = new RectangleF( START_POS_X,

                    START_POS_Y + REG_HEIGHT * ( 1 + ( (i+1) + nStartIndex - 7 ) / 7 ),

                    (float)(this.Width), REG_HEIGHT );

                g.DrawString( strDraw, myFont, brDraw, recRegion, sfDraw );

                strDraw = "";

                break;

        }

    }

    if( strDraw != "" )

    {

        strDraw = strDraw.PadRight( BLANK_SPACE_NUM * 7, ' ' );

        recRegion = new RectangleF( START_POS_X,

            START_POS_Y + REG_HEIGHT * ( 1 + ( i + nStartIndex - 7 ) / 7 + 1 ),

            (float)(this.Width), REG_HEIGHT );

        g.DrawString( strDraw, myFont, brDraw, recRegion, sfDraw );

    }

}

private void frmSprite_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

{

    Graphics g = e.Graphics;

    Draw( g );

}

为了窗体能方便的退出,我加了一个NotifyIconContextMenu来处理,具体如下。

private System.Windows.Forms.NotifyIcon ntfSprite;

private System.Windows.Forms.ContextMenu mnuContext;

private System.Windows.Forms.MenuItem mnuExit;

private void mnuExit_Click(object sender, System.EventArgs e)

{

    this.Close();

}

       本来以为自己的窗体放在Desktop之前,会影响窗体下Desktop的某些操作,但事实,这一点根本不用担心,因为.Net框架已经替你做了,因此你不用再调用API来传递消息。

如果想要所显示的窗体能够随意拖动,这可以参看我另外一篇文章:

http://blog.csdn.net/knight94/archive/2006/04/14/663089.aspx

以上的代码只是做了简单的测试,大家可以在我的基础上做扩展,例如调用Api来对当前进程作一些隐藏,设置窗体的起始位置,以及一些界面操作之类,放到启动菜单,这些都是可以完成的,我这里就不罗嗦了