private void DrawObjects(Graphics g, List <Map.Object> objects, bool shadow = false) { Pen pen; PointF center, ptf, topLeft; ThingDb.Thing tt; Map.Object underCursor = null; if (MapInterface.CurrentMode == EditMode.OBJECT_SELECT || MainWindow.Instance.mapView.picking) { underCursor = MainWindow.Instance.mapView.GetObjectUnderCursor(); } if (mapRenderer.proDefault && underCursor == null) { MainWindow.Instance.mapView.mapPanel.Cursor = Cursors.Default; } bool drawExtents3D = EditorSettings.Default.Draw_Extents_3D; if (EditorSettings.Default.Draw_Objects) { foreach (Map.Object oe in objects) { var customRender = oe as CustomRenderObject; if (customRender != null) { customRender.Render(g); continue; } ptf = oe.Location; float x = ptf.X, y = ptf.Y; tt = ThingDb.Things[oe.Name]; center = new PointF(x, y); topLeft = new PointF(center.X - objectSelectionRadius, center.Y - objectSelectionRadius); pen = mapRenderer.ColorLayout.Objects; // Object facing helper if (EditorSettings.Default.Draw_ObjectFacing) { float deg = -1F; if (tt.Xfer == "MonsterXfer") { MonsterXfer xfer = oe.GetExtraData <MonsterXfer>(); deg = (float)MonsterXfer.NOX_DIRECT_VALS[xfer.DirectionId] / 256F * 360F; } if (tt.Xfer == "NPCXfer") { NPCXfer xfer = oe.GetExtraData <NPCXfer>(); deg = (float)MonsterXfer.NOX_DIRECT_VALS[xfer.DirectionId] / 256F * 360F; } if (deg >= 0F) { using (var m = new System.Drawing.Drawing2D.Matrix()) { m.RotateAt(deg, center); g.Transform = m; g.DrawLine(objMoveablePen, center.X, center.Y, center.X + 20, center.Y); g.ResetTransform(); } } // Sentry ray if (tt.Xfer == "SentryXfer") { SentryXfer sentry = oe.GetExtraData <SentryXfer>(); float targX = x + ((float)Math.Cos(sentry.BasePosRadian) * 80F); float targY = y + ((float)Math.Sin(sentry.BasePosRadian) * 80F); // show sentry ray direction g.DrawLine(sentryPen, x, y, targX, targY); } } // invisible triggers and pressure plates if (tt.DrawType == "TriggerDraw" || tt.DrawType == "PressurePlateDraw") { if (shadow) { DrawObjectExtent(g, oe, drawExtents3D); } //else // DrawTriggerExtent(g, oe, underCursor); continue; } // black powder if (EditorSettings.Default.Edit_PreviewMode) // Visual Preview { if (tt.DrawType == "BlackPowderDraw") { /* Rectangle bp = new Rectangle((int)x - 2, (int)y - 2, 4, 4); * g.FillRectangle(new SolidBrush(Color.Gray), bp);*/ continue; } if (tt.Xfer == "InvisibleLightXfer") { /*bool isUnderCursor = false; * * Pen Penlight = new Pen(Color.Yellow, 1); * if (underCursor != null) isUnderCursor = underCursor.Equals(oe); * * if (isUnderCursor) * Penlight = new Pen(Color.Orange, 1); * * bool isSelected = mapRenderer.SelectedObjects.Items.Contains(oe); * bool isSelected2 = MapInterface.RecSelected.Contains(oe); * if (isSelected && isSelected2 && MapInterface.KeyHelper.ShiftKey) * { * isSelected = false; * isSelected2 = false; * } * * * if (isSelected || isSelected2) * { * Penlight = Pens.DarkOrange; * if (mapRenderer.proHand && isUnderCursor) * MainWindow.Instance.mapView.mapPanel.Cursor = Cursors.Hand; * * } * * g.DrawEllipse(Penlight, new RectangleF(center.X - 9, center.Y - 9, 18, 18)); * g.DrawEllipse(Penlight, new RectangleF(center.X - 13, center.Y - 13, 26, 26)); * g.DrawEllipse(Penlight, new RectangleF(center.X - 17, center.Y - 17, 34, 34)); */ continue; } Bitmap image = GetObjectImage(oe, shadow); /*if (tt.Name.StartsWith("Amb") && image == null) * { * image = mapRenderer.VideoBag.GetBitmap(tt.SpriteMenuIcon); * drawOffsetX = 82; * drawOffsetY = 122; * }*/ if (image == null || tt.DrawType == "NoDraw") { // in case of failure draw only the extent //DrawObjectExtent(g, oe, drawExtents3D); } else { int sizeX = tt.SizeX / 2; int sizeY = tt.SizeY / 2; x -= (sizeX - drawOffsetX); y -= (sizeY - drawOffsetY) + tt.Z; // no blurring int ix = Convert.ToInt32(x); int iy = Convert.ToInt32(y); // recolor in case it is being selected bool isSelected = mapRenderer.SelectedObjects.Items.Contains(oe); bool isSelected2 = MapInterface.RecSelected.Contains(oe); if (isSelected && isSelected2 && MapInterface.KeyHelper.ShiftKey) { isSelected = false; isSelected2 = false; } bool isUnderCursor = false; if (underCursor != null) { isUnderCursor = underCursor.Equals(oe); } // draw the image if (isSelected || isUnderCursor || shadow || isSelected2) { // highlight selection var shader = new BitmapShader(image); shader.LockBitmap(); var hltColor = mapRenderer.ColorLayout.Selection; if (isSelected || isSelected2) { if (mapRenderer.proHand && isUnderCursor) { MainWindow.Instance.mapView.mapPanel.Cursor = Cursors.Hand; } shader.ColorShade(hltColor, 0.5F); } else if (isUnderCursor) { hltColor = Color.PaleGreen; if (MapInterface.CurrentMode == EditMode.OBJECT_PLACE && !MainWindow.Instance.mapView.picking) { hltColor = mapRenderer.ColorLayout.Removing; } shader.ColorGradWaves(hltColor, 7F, Environment.TickCount); } if (shadow) { shader.MakeSemitransparent(165); } image = shader.UnlockBitmap(); g.DrawImage(image, ix, iy, image.Width, image.Height); image.Dispose(); } else { g.DrawImage(image, ix, iy, image.Width, image.Height); } } } else { if (mapRenderer.SelectedObjects.Items.Contains(oe)) { pen = Pens.Green; } if (MapInterface.RecSelected.Contains(oe)) { pen = Pens.Green; } if ((mapRenderer.SelectedObjects.Items.Contains(oe) && MapInterface.RecSelected.Contains(oe))) { pen = mapRenderer.ColorLayout.Objects; } g.DrawEllipse(pen, new RectangleF(topLeft, new Size(2 * objectSelectionRadius, 2 * objectSelectionRadius)));//55 // If is a door if ((tt.Class & ThingDb.Thing.ClassFlags.DOOR) == ThingDb.Thing.ClassFlags.DOOR) { DoorXfer door = oe.GetExtraData <DoorXfer>(); if (door.Direction == DoorXfer.DOORS_DIR.South) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y - 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X - 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X - 20, (int)center.Y - 20), new Point((int)center.X - 20, (int)center.Y - 60)); } } else if (door.Direction == DoorXfer.DOORS_DIR.West) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y - 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X + 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y - 60)); g.DrawLine(doorPen, new Point((int)center.X + 20, (int)center.Y - 20), new Point((int)center.X + 20, (int)center.Y - 60)); } } else if (door.Direction == DoorXfer.DOORS_DIR.North) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y + 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X + 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X + 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X + 20, (int)center.Y + 20), new Point((int)center.X + 20, (int)center.Y - 20)); } } else if (door.Direction == DoorXfer.DOORS_DIR.East) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y + 20)); if (drawExtents3D) { g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y - 40), new Point((int)center.X - 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X, (int)center.Y), new Point((int)center.X - 20, (int)center.Y - 20)); g.DrawLine(doorPen, new Point((int)center.X - 20, (int)center.Y + 20), new Point((int)center.X - 20, (int)center.Y - 20)); } } } } if (EditorSettings.Default.Draw_Extents) { if (!(!EditorSettings.Default.Draw_AllExtents && oe.HasFlag(ThingDb.Thing.FlagsFlags.NO_COLLIDE))) { DrawObjectExtent(g, oe, drawExtents3D); } } } // Draw labels on the separate cycle to prevent layer glitching if (EditorSettings.Default.Draw_AllText) { foreach (Map.Object oe in objects) { Point textLocaton = new Point(Convert.ToInt32(oe.Location.X), Convert.ToInt32(oe.Location.Y)); textLocaton.X -= objectSelectionRadius; textLocaton.Y -= objectSelectionRadius; if (EditorSettings.Default.Draw_ObjCustomLabels && oe.Team > 0) { // Draw team Point loc = new Point(textLocaton.X, textLocaton.Y - 12); TextRenderer.DrawText(g, String.Format(FORMAT_OBJECT_TEAM, oe.Team), objectExtentFont, loc, Color.LightPink); } if (EditorSettings.Default.Draw_ObjCustomLabels && oe.Scr_Name.Length > 0) { // Draw custom label Size size = TextRenderer.MeasureText(oe.ScrNameShort, objectExtentFont); textLocaton.X -= size.Width / 3; TextRenderer.DrawText(g, oe.ScrNameShort, objectExtentFont, textLocaton, Color.Cyan); } else if (EditorSettings.Default.Draw_ObjThingNames) { // Draw thing name Size size = TextRenderer.MeasureText(oe.Name, objectExtentFont); textLocaton.X -= size.Width / 3; TextRenderer.DrawText(g, oe.Name, objectExtentFont, textLocaton, Color.Green); } else if (!EditorSettings.Default.Edit_PreviewMode && oe.Extent >= 0 && !(EditorSettings.Default.Draw_Extents || EditorSettings.Default.Draw_AllExtents)) { TextRenderer.DrawText(g, oe.Extent.ToString(), objectExtentFont, textLocaton, Color.Purple); } } } } }
/// <summary> /// Initializes a new instance of the <see cref='System.Windows.Forms.ThreadExceptionDialog'/> class. /// </summary> public ThreadExceptionDialog(Exception t) { if (DpiHelper.IsScalingRequirementMet) { scaledMaxWidth = LogicalToDeviceUnits(MAXWIDTH); scaledMaxHeight = LogicalToDeviceUnits(MAXHEIGHT); scaledPaddingWidth = LogicalToDeviceUnits(PADDINGWIDTH); scaledPaddingHeight = LogicalToDeviceUnits(PADDINGHEIGHT); scaledMaxTextWidth = LogicalToDeviceUnits(MAXTEXTWIDTH); scaledMaxTextHeight = LogicalToDeviceUnits(MAXTEXTHEIGHT); scaledButtonTopPadding = LogicalToDeviceUnits(BUTTONTOPPADDING); scaledButtonDetailsLeftPadding = LogicalToDeviceUnits(BUTTONDETAILS_LEFTPADDING); scaledMessageTopPadding = LogicalToDeviceUnits(MESSAGE_TOPPADDING); scaledHeightPadding = LogicalToDeviceUnits(HEIGHTPADDING); scaledButtonWidth = LogicalToDeviceUnits(BUTTONWIDTH); scaledButtonHeight = LogicalToDeviceUnits(BUTTONHEIGHT); scaledButtonAlignmentWidth = LogicalToDeviceUnits(BUTTONALIGNMENTWIDTH); scaledButtonAlignmentPadding = LogicalToDeviceUnits(BUTTONALIGNMENTPADDING); scaledDetailsWidthPadding = LogicalToDeviceUnits(DETAILSWIDTHPADDING); scaledDetailsHeight = LogicalToDeviceUnits(DETAILSHEIGHT); scaledPictureWidth = LogicalToDeviceUnits(PICTUREWIDTH); scaledPictureHeight = LogicalToDeviceUnits(PICTUREHEIGHT); scaledExceptionMessageVerticalPadding = LogicalToDeviceUnits(EXCEPTIONMESSAGEVERTICALPADDING); } string messageFormat; string messageText; Button[] buttons; bool detailAnchor = false; WarningException w = t as WarningException; if (w != null) { messageFormat = SR.ExDlgWarningText; messageText = w.Message; if (w.HelpUrl == null) { buttons = new Button[] { continueButton }; } else { buttons = new Button[] { continueButton, helpButton }; } } else { messageText = t.Message; detailAnchor = true; if (Application.AllowQuit) { if (t is System.Security.SecurityException) { messageFormat = SR.ExDlgSecurityErrorText; } else { messageFormat = SR.ExDlgErrorText; } buttons = new Button[] { detailsButton, continueButton, quitButton }; } else { if (t is System.Security.SecurityException) { messageFormat = SR.ExDlgSecurityContinueErrorText; } else { messageFormat = SR.ExDlgContinueErrorText; } buttons = new Button[] { detailsButton, continueButton }; } } if (messageText.Length == 0) { messageText = t.GetType().Name; } if (t is System.Security.SecurityException) { messageText = string.Format(messageFormat, t.GetType().Name, Trim(messageText)); } else { messageText = string.Format(messageFormat, Trim(messageText)); } StringBuilder detailsTextBuilder = new StringBuilder(); string newline = "\r\n"; string separator = SR.ExDlgMsgSeperator; string sectionseparator = SR.ExDlgMsgSectionSeperator; if (Application.CustomThreadExceptionHandlerAttached) { detailsTextBuilder.Append(SR.ExDlgMsgHeaderNonSwitchable); } else { detailsTextBuilder.Append(SR.ExDlgMsgHeaderSwitchable); } detailsTextBuilder.Append(string.Format(CultureInfo.CurrentCulture, sectionseparator, SR.ExDlgMsgExceptionSection)); detailsTextBuilder.Append(t.ToString()); detailsTextBuilder.Append(newline); detailsTextBuilder.Append(newline); detailsTextBuilder.Append(string.Format(CultureInfo.CurrentCulture, sectionseparator, SR.ExDlgMsgLoadedAssembliesSection)); foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { AssemblyName name = asm.GetName(); string fileVer = SR.NotAvailable; try { if (name.EscapedCodeBase != null && name.EscapedCodeBase.Length > 0) { Uri codeBase = new Uri(name.EscapedCodeBase); if (codeBase.Scheme == "file") { fileVer = FileVersionInfo.GetVersionInfo(NativeMethods.GetLocalPath(name.EscapedCodeBase)).FileVersion; } } } catch (System.IO.FileNotFoundException) { } detailsTextBuilder.Append(string.Format(SR.ExDlgMsgLoadedAssembliesEntry, name.Name, name.Version, fileVer, name.EscapedCodeBase)); detailsTextBuilder.Append(separator); } detailsTextBuilder.Append(string.Format(CultureInfo.CurrentCulture, sectionseparator, SR.ExDlgMsgJITDebuggingSection)); if (Application.CustomThreadExceptionHandlerAttached) { detailsTextBuilder.Append(SR.ExDlgMsgFooterNonSwitchable); } else { detailsTextBuilder.Append(SR.ExDlgMsgFooterSwitchable); } detailsTextBuilder.Append(newline); detailsTextBuilder.Append(newline); string detailsText = detailsTextBuilder.ToString(); Graphics g = message.CreateGraphicsInternal(); Size textSize = new Size(scaledMaxWidth - scaledPaddingWidth, int.MaxValue); if (DpiHelper.IsScalingRequirementMet && Label.UseCompatibleTextRenderingDefault == false) { // we need to measure string using API that matches the rendering engine - TextRenderer.MeasureText for GDI textSize = Size.Ceiling(TextRenderer.MeasureText(messageText, Font, textSize, TextFormatFlags.WordBreak)); } else { // if HighDpi improvements are not enabled, or rendering mode is GDI+, use Graphics.MeasureString textSize = Size.Ceiling(g.MeasureString(messageText, Font, textSize.Width)); } textSize.Height += scaledExceptionMessageVerticalPadding; g.Dispose(); if (textSize.Width < scaledMaxTextWidth) { textSize.Width = scaledMaxTextWidth; } if (textSize.Height > scaledMaxHeight) { textSize.Height = scaledMaxHeight; } int width = textSize.Width + scaledPaddingWidth; int buttonTop = Math.Max(textSize.Height, scaledMaxTextHeight) + scaledPaddingHeight; Form activeForm = Form.ActiveForm; if (activeForm == null || activeForm.Text.Length == 0) { Text = SR.ExDlgCaption; } else { Text = string.Format(SR.ExDlgCaption2, activeForm.Text); } AcceptButton = continueButton; CancelButton = continueButton; FormBorderStyle = FormBorderStyle.FixedDialog; MaximizeBox = false; MinimizeBox = false; StartPosition = FormStartPosition.CenterScreen; Icon = null; ClientSize = new Size(width, buttonTop + scaledButtonTopPadding); TopMost = true; pictureBox.Location = new Point(scaledPictureWidth / 8, scaledPictureHeight / 8); pictureBox.Size = new Size(scaledPictureWidth * 3 / 4, scaledPictureHeight * 3 / 4); pictureBox.SizeMode = PictureBoxSizeMode.StretchImage; if (t is System.Security.SecurityException) { pictureBox.Image = SystemIcons.Information.ToBitmap(); } else { pictureBox.Image = SystemIcons.Error.ToBitmap(); } Controls.Add(pictureBox); message.SetBounds(scaledPictureWidth, scaledMessageTopPadding + (scaledMaxTextHeight - Math.Min(textSize.Height, scaledMaxTextHeight)) / 2, textSize.Width, textSize.Height); message.Text = messageText; Controls.Add(message); continueButton.Text = SR.ExDlgContinue; continueButton.FlatStyle = FlatStyle.Standard; continueButton.DialogResult = DialogResult.Cancel; quitButton.Text = SR.ExDlgQuit; quitButton.FlatStyle = FlatStyle.Standard; quitButton.DialogResult = DialogResult.Abort; helpButton.Text = SR.ExDlgHelp; helpButton.FlatStyle = FlatStyle.Standard; helpButton.DialogResult = DialogResult.Yes; detailsButton.Text = SR.ExDlgShowDetails; detailsButton.FlatStyle = FlatStyle.Standard; detailsButton.Click += new EventHandler(DetailsClick); Button b = null; int startIndex = 0; if (detailAnchor) { b = detailsButton; expandImage = DpiHelper.GetBitmapFromIcon(GetType(), DownBitmapName); collapseImage = DpiHelper.GetBitmapFromIcon(GetType(), UpBitmapName); if (DpiHelper.IsScalingRequirementMet) { ScaleBitmapLogicalToDevice(ref expandImage); ScaleBitmapLogicalToDevice(ref collapseImage); } b.SetBounds(scaledButtonDetailsLeftPadding, buttonTop, scaledButtonWidth, scaledButtonHeight); b.Image = expandImage; b.ImageAlign = ContentAlignment.MiddleLeft; Controls.Add(b); startIndex = 1; } int buttonLeft = (width - scaledButtonDetailsLeftPadding - ((buttons.Length - startIndex) * scaledButtonAlignmentWidth - scaledButtonAlignmentPadding)); for (int i = startIndex; i < buttons.Length; i++) { b = buttons[i]; b.SetBounds(buttonLeft, buttonTop, scaledButtonWidth, scaledButtonHeight); Controls.Add(b); buttonLeft += scaledButtonAlignmentWidth; } details.Text = detailsText; details.ScrollBars = ScrollBars.Both; details.Multiline = true; details.ReadOnly = true; details.WordWrap = false; details.TabStop = false; details.AcceptsReturn = false; details.SetBounds(scaledButtonDetailsLeftPadding, buttonTop + scaledButtonTopPadding, width - scaledDetailsWidthPadding, scaledDetailsHeight); details.Visible = detailsVisible; Controls.Add(details); if (DpiHelper.IsScalingRequirementMet) { DpiChanged += ThreadExceptionDialog_DpiChanged; } }
[SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters")] // using "\t" to figure out the width of tab private void CalculateInternalLayoutMetrics() { Size maxTextSize = Size.Empty; Size maxImageSize = Size.Empty; Size maxCheckSize = scaledDefaultImageSize; Size maxArrowSize = Size.Empty; Size maxNonMenuItemSize = Size.Empty; // determine Text Metrics for (int i = 0; i < Items.Count; i++) { ToolStripItem item = Items[i]; ToolStripMenuItem menuItem = item as ToolStripMenuItem; if (menuItem != null) { Size menuItemTextSize = menuItem.GetTextSize(); if (menuItem.ShowShortcutKeys) { Size shortcutTextSize = menuItem.GetShortcutTextSize(); if (tabWidth == -1) { tabWidth = TextRenderer.MeasureText("\t", this.Font).Width; } menuItemTextSize.Width += tabWidth + shortcutTextSize.Width; menuItemTextSize.Height = Math.Max(menuItemTextSize.Height, shortcutTextSize.Height); } // we truly only care about the maximum size we find. maxTextSize.Width = Math.Max(maxTextSize.Width, menuItemTextSize.Width); maxTextSize.Height = Math.Max(maxTextSize.Height, menuItemTextSize.Height); // determine Image Metrics Size imageSize = Size.Empty; if (menuItem.Image != null) { imageSize = (menuItem.ImageScaling == ToolStripItemImageScaling.SizeToFit) ? ImageScalingSize : menuItem.Image.Size; } maxImageSize.Width = Math.Max(maxImageSize.Width, imageSize.Width); maxImageSize.Height = Math.Max(maxImageSize.Height, imageSize.Height); if (menuItem.CheckedImage != null) { Size checkedImageSize = menuItem.CheckedImage.Size; maxCheckSize.Width = Math.Max(checkedImageSize.Width, maxCheckSize.Width); maxCheckSize.Height = Math.Max(checkedImageSize.Height, maxCheckSize.Height); } } else if (!(item is ToolStripSeparator)) { maxNonMenuItemSize.Height = Math.Max(item.Bounds.Height, maxNonMenuItemSize.Height); maxNonMenuItemSize.Width = Math.Max(item.Bounds.Width, maxNonMenuItemSize.Width); } } this.maxItemSize.Height = Math.Max(maxTextSize.Height + scaledTextPadding.Vertical, Math.Max(maxCheckSize.Height + scaledCheckPadding.Vertical, maxArrowSize.Height + scaledArrowPadding.Vertical)); if (ShowImageMargin) { // only add in the image into the calculation if we're going to render it. this.maxItemSize.Height = Math.Max(maxImageSize.Height + scaledImagePadding.Vertical, maxItemSize.Height); } bool useDefaultCheckMarginWidth = (ShowCheckMargin && (maxCheckSize.Width == 0)); bool useDefaultImageMarginWidth = (ShowImageMargin && (maxImageSize.Width == 0)); // Always save space for an arrow maxArrowSize = new Size(scaledArrowSize, maxItemSize.Height); maxTextSize.Height = maxItemSize.Height - scaledTextPadding.Vertical; maxImageSize.Height = maxItemSize.Height - scaledImagePadding.Vertical; maxCheckSize.Height = maxItemSize.Height - scaledCheckPadding.Vertical; // fixup if there are non-menu items that are larger than our normal menu items maxTextSize.Width = Math.Max(maxTextSize.Width, maxNonMenuItemSize.Width); Point nextPoint = Point.Empty; int checkAndImageMarginWidth = 0; int extraImageWidth = Math.Max(0, maxImageSize.Width - scaledDefaultImageSize.Width); if (ShowCheckMargin && ShowImageMargin) { // double column - check margin then image margin // default to 46px - grow if necessary. checkAndImageMarginWidth = scaledDefaultImageAndCheckMarginWidth; // add in the extra space for the image... since the check size is locked down to 16x16. checkAndImageMarginWidth += extraImageWidth; // align the checkmark nextPoint = new Point(scaledCheckPadding.Left, scaledCheckPadding.Top); checkRectangle = LayoutUtils.Align(maxCheckSize, new Rectangle(nextPoint.X, nextPoint.Y, maxCheckSize.Width, maxItemSize.Height), ContentAlignment.MiddleCenter); // align the image rectangle nextPoint.X = checkRectangle.Right + scaledCheckPadding.Right + scaledImagePadding.Left; nextPoint.Y = scaledImagePadding.Top; imageRectangle = LayoutUtils.Align(maxImageSize, new Rectangle(nextPoint.X, nextPoint.Y, maxImageSize.Width, maxItemSize.Height), ContentAlignment.MiddleCenter); } else if (ShowCheckMargin) { // no images should be shown in a ShowCheckMargin only scenario. // default to 24px - grow if necessary. checkAndImageMarginWidth = scaledDefaultImageMarginWidth; // align the checkmark nextPoint = new Point(1, scaledCheckPadding.Top); // nextPoint = new Point(scaledCheckPadding.Left, scaledCheckPadding.Top); checkRectangle = LayoutUtils.Align(maxCheckSize, new Rectangle(nextPoint.X, nextPoint.Y, checkAndImageMarginWidth, maxItemSize.Height), ContentAlignment.MiddleCenter); imageRectangle = Rectangle.Empty; } else if (ShowImageMargin) { // checks and images render in the same area. // default to 24px - grow if necessary. checkAndImageMarginWidth = scaledDefaultImageMarginWidth; // add in the extra space for the image... since the check size is locked down to 16x16. checkAndImageMarginWidth += extraImageWidth; // NOTE due to the Padding property, we're going to have to recalc the vertical alignment in ToolStripMenuItemInternalLayout. // Dont fuss here over the Y, X is what's critical. // check and image rect are the same - take the max of the image size and the check size and align nextPoint = new Point(1, scaledCheckPadding.Top); checkRectangle = LayoutUtils.Align(LayoutUtils.UnionSizes(maxCheckSize, maxImageSize), new Rectangle(nextPoint.X, nextPoint.Y, checkAndImageMarginWidth - 1, maxItemSize.Height), ContentAlignment.MiddleCenter); // align the image imageRectangle = checkRectangle; } else { checkAndImageMarginWidth = 0; } nextPoint.X = checkAndImageMarginWidth + 1; // calculate space for image // if we didnt have a check - make sure to ignore check padding // consider: should we constrain to a reasonable width? //imageMarginBounds = new Rectangle(0, 0, Math.Max(imageMarginWidth,DefaultImageMarginWidth), this.Height); imageMarginBounds = new Rectangle(0, 0, checkAndImageMarginWidth, this.Height); // calculate space for shortcut and text nextPoint.X = imageMarginBounds.Right + scaledTextPadding.Left; nextPoint.Y = scaledTextPadding.Top; textRectangle = new Rectangle(nextPoint, maxTextSize); // calculate space for arrow nextPoint.X = textRectangle.Right + scaledTextPadding.Right + scaledArrowPadding.Left; nextPoint.Y = scaledArrowPadding.Top; arrowRectangle = new Rectangle(nextPoint, maxArrowSize); // calculate space required for all of these pieces this.maxItemSize.Width = (arrowRectangle.Right + scaledArrowPadding.Right) - imageMarginBounds.Left; this.Padding = DefaultPadding; int trimPadding = imageMarginBounds.Width; if (RightToLeft == RightToLeft.Yes) { // reverse the rectangle alignment in RightToLeft.Yes trimPadding += scaledTextPadding.Right; int width = maxItemSize.Width; checkRectangle.X = width - checkRectangle.Right; imageRectangle.X = width - imageRectangle.Right; textRectangle.X = width - textRectangle.Right; arrowRectangle.X = width - arrowRectangle.Right; imageMarginBounds.X = width - imageMarginBounds.Right; } else { trimPadding += scaledTextPadding.Left; } // We need to make sure that the text really appears vertically centered - this can be a problem in // systems which force the text rectangle to be odd. // force this to be an even height. this.maxItemSize.Height += this.maxItemSize.Height % 2; textRectangle.Y = LayoutUtils.VAlign(textRectangle.Size, new Rectangle(Point.Empty, maxItemSize), ContentAlignment.MiddleCenter).Y; textRectangle.Y += (textRectangle.Height % 2); // if the height is odd, push down by one px state[stateMaxItemSizeValid] = true; this.PaddingToTrim = trimPadding; }
/// <summary> /// Measures the size of the button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <returns></returns> public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e) { if (!Visible && !Owner.IsDesignMode()) { SetLastMeasuredSize(new Size(0, 0)); return(LastMeasuredSize); } RibbonElementSizeMode theSize = GetNearestSize(e.SizeMode); int widthSum = Owner.ItemMargin.Horizontal; int heightSum = Owner.ItemMargin.Vertical; int largeHeight = OwnerPanel == null ? 0 : OwnerPanel.ContentBounds.Height - Owner.ItemPadding.Vertical;// -Owner.ItemMargin.Vertical; //58; Size simg = SmallImage != null ? SmallImage.Size : Size.Empty; Size img = Image != null ? Image.Size : Size.Empty; Size sz = Size.Empty; switch (theSize) { case RibbonElementSizeMode.Large: case RibbonElementSizeMode.Overflow: sz = MeasureStringLargeSize(e.Graphics, Text, Owner.Font); if (!string.IsNullOrEmpty(Text)) { widthSum += Math.Max(sz.Width + 1, img.Width); //Got off the patch site from logicalerror //heightSum = largeHeight; heightSum = Math.Max(0, largeHeight); } else { widthSum += img.Width; heightSum += img.Height; } break; case RibbonElementSizeMode.DropDown: sz = TextRenderer.MeasureText(Text, Owner.Font); if (!string.IsNullOrEmpty(Text)) { widthSum += sz.Width + 1; } widthSum += simg.Width + Owner.ItemMargin.Horizontal; heightSum += Math.Max(sz.Height, simg.Height); heightSum += 2; break; case RibbonElementSizeMode.Medium: sz = TextRenderer.MeasureText(Text, Owner.Font); if (!string.IsNullOrEmpty(Text)) { widthSum += sz.Width + 1; } widthSum += simg.Width + Owner.ItemMargin.Horizontal; heightSum += Math.Max(sz.Height, simg.Height); break; case RibbonElementSizeMode.Compact: widthSum += simg.Width; heightSum += simg.Height; break; default: throw new ApplicationException("SizeMode not supported: " + e.SizeMode.ToString()); } //if (theSize == RibbonElementSizeMode.DropDown) //{ // heightSum += 2; //} switch (Style) { case RibbonButtonStyle.DropDown: case RibbonButtonStyle.SplitDropDown: // drawing size calculation for DropDown and SplitDropDown is identical widthSum += arrowWidth + _dropDownMargin.Horizontal; break; case RibbonButtonStyle.DropDownListItem: break; } //check the minimum and mazimum size properties but only in large mode if (theSize == RibbonElementSizeMode.Large) { //Minimum Size if (MinimumSize.Height > 0 && heightSum < MinimumSize.Height) { heightSum = MinimumSize.Height; } if (MinimumSize.Width > 0 && widthSum < MinimumSize.Width) { widthSum = MinimumSize.Width; } //Maximum Size if (MaximumSize.Height > 0 && heightSum > MaximumSize.Height) { heightSum = MaximumSize.Height; } if (MaximumSize.Width > 0 && widthSum > MaximumSize.Width) { widthSum = MaximumSize.Width; } } SetLastMeasuredSize(new Size(widthSum, heightSum)); return(LastMeasuredSize); }
/// <summary> /// Measures the size of the button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <returns></returns> public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e) { RibbonElementSizeMode theSize = GetNearestSize(e.SizeMode); int widthSum = Owner.ItemMargin.Horizontal; int heightSum = Owner.ItemMargin.Vertical; int largeHeight = OwnerPanel == null ? 0 : OwnerPanel.ContentBounds.Height - Owner.ItemPadding.Vertical;// -Owner.ItemMargin.Vertical; //58; Size simg = SmallImage != null ? SmallImage.Size : Size.Empty; Size img = Image != null ? Image.Size : Size.Empty; Size sz = Size.Empty; switch (theSize) { case RibbonElementSizeMode.Large: case RibbonElementSizeMode.Overflow: sz = MeasureStringLargeSize(e.Graphics, Text, Owner.Font); if (!string.IsNullOrEmpty(Text)) { widthSum += Math.Max(sz.Width + 1, img.Width); heightSum = largeHeight; } else { widthSum += img.Width; heightSum += img.Height; } break; case RibbonElementSizeMode.DropDown: case RibbonElementSizeMode.Medium: sz = TextRenderer.MeasureText(Text, Owner.Font); if (!string.IsNullOrEmpty(Text)) { widthSum += sz.Width + 1; } widthSum += simg.Width + Owner.ItemMargin.Horizontal; heightSum += Math.Max(sz.Height, simg.Height); break; case RibbonElementSizeMode.Compact: widthSum += simg.Width; heightSum += simg.Height; break; default: throw new ApplicationException("SizeMode not supported: " + e.SizeMode.ToString()); } if (theSize == RibbonElementSizeMode.DropDown) { heightSum += 2; } if (Style == RibbonButtonStyle.DropDown) { widthSum += arrowWidth + Owner.ItemMargin.Right; } else if (Style == RibbonButtonStyle.SplitDropDown) { widthSum += arrowWidth + Owner.ItemMargin.Horizontal; } SetLastMeasuredSize(new Size(widthSum, heightSum)); return(LastMeasuredSize); }
public static void AdjustTileToWidth(this ListView lvw, int maxLines = 1, int iconSpacing = 4) { const string str = "Wg"; var lvTVInfo = new NativeMethods.LVTILEVIEWINFO(0) { IconTextSpacing = iconSpacing, MaxTextLines = maxLines }; var sb = new StringBuilder(str); for (var i = 0; i < maxLines; i++) { sb.Append("\r" + str); } using (var g = lvw.CreateGraphics()) lvTVInfo.TileSize = new Size(lvw.ClientSize.Width, Math.Max(lvw.LargeImageList.ImageSize.Height, TextRenderer.MeasureText(g, sb.ToString(), lvw.Font).Height)); NativeMethods.SendMessage(lvw.Handle, NativeMethods.ListViewMessage.SetTileViewInfo, 0, lvTVInfo); //var lvTVInfo = new NativeMethods.LVTILEVIEWINFO(0) { TileWidth = lvw.ClientSize.Width }; //NativeMethods.SendMessage(lvw.Handle, NativeMethods.ListViewMessage.SetTileViewInfo, 0, lvTVInfo); //NativeMethods.SendMessage(lvw.Handle, (uint)NativeMethods.ListViewMessage.SetExtendedListViewStyle, new IntPtr(0x200000), new IntPtr(0x200000)); }
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); // Can't render without an owner if (this.Owner == null) { return; } // If DropDown.ShowImageMargin is false, we don't display the image Image draw_image = this.UseImageMargin ? this.Image : null; // Disable this color detection until we do the color detection for ToolStrip *completely* // Color font_color = this.ForeColor == SystemColors.ControlText ? SystemColors.MenuText : this.ForeColor; Color font_color = ForeColor; if ((this.Selected || this.Pressed) && this.IsOnDropDown && font_color == SystemColors.MenuText) { font_color = SystemColors.HighlightText; } if (!this.Enabled && this.ForeColor == SystemColors.ControlText) { font_color = SystemColors.GrayText; } // Gray stuff out if we're disabled draw_image = this.Enabled ? draw_image : ToolStripRenderer.CreateDisabledImage(draw_image); // Draw our background this.Owner.Renderer.DrawMenuItemBackground(new ToolStripItemRenderEventArgs(e.Graphics, this)); // Figure out where our text and image go Rectangle text_layout_rect; Rectangle image_layout_rect; this.CalculateTextAndImageRectangles(out text_layout_rect, out image_layout_rect); if (this.IsOnDropDown) { if (!this.UseImageMargin) { image_layout_rect = Rectangle.Empty; text_layout_rect = new Rectangle(8, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height); } else { text_layout_rect = new Rectangle(35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height); if (image_layout_rect != Rectangle.Empty) { image_layout_rect = new Rectangle(new Point(4, 3), base.GetImageSize()); } } if (this.Checked && this.ShowMargin) { this.Owner.Renderer.DrawItemCheck(new ToolStripItemImageRenderEventArgs(e.Graphics, this, new Rectangle(2, 1, 19, 19))); } } if (text_layout_rect != Rectangle.Empty) { this.Owner.Renderer.DrawItemText(new ToolStripItemTextRenderEventArgs(e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign)); } string key_string = GetShortcutDisplayString(); if (!string.IsNullOrEmpty(key_string) && !this.HasDropDownItems) { int offset = 15; Size key_string_size = TextRenderer.MeasureText(key_string, this.Font); Rectangle key_string_rect = new Rectangle(this.ContentRectangle.Right - key_string_size.Width - offset, text_layout_rect.Top, key_string_size.Width, text_layout_rect.Height); this.Owner.Renderer.DrawItemText(new ToolStripItemTextRenderEventArgs(e.Graphics, this, key_string, key_string_rect, font_color, this.Font, this.TextAlign)); } if (image_layout_rect != Rectangle.Empty) { this.Owner.Renderer.DrawItemImage(new ToolStripItemImageRenderEventArgs(e.Graphics, this, draw_image, image_layout_rect)); } if (this.IsOnDropDown && this.HasDropDownItems && this.Parent is ToolStripDropDownMenu) { this.Owner.Renderer.DrawArrow(new ToolStripArrowRenderEventArgs(e.Graphics, this, new Rectangle(this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right)); } return; }
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) { if (text == null) { text = ""; } if (caption == null) { caption = ""; } // ensure we are always in a known state _state = DialogResult.None; // convert to nice wrapped lines. text = AddNewLinesToText(text); // get pixel width and height Size textSize = TextRenderer.MeasureText(text, SystemFonts.DefaultFont); // allow for icon if (icon != MessageBoxIcon.None) { textSize.Width += SystemIcons.Question.Width; } var msgBoxFrm = new Form { FormBorderStyle = FormBorderStyle.FixedDialog, ShowInTaskbar = false, StartPosition = FormStartPosition.CenterScreen, Text = caption, MaximizeBox = false, MinimizeBox = false, Width = textSize.Width + 50, Height = textSize.Height + 100, TopMost = true, TopLevel = true }; Rectangle screenRectangle = msgBoxFrm.RectangleToScreen(msgBoxFrm.ClientRectangle); int titleHeight = screenRectangle.Top - msgBoxFrm.Top; var lblMessage = new Label { Left = 58, Top = 15, Width = textSize.Width + 10, Height = textSize.Height + 10, Text = text }; msgBoxFrm.Controls.Add(lblMessage); var actualIcon = getMessageBoxIcon(icon); if (actualIcon == null) { lblMessage.Location = new Point(FORM_X_MARGIN, FORM_Y_MARGIN); } else { var iconPbox = new PictureBox { Image = actualIcon.ToBitmap(), Location = new Point(FORM_X_MARGIN, FORM_Y_MARGIN) }; msgBoxFrm.Controls.Add(iconPbox); } AddButtonsToForm(msgBoxFrm, buttons); // display even if theme fails try { ThemeManager.ApplyThemeTo(msgBoxFrm); } catch { } if (System.Windows.Forms.Application.OpenForms.Count > 0) { msgBoxFrm.StartPosition = FormStartPosition.Manual; Form parentForm = System.Windows.Forms.Application.OpenForms[0]; // center of first form msgBoxFrm.Location = new Point(parentForm.Location.X + parentForm.Width / 2 - msgBoxFrm.Width / 2, parentForm.Location.Y + parentForm.Height / 2 - msgBoxFrm.Height / 2); DialogResult test = msgBoxFrm.ShowDialog(); } else { DialogResult test = msgBoxFrm.ShowDialog(); } DialogResult answer = _state; return(answer); }
// This is not a breaking change -- Even though this control previously autosized to hieght, // it didn't actually have an AutoSize property. The new AutoSize property enables the // smarter behavior. internal override Size GetPreferredSizeCore(Size proposedConstraints) { int height = PreferredHeight; int baseSize = Hexadecimal ? 16 : 10; int digit = GetLargestDigit(0, baseSize); // The floor of log is intentionally 1 less than the number of digits. We initialize // testNumber to account for the missing digit. int numDigits = (int)Math.Floor(Math.Log(Math.Max(-(double)Minimum, (double)Maximum), baseSize)); int maxDigits; if (this.Hexadecimal) { maxDigits = (int)Math.Floor(Math.Log(long.MaxValue, baseSize)); } else { maxDigits = (int)Math.Floor(Math.Log((double)decimal.MaxValue, baseSize)); } bool maxDigitsReached = numDigits >= maxDigits; decimal testNumber; // preinitialize testNumber with the leading digit if (digit != 0 || numDigits == 1) { testNumber = digit; } else { // zero can not be the leading digit if we need more than // one digit. (0*baseSize = 0 in the loop below) testNumber = GetLargestDigit(1, baseSize); } if (maxDigitsReached) { // Prevent numDigits = maxDigits - 1; } // e.g., if the lagest digit is 7, and we can have 3 digits, the widest string would be "777" for (int i = 0; i < numDigits; i++) { testNumber = testNumber * baseSize + digit; } int textWidth = TextRenderer.MeasureText(GetNumberText(testNumber), this.Font).Width; if (maxDigitsReached) { string shortText; if (this.Hexadecimal) { shortText = ((long)testNumber).ToString("X", CultureInfo.InvariantCulture); } else { shortText = testNumber.ToString(CultureInfo.CurrentCulture); } int shortTextWidth = TextRenderer.MeasureText(shortText, this.Font).Width; // Adding the width of the one digit that was dropped earlier. // This assumes that no additional thousand separator is added by that digit which is correct. textWidth += shortTextWidth / (numDigits + 1); } // Call AdjuctWindowRect to add space for the borders int width = SizeFromClientSize(textWidth, height).Width + upDownButtons.Width; return(new Size(width, height) + Padding.Size); }
private void CalculateInternalLayoutMetrics() { Size empty = Size.Empty; Size alignThis = Size.Empty; Size defaultImageSize = ToolStripDropDownMenu.defaultImageSize; Size size = Size.Empty; Size size5 = Size.Empty; for (int i = 0; i < this.Items.Count; i++) { ToolStripItem item = this.Items[i]; ToolStripMenuItem item2 = item as ToolStripMenuItem; if (item2 != null) { Size textSize = item2.GetTextSize(); if (item2.ShowShortcutKeys) { Size shortcutTextSize = item2.GetShortcutTextSize(); if (this.tabWidth == -1) { this.tabWidth = TextRenderer.MeasureText("\t", this.Font).Width; } textSize.Width += this.tabWidth + shortcutTextSize.Width; textSize.Height = Math.Max(textSize.Height, shortcutTextSize.Height); } empty.Width = Math.Max(empty.Width, textSize.Width); empty.Height = Math.Max(empty.Height, textSize.Height); Size size8 = Size.Empty; if (item2.Image != null) { size8 = (item2.ImageScaling == ToolStripItemImageScaling.SizeToFit) ? base.ImageScalingSize : item2.Image.Size; } alignThis.Width = Math.Max(alignThis.Width, size8.Width); alignThis.Height = Math.Max(alignThis.Height, size8.Height); if (item2.CheckedImage != null) { Size size9 = item2.CheckedImage.Size; defaultImageSize.Width = Math.Max(size9.Width, defaultImageSize.Width); defaultImageSize.Height = Math.Max(size9.Height, defaultImageSize.Height); } } else if (!(item is ToolStripSeparator)) { size5.Height = Math.Max(item.Bounds.Height, size5.Height); size5.Width = Math.Max(item.Bounds.Width, size5.Width); } } this.maxItemSize.Height = Math.Max(empty.Height + TextPadding.Vertical, Math.Max((int)(defaultImageSize.Height + CheckPadding.Vertical), (int)(size.Height + ArrowPadding.Vertical))); if (this.ShowImageMargin) { this.maxItemSize.Height = Math.Max(alignThis.Height + ImagePadding.Vertical, this.maxItemSize.Height); } if (this.ShowCheckMargin) { int num1 = defaultImageSize.Width; } if (this.ShowImageMargin) { int num6 = alignThis.Width; } size = new Size(10, this.maxItemSize.Height); empty.Height = this.maxItemSize.Height - TextPadding.Vertical; alignThis.Height = this.maxItemSize.Height - ImagePadding.Vertical; defaultImageSize.Height = this.maxItemSize.Height - CheckPadding.Vertical; empty.Width = Math.Max(empty.Width, size5.Width); Point location = Point.Empty; int width = 0; int num3 = Math.Max(0, alignThis.Width - ToolStripDropDownMenu.defaultImageSize.Width); if (this.ShowCheckMargin && this.ShowImageMargin) { width = DefaultImageAndCheckMarginWidth + num3; location = new Point(CheckPadding.Left, CheckPadding.Top); this.checkRectangle = LayoutUtils.Align(defaultImageSize, new Rectangle(location.X, location.Y, defaultImageSize.Width, this.maxItemSize.Height), ContentAlignment.MiddleCenter); location.X = (this.checkRectangle.Right + CheckPadding.Right) + ImagePadding.Left; location.Y = ImagePadding.Top; this.imageRectangle = LayoutUtils.Align(alignThis, new Rectangle(location.X, location.Y, alignThis.Width, this.maxItemSize.Height), ContentAlignment.MiddleCenter); } else if (this.ShowCheckMargin) { width = DefaultImageMarginWidth; location = new Point(1, CheckPadding.Top); this.checkRectangle = LayoutUtils.Align(defaultImageSize, new Rectangle(location.X, location.Y, width, this.maxItemSize.Height), ContentAlignment.MiddleCenter); this.imageRectangle = Rectangle.Empty; } else if (this.ShowImageMargin) { width = DefaultImageMarginWidth + num3; location = new Point(1, CheckPadding.Top); this.checkRectangle = LayoutUtils.Align(LayoutUtils.UnionSizes(defaultImageSize, alignThis), new Rectangle(location.X, location.Y, width - 1, this.maxItemSize.Height), ContentAlignment.MiddleCenter); this.imageRectangle = this.checkRectangle; } else { width = 0; } location.X = width + 1; this.imageMarginBounds = new Rectangle(0, 0, width, base.Height); location.X = this.imageMarginBounds.Right + TextPadding.Left; location.Y = TextPadding.Top; this.textRectangle = new Rectangle(location, empty); location.X = (this.textRectangle.Right + TextPadding.Right) + ArrowPadding.Left; location.Y = ArrowPadding.Top; this.arrowRectangle = new Rectangle(location, size); this.maxItemSize.Width = (this.arrowRectangle.Right + ArrowPadding.Right) - this.imageMarginBounds.Left; base.Padding = this.DefaultPadding; int num4 = this.imageMarginBounds.Width; if (this.RightToLeft == RightToLeft.Yes) { num4 += TextPadding.Right; int num5 = this.maxItemSize.Width; this.checkRectangle.X = num5 - this.checkRectangle.Right; this.imageRectangle.X = num5 - this.imageRectangle.Right; this.textRectangle.X = num5 - this.textRectangle.Right; this.arrowRectangle.X = num5 - this.arrowRectangle.Right; this.imageMarginBounds.X = num5 - this.imageMarginBounds.Right; } else { num4 += TextPadding.Left; } this.maxItemSize.Height += this.maxItemSize.Height % 2; this.textRectangle.Y = LayoutUtils.VAlign(this.textRectangle.Size, new Rectangle(Point.Empty, this.maxItemSize), ContentAlignment.MiddleCenter).Y; this.textRectangle.Y += this.textRectangle.Height % 2; this.state[stateMaxItemSizeValid] = true; this.PaddingToTrim = num4; }
/// <summary> /// Paints the control. /// </summary> /// <param name="e">The <see cref="PaintEventArgs"/> instance containing the event data.</param> protected virtual void PaintControl(PaintEventArgs e) { var cbi = NativeMethods.COMBOBOXINFO.FromComboBox(this); var itemText = SelectedIndex >= 0 ? GetItemText(SelectedItem) : string.Empty; var state = Enabled ? currentState : ComboBoxState.Disabled; Rectangle tr = cbi.rcItem; /*Rectangle tr = this.ClientRectangle; * tr.Width -= (SystemInformation.VerticalScrollBarWidth + 2); * tr.Inflate(0, -2); * tr.Offset(1, 0);*/ Rectangle br = cbi.rcButton; var vsSuccess = false; if (VisualStyleRenderer.IsSupported && Application.RenderWithVisualStyles) { /*Rectangle r = Rectangle.Inflate(this.ClientRectangle, 1, 1); * if (this.DropDownStyle != ComboBoxStyle.DropDownList) * { * e.Graphics.Clear(this.BackColor); * ComboBoxRenderer.DrawTextBox(e.Graphics, r, itemText, this.Font, tr, tff, state); * ComboBoxRenderer.DrawDropDownButton(e.Graphics, br, state); * } * else*/ { try { var vr = new VisualStyleRenderer("Combobox", DropDownStyle == ComboBoxStyle.DropDownList ? 5 : 4, (int)state); vr.DrawParentBackground(e.Graphics, ClientRectangle, this); vr.DrawBackground(e.Graphics, ClientRectangle); if (DropDownStyle != ComboBoxStyle.DropDownList) { br.Inflate(1, 1); } var cr = DropDownStyle == ComboBoxStyle.DropDownList ? Rectangle.Inflate(br, -1, -1) : br; vr.SetParameters("Combobox", 7, (int)(br.Contains(PointToClient(Cursor.Position)) ? state : ComboBoxState.Normal)); vr.DrawBackground(e.Graphics, br, cr); if (Focused && State != ComboBoxState.Pressed) { var sz = TextRenderer.MeasureText(e.Graphics, "Wg", Font, tr.Size, TextFormatFlags.Default); var fr = Rectangle.Inflate(tr, 0, (sz.Height - tr.Height) / 2 + 1); ControlPaint.DrawFocusRectangle(e.Graphics, fr); } var fgc = Enabled ? ForeColor : SystemColors.GrayText; TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff); vsSuccess = true; } catch { } } } if (!vsSuccess) { Diagnostics.Debug.WriteLine($"CR:{ClientRectangle};ClR:{e.ClipRectangle};Foc:{Focused};St:{state};Tx:{itemText}"); var focusedNotPressed = Focused && state != ComboBoxState.Pressed; var bgc = Enabled ? (focusedNotPressed ? SystemColors.Highlight : BackColor) : SystemColors.Control; var fgc = Enabled ? (focusedNotPressed ? SystemColors.HighlightText : ForeColor) : SystemColors.GrayText; e.Graphics.Clear(bgc); ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle, Border3DStyle.Sunken); ControlPaint.DrawComboButton(e.Graphics, br, Enabled ? (state == ComboBoxState.Pressed ? ButtonState.Pushed : ButtonState.Normal) : ButtonState.Inactive); tr = new Rectangle(tr.X + 3, tr.Y + 1, tr.Width - 4, tr.Height - 2); TextRenderer.DrawText(e.Graphics, itemText, Font, tr, fgc, tff); if (focusedNotPressed) { var fr = Rectangle.Inflate(cbi.rcItem, -1, -1); fr.Height++; fr.Width++; ControlPaint.DrawFocusRectangle(e.Graphics, fr, fgc, bgc); e.Graphics.DrawRectangle(SystemPens.Window, cbi.rcItem); } } }
/// <summary> /// Measures the size of the tab. The tab content bounds is measured by the Ribbon control /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e) { Size textSize = TextRenderer.MeasureText(Text, Owner.Font); return(textSize); }
public static void DrawGroupBox(Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state) { Size font_size = TextRenderer.MeasureText(groupBoxText, font); if (Application.RenderWithVisualStyles || always_use_visual_styles == true) { VisualStyleRenderer vsr; Rectangle new_bounds; switch (state) { case GroupBoxState.Normal: default: vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal); new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 1, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 1); break; case GroupBoxState.Disabled: vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Disabled); new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 2, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 2); break; } if (groupBoxText == String.Empty) { vsr.DrawBackground(g, bounds); } else { vsr.DrawBackgroundExcludingArea(g, new_bounds, new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height)); } if (textColor == Color.Empty) { textColor = vsr.GetColor(ColorProperty.TextColor); } if (groupBoxText != String.Empty) { TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 8, bounds.Top), textColor, flags); } } else { // MS has a pretty big bug when rendering the non-visual styles group box. Instead of using the height // part of the bounds as height, they use it as the bottom, so the boxes are drawn in completely different // places. Rather than emulate this bug, we do it correctly. After googling for a while, I don't think // anyone has ever actually used this class for anything, so it should be fine. :) Rectangle new_bounds = new Rectangle(bounds.Left, bounds.Top + (int)(font_size.Height / 2), bounds.Width, bounds.Height - (int)(font_size.Height / 2)); // Don't paint over the background where we are going to put the text Region old_clip = g.Clip; g.SetClip(new Rectangle(bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude); ControlPaint.DrawBorder3D(g, new_bounds, Border3DStyle.Etched); g.Clip = old_clip; if (groupBoxText != String.Empty) { if (textColor == Color.Empty) { textColor = state == GroupBoxState.Normal ? SystemColors.ControlText : SystemColors.GrayText; } TextRenderer.DrawText(g, groupBoxText, font, new Point(bounds.Left + 8, bounds.Top), textColor, flags); } } }
public ThreadExceptionDialog(Exception t) { if (DpiHelper.IsScalingRequirementMet) { scaledMaxWidth = LogicalToDeviceUnits(MAXWIDTH); scaledMaxHeight = LogicalToDeviceUnits(MAXHEIGHT); scaledPaddingWidth = LogicalToDeviceUnits(PADDINGWIDTH); scaledPaddingHeight = LogicalToDeviceUnits(PADDINGHEIGHT); scaledMaxTextWidth = LogicalToDeviceUnits(MAXTEXTWIDTH); scaledMaxTextHeight = LogicalToDeviceUnits(MAXTEXTHEIGHT); scaledButtonTopPadding = LogicalToDeviceUnits(BUTTONTOPPADDING); scaledButtonDetailsLeftPadding = LogicalToDeviceUnits(BUTTONDETAILS_LEFTPADDING); scaledMessageTopPadding = LogicalToDeviceUnits(MESSAGE_TOPPADDING); scaledHeightPadding = LogicalToDeviceUnits(HEIGHTPADDING); scaledButtonWidth = LogicalToDeviceUnits(BUTTONWIDTH); scaledButtonHeight = LogicalToDeviceUnits(BUTTONHEIGHT); scaledButtonAlignmentWidth = LogicalToDeviceUnits(BUTTONALIGNMENTWIDTH); scaledButtonAlignmentPadding = LogicalToDeviceUnits(BUTTONALIGNMENTPADDING); scaledDetailsWidthPadding = LogicalToDeviceUnits(DETAILSWIDTHPADDING); scaledDetailsHeight = LogicalToDeviceUnits(DETAILSHEIGHT); scaledPictureWidth = LogicalToDeviceUnits(PICTUREWIDTH); scaledPictureHeight = LogicalToDeviceUnits(PICTUREHEIGHT); scaledExceptionMessageVerticalPadding = LogicalToDeviceUnits(EXCEPTIONMESSAGEVERTICALPADDING); } string messageFormat; string messageText; Button[] buttons; bool detailAnchor = false; if (t is WarningException w) { messageFormat = SR.ExDlgWarningText; messageText = w.Message; if (w.HelpUrl is null) { buttons = new Button[] { continueButton }; } else { buttons = new Button[] { continueButton, helpButton }; } } else { messageText = t.Message; detailAnchor = true; if (Application.AllowQuit) { if (t is Security.SecurityException) { messageFormat = SR.ExDlgSecurityErrorText; } else { messageFormat = SR.ExDlgErrorText; } buttons = new Button[] { detailsButton, continueButton, quitButton }; } else { if (t is Security.SecurityException) { messageFormat = SR.ExDlgSecurityContinueErrorText; } else { messageFormat = SR.ExDlgContinueErrorText; } buttons = new Button[] { detailsButton, continueButton }; } } if (messageText.Length == 0) { messageText = t.GetType().Name; } if (t is Security.SecurityException) { messageText = string.Format(messageFormat, t.GetType().Name, Trim(messageText)); } else { messageText = string.Format(messageFormat, Trim(messageText)); } StringBuilder detailsTextBuilder = new StringBuilder(); string newline = "\r\n"; string separator = SR.ExDlgMsgSeperator; string sectionseparator = SR.ExDlgMsgSectionSeperator; if (Application.CustomThreadExceptionHandlerAttached) { detailsTextBuilder.Append(SR.ExDlgMsgHeaderNonSwitchable); } else { detailsTextBuilder.Append(SR.ExDlgMsgHeaderSwitchable); } detailsTextBuilder.Append(string.Format(CultureInfo.CurrentCulture, sectionseparator, SR.ExDlgMsgExceptionSection)); detailsTextBuilder.Append(t.ToString()); detailsTextBuilder.Append(newline); detailsTextBuilder.Append(newline); detailsTextBuilder.Append(string.Format(CultureInfo.CurrentCulture, sectionseparator, SR.ExDlgMsgLoadedAssembliesSection)); foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { AssemblyName name = asm.GetName(); string fileVer = SR.NotAvailable; try { if (name.EscapedCodeBase != null && name.EscapedCodeBase.Length > 0) { Uri codeBase = new Uri(name.EscapedCodeBase); if (codeBase.Scheme == "file") { fileVer = FileVersionInfo.GetVersionInfo(NativeMethods.GetLocalPath(name.EscapedCodeBase)).FileVersion; } } } catch (IO.FileNotFoundException) { } detailsTextBuilder.Append(string.Format(SR.ExDlgMsgLoadedAssembliesEntry, name.Name, name.Version, fileVer, name.EscapedCodeBase)); detailsTextBuilder.Append(separator); } detailsTextBuilder.Append(string.Format(CultureInfo.CurrentCulture, sectionseparator, SR.ExDlgMsgJITDebuggingSection)); if (Application.CustomThreadExceptionHandlerAttached) { detailsTextBuilder.Append(SR.ExDlgMsgFooterNonSwitchable); } else { detailsTextBuilder.Append(SR.ExDlgMsgFooterSwitchable); } detailsTextBuilder.Append(newline); detailsTextBuilder.Append(newline); string detailsText = detailsTextBuilder.ToString(); Graphics g = message.CreateGraphicsInternal(); Size textSize = new Size(scaledMaxWidth - scaledPaddingWidth, int.MaxValue); if (DpiHelper.IsScalingRequirementMet && !Control.UseCompatibleTextRenderingDefault) { // we need to measure string using API that matches the rendering engine - TextRenderer.MeasureText for GDI textSize = Size.Ceiling(TextRenderer.MeasureText(messageText, Font, textSize, TextFormatFlags.WordBreak)); } else { // if HighDpi improvements are not enabled, or rendering mode is GDI+, use Graphics.MeasureString textSize = Size.Ceiling(g.MeasureString(messageText, Font, textSize.Width)); } textSize.Height += scaledExceptionMessageVerticalPadding; g.Dispose(); if (textSize.Width < scaledMaxTextWidth) { textSize.Width = scaledMaxTextWidth; } if (textSize.Height > scaledMaxHeight) { textSize.Height = scaledMaxHeight; } int width = textSize.Width + scaledPaddingWidth; int buttonTop = Math.Max(textSize.Height, scaledMaxTextHeight) + scaledPaddingHeight; Form activeForm = Form.ActiveForm; if (activeForm is null || activeForm.Text.Length == 0) { Text = SR.ExDlgCaption; }