首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows 7左键单击时钟弹出窗口

Windows 7左键单击时钟弹出窗口
EN

Stack Overflow用户
提问于 2010-07-29 02:50:34
回答 1查看 1.4K关注 0票数 3

当在Windows7(可能也是Vista )的任务栏中单击时钟时,会打开一个弹出窗口,显示日历和时钟(因此不是日期和时间调整窗口)。如何自己打开此窗口(在C#中首选)?

我希望timedate.cpl会调用它,但这会打开日期和时间调整窗口。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-29 04:03:58

要显示时钟,您需要将相应的窗口消息发送到托盘窗口。这可以使用Windows API函数SendMessage来完成

代码语言:javascript
复制
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;

class ShowCalendar
{
    private delegate bool EnumChildCallback(IntPtr hwnd, 
            ref IntPtr lParam);

    [DllImport("User32.dll")]
    private static extern bool EnumChildWindows(IntPtr hWndParent, 
            EnumChildCallback lpEnumFunc, 
            ref IntPtr lParam);

    [DllImport("User32.dll")]
    private static extern int GetClassName(IntPtr hWnd, 
        StringBuilder lpClassName, 
        int nMaxCount);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, 
        UInt32 Msg, 
        IntPtr wParam, 
        IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, 
        string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, 
        IntPtr hwndChildAfter, 
        string lpszClass, 
        string lpszWindow);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetWindowRect(IntPtr hWnd, 
            out RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int Left;        
        public int Top;         
        public int Right;       
        public int Bottom;      
    }

    private static readonly string TrayWndClassName = "Shell_TrayWnd";
    private static readonly string TrayNotifyWndClassName = "TrayNotifyWnd";
    private static readonly string ClockWndClassName = "TrayClockWClass";
    private static readonly uint WM_NCLBUTTONDOWN = 0x00A1;
    private static readonly uint HTCAPTION = 2;

    private static bool EnumChildProc(IntPtr hwndChild, ref IntPtr lParam)
    {
        StringBuilder className = new StringBuilder(128);
        GetClassName(hwndChild, className, 128);

        if (className.ToString() == ClockWndClassName)
        {
            lParam = hwndChild;
            return false;
        }
        return true;
    }


    static void Main(string[] args)
    {
        IntPtr hWndTray = FindWindow(TrayWndClassName, string.Empty);
        if (hWndTray == IntPtr.Zero)
        {
            throw new Win32Exception();
        }

        IntPtr hWndTrayNotify = FindWindowEx(hWndTray, 
            IntPtr.Zero, 
            TrayNotifyWndClassName, 
            string.Empty);
        if (hWndTrayNotify == IntPtr.Zero)
        {
            throw new Win32Exception();
        }

        // search clock window
        EnumChildCallback cb = new EnumChildCallback(EnumChildProc);
        IntPtr hWndClock = IntPtr.Zero;
        EnumChildWindows(hWndTray, cb, ref hWndClock);
        if (hWndClock == IntPtr.Zero)
        {
            throw new Win32Exception();
        }

        // get clock window position
        RECT rect;
        if (!GetWindowRect(hWndClock, out rect))
        {
            throw new Win32Exception();
        }

        // send click, lParam contains window position
        IntPtr wParam = new IntPtr(HTCAPTION);
        IntPtr lParam = new IntPtr(rect.Top << 16 | rect.Left);
        SendMessage(hWndTray, WM_NCLBUTTONDOWN, wParam, lParam);
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3356309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档