示例#1
0
 public static extern int GetTextExtentPoint32(IntPtr hDC, string String, int stringSize, ref WINAPI.SIZE size);
        /// <summary>
        /// Shows the ballon tooltip
        /// </summary>
        /// <param name="timeout"></param>
        public void ShowBalloonTip(int timeout)
        {

            //Check if a balloon already is shown or no user control was defined
            if (moNativeWindow.Handle != IntPtr.Zero || moPanel == null)
                return;

            //Get a handle for the notify icon
            IntPtr loNotifyIconHandle = GetHandler(moNotifyIcon);

            if (loNotifyIconHandle == IntPtr.Zero)
                return;

            //Trying to find notify icon rectangle on a desktop
            if (!GetNotifyIconScreenRect(loNotifyIconHandle))
                return;

            //Create parameters for a new tooltip window
            System.Windows.Forms.CreateParams moCreateParams = new System.Windows.Forms.CreateParams();

            // New window is a tooltip and a balloon
            moCreateParams.ClassName = WINAPI.TOOLTIPS_CLASS;
            moCreateParams.Style = WINAPI.WS_POPUP | WINAPI.TTS_NOPREFIX | WINAPI.TTS_ALWAYSTIP | WINAPI.TTS_BALLOON;
            moCreateParams.Parent = loNotifyIconHandle;

            // Create the tooltip window
            moNativeWindow.CreateHandle(moCreateParams);

            //We save old window proc to be used later and replac it our own
            IntPtr loNativeProc = WINAPI.SetWindowLong(moNativeWindow.Handle, WINAPI.GWL_WNDPROC, wpcallback);

            if (loNativeProc == IntPtr.Zero)
                return;

            if (WINAPI.SetProp(moNativeWindow.Handle, "NATIVEPROC", loNativeProc) == 0)
                return;

            // Make tooltip  the top level window
            if (!WINAPI.SetWindowPos(moNativeWindow.Handle, WINAPI.HWND_TOPMOST, 0, 0, 0, 0, WINAPI.SWP_NOACTIVATE | WINAPI.SWP_NOSIZE))
                return;

            // Tool tip info. Set a TRACK flag to show it in specified position
            WINAPI.TOOLINFO ti = new WINAPI.TOOLINFO();
            ti.cbSize = Marshal.SizeOf(ti);
            ti.uFlags = WINAPI.TTF_TRACK;
            ti.hwnd = loNotifyIconHandle;


            //Approximating window size by use of char size
            WINAPI.SIZE size = new WINAPI.SIZE();
            IntPtr hDC = WINAPI.GetDC(moNativeWindow.Handle);

            if (hDC == IntPtr.Zero)
                return;

            if (WINAPI.GetTextExtentPoint32(hDC, ".", 1, ref size) == 0)
                return;

            int lines = 1 + moPanel.Height / size.cy;
            int cols = moPanel.Width / size.cx;

            ti.lpszText = ".";
            for (int i = 0; i < lines; i++)
            {
                for (int j = 0; j < cols; j++)
                    ti.lpszText += ".";
                ti.lpszText += "\r\n";
            }


            //Add tool to the notify icon
            if (WINAPI.SendMessage(moNativeWindow.Handle, WINAPI.TTM_ADDTOOL, 0, ref ti) == 0)
                return;

            WINAPI.SendMessage(moNativeWindow.Handle, WINAPI.TTM_SETTIPBKCOLOR,
                ColorTranslator.ToWin32(Color.LightYellow), ref ti);

            WINAPI.SendMessage(moNativeWindow.Handle, WINAPI.TTM_SETTIPTEXTCOLOR,
                ColorTranslator.ToWin32(Color.Black), ref ti);

            WINAPI.SendMessage(moNativeWindow.Handle, WINAPI.TTM_SETDELAYTIME, Duration.AutoPop, 1000);
            WINAPI.SendMessage(moNativeWindow.Handle, WINAPI.TTM_TRACKPOSITION, 0, (moPosition.Y << 16) + moPosition.X);
            WINAPI.SendMessage(moNativeWindow.Handle, WINAPI.TTM_SETTITLE, (int)BalloonTipIcon, BalloonTipTitle);
            WINAPI.SendMessage(moNativeWindow.Handle, WINAPI.TTM_TRACKACTIVATE, 1, ref ti);

            // Raise shown event
            OnBalloonTipShown(this, EventArgs.Empty);

            // Timeout for the tooltip
            moTimer.Interval = timeout;
            moTimer.Start();
        }