/// <summary> /// Calls API <msdn>CalculatePopupWindowPosition</msdn> and sets <b>Bounds</b>. /// The window by default will be at the right or left side of <i>anchorRect</i>. Use <b>PopupAlignment.TPM_VERTICAL</b> if need below or above. /// </summary> public void ZCalculateAndSetPosition(Rectangle anchorRect, PopupAlignment align, Size?size = null) { int x = align.HasAny(PopupAlignment.TPM_VERTICAL | PopupAlignment.TPM_RIGHTALIGN) ? anchorRect.Left : anchorRect.Right; int y = align.Has(PopupAlignment.TPM_VERTICAL) && !align.Has(PopupAlignment.TPM_BOTTOMALIGN) ? anchorRect.Bottom : anchorRect.Top; ZCalculateAndSetPosition(x, y, align, anchorRect, size); }
public void Show(Control parent, PopupAlignment alignment, string message, string title = "", PopupType type = PopupType.Info, int timeout = 0, int padding = 10) { // No message means kill current pop-up if (message == "") { Hide(); return; } _parent = parent; _alignment = alignment; _padding = padding; // Reset sizes Size = MinimumSize; MaximumSize = new Size((int)(parent.Size.Width * 0.7f), parent.Size.Height / 4); // Hide header if not specified bool titleIsVisible = (type == PopupType.None || title != string.Empty); if (titleIsVisible) { panelTitle.Visible = true; lblTitle.Text = title; // Setup header type switch (type) { default: case PopupType.Info: lblTitle.ForeColor = _infoColor; break; case PopupType.Warning: lblTitle.ForeColor = _warningColor; break; case PopupType.Error: lblTitle.ForeColor = _errorColor; break; } } else { panelTitle.Visible = false; } // Measure text dimensions SizeF size = TextRenderer.MeasureText(message, lblMessage.Font, new Size(MaximumSize.Width - panelMessage.Padding.Horizontal, MaximumSize.Height - panelMessage.Padding.Vertical - (titleIsVisible ? panelTitle.Height : 0)), TextFormatFlags.WordBreak); lblMessage.Text = message; Width = (int)Math.Ceiling(size.Width) + panelMessage.Padding.Horizontal + 1; Height = (int)Math.Ceiling(size.Height) + (titleIsVisible ? panelTitle.Height : 0) + panelMessage.Padding.Vertical + 1; // Setup message timeout if (timeout > 0) { _animTimeout = timeout * 10.0f; } else { // Default timeout is based on average reading speed. int wordCount = 0, index = 0; while (index < message.Length) { while (index < message.Length && !char.IsWhiteSpace(message[index])) { index++; } wordCount++; while (index < message.Length && char.IsWhiteSpace(message[index])) { index++; } } _animTimeout = wordCount * _avgReadSpeed; } // Start intro animation _animProgress = 0.0f; _animTimer.Start(); // Clear opacity to prevent flicker Opacity = 0.0f; if (!Visible) { Show(parent); } }
/// <summary> /// Calls API <msdn>CalculatePopupWindowPosition</msdn> and sets <b>Bounds</b>. /// </summary> public void ZCalculateAndSetPosition(int x, int y, PopupAlignment align, Rectangle excludeRect = default, Size?size = null) { Api.CalculatePopupWindowPosition((x, y), size ?? this.Size, (uint)align, excludeRect, out var r); Bounds = r; }