Hi i want to make my graphic object "selected" after click on them.
I tryied something like that to make selected line:
else if (e.OriginalSource is Line)
{
LineFocus = true;
MojaLinia = (Line)e.OriginalSource;
Rectangle rect_1 = new Rectangle
{
Stroke = Brushes.Black,
StrokeThickness = 1,
Fill = new SolidColorBrush(Color.FromRgb(255, 255, 255))
};
rect_1.Width = 6;
rect_1.Height = 6;
Canvas.SetLeft(rect_1, MojaLinia.X1);
Canvas.SetTop(rect_1, MojaLinia.Y1);
canvas.Children.Add(rect_1);
Rectangle rect_2 = new Rectangle
{
Stroke = Brushes.Black,
StrokeThickness = 1,
Fill = new SolidColorBrush(Color.FromRgb(255, 255, 255))
};
rect_2.Width = 6;
rect_2.Height = 6;
Canvas.SetLeft(rect_2, MojaLinia.X2);
Canvas.SetTop(rect_2, MojaLinia.Y2);
canvas.Children.Add(rect_2);
}
Its a bit stupid, and its hard to make white rectangle with distance from line. Is there any good way to do this without tons of if() ?
Im using VS2012, WPF/C# .
You can use Adorners to show your shape as "selected", this way you can also make the "selected" state visual hint in XAML.
And you can use an Attached Property to "add" a IsSelected property (boolean for example) to your object and toggle the value each time there is a click event rised.
Related
I have been using UmaStackPanel to align two views in Xamarin iOS. Somehow, even when I don't specifically define the space, there's a huge gap between the two views. One on one corner and the other in the other corner. I have been trying to remove the gap between, and I have failed. How to go about this?
The first button looks like this:
UIButton test= new UIButton(UIButtonType.System)
{
HorizontalAlignment = UIControlContentHorizontalAlignment.Left,
TranslatesAutoresizingMaskIntoConstraints = false,
TitleEdgeInsets = new UIEdgeInsets(0, 10, 0, 0),
Font = UmaFont.GetDefaultAppFont(17f)
};
test.SetImage(IconUtils.arrow.ToUIImage(new CoreGraphics.CGSize(24, 24)), UIControlState.Normal);
test.SetTitle(My Test, UIControlState.Normal);
test.SetTitleColor(UIColor.Black, UIControlState.Normal);
test.TintColor = iconColor;
UIImageView testView = new UIImageView()
{
ClipsToBounds = true,
TranslatesAutoresizingMaskIntoConstraints = false,
};
testView.Layer.MasksToBounds = true;
testView.Layer.CornerRadius = 12;
testView.ContentMode = UIViewContentMode.ScaleAspectFit;
testView.Image = IconUtils.arrow.ToUIImage(new CoreGraphics.CGSize(24, 24));
testView.Layer.BorderWidth = 2;
And I created a horizontal UmaStackPanel and added the views.
This is example of what white color and text/font size and style I want to get in my project taken from another project.
I want to get in the editorwindow background color and the white text color like in the screenshot.
I don't need a treeview just to make this kind of text.
I'm using EditorWindow type script.
In the top I did:
private static Texture2D tex;
Then:
[MenuItem("Window/Test")]
static void ShowEditor()
{
editor = EditorWindow.GetWindow<Test>();
editor.Init();
tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
tex.SetPixel(0, 0, Color.black);
tex.Apply();
CenterWindow();
}
And inside the OnGUI:
void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, maxSize.x, maxSize.y), tex, ScaleMode.StretchToFill);
//GUI.Label(new Rect(200, 200, 100, 100), "A label");
//GUI.TextField(new Rect(20, 20, 70, 30), "");
GUIStyle itemStyle = new GUIStyle(); //make a new GUIStyle
itemStyle.alignment = TextAnchor.MiddleLeft; //align text to the left
itemStyle.active.background = itemStyle.normal.background; //gets rid of button click background style.
itemStyle.margin = new RectOffset(0, 0, 0, 0);
GUI.backgroundColor = Color.white;
GUI.skin.toggle.fontStyle = FontStyle.Normal;
GUI.skin.toggle.fontSize = 13;
}
But it didn't change much. It draw and colored the whole window in black but the elements the gui elements are not in white like in the screenshot example.
You are missing itemStyle.normal.textColor = Color.white;. Adding this should make all the text with this style turn white
This question already has answers here:
how to change the check image on a checkbox
(5 answers)
Closed 2 years ago.
I want to change the border color and the background of the square and the color of the check mark, but not the text. To better understand, what I want to accomplish is the following example:
checkBox1.Checked = false
checkBox1.Checked = true
Thanks so much to everyone of you responds exactly to this request!
You can simply draw the checkmark in the Paint event:
private void checkBox1_Paint(object sender, PaintEventArgs e)
{
Point pt = new Point(e.ClipRectangle.Left + 2, e.ClipRectangle.Top + 4);
Rectangle rect = new Rectangle(pt, new Size(22, 22));
if (checkBox1.Checked)
{
using (Font wing = new Font("Wingdings", 14f))
e.Graphics.DrawString("ü", wing, Brushes.DarkOrange,rect);
}
e.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect);
}
for this to work you need to:
set Apperance = Appearance.Button
set FlatStyle = FlatStyle.Flat
set TextAlign = ContentAlignment.MiddleRight
set FlatAppearance.BorderSize = 0
set AutoSize = false
If you want to re-use this it will be best to subclass the checkbox and override the OnPaint event there. Here is an example:
public ColorCheckBox()
{
Appearance = System.Windows.Forms.Appearance.Button;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
TextAlign = ContentAlignment.MiddleRight;
FlatAppearance.BorderSize = 0;
AutoSize = false;
Height = 16;
}
protected override void OnPaint(PaintEventArgs pevent)
{
//base.OnPaint(pevent);
pevent.Graphics.Clear(BackColor);
using (SolidBrush brush = new SolidBrush(ForeColor))
pevent.Graphics.DrawString(Text, Font, brush, 27, 4);
Point pt = new Point( 4 , 4);
Rectangle rect = new Rectangle(pt, new Size(16, 16));
pevent.Graphics.FillRectangle(Brushes.Beige, rect);
if (Checked)
{
using (SolidBrush brush = new SolidBrush(ccol))
using (Font wing = new Font("Wingdings", 12f))
pevent.Graphics.DrawString("ü", wing, brush, 1,2);
}
pevent.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect);
Rectangle fRect = ClientRectangle;
if (Focused)
{
fRect.Inflate(-1, -1);
using (Pen pen = new Pen(Brushes.Gray) { DashStyle = DashStyle.Dot })
pevent.Graphics.DrawRectangle(pen, fRect);
}
}
You may need to tweek the sizes of the control and the font.. And if you want to you expand the code to honor the TextAlign and the CheckAlign properties.
And if you need a three-state control you can adapt the code to display a third state appearance, especially if you think of one that looks better than the original..
You have to write your own checkbox. By making a custom control in which there's a blue square(possibly inherits from Button) that toggles between checked and not checked images by an OnClick event and place a label next to it.
I have a figure in Canvas in WPF application. This figure is generated by program (I click a button and then a figure appears). How to order the Figure_MouseLeftButtonDown function to change some properties of THIS figure? Also I want to move this figure by dragging. Now I have something like this:
var ell = new Ellipse() {
Name = "FirstEllipse",
Width = 150,
Height = 100,
Margin = new Thickness(200, 150, 0, 0),
Fill = Brushes.Red
};
ell.MouseLeftButtonDown += Ellipse_MouseLeftButtonDown;
canvas.Children.Add(ell);
private void Figure_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
sender.SetValue(Ellipse.FillProperty, Brushes.Aquamarine);
}
I made it, maybe this answer will help somebody else. Ellipse ellip = new Ellipse();
ellip = (Ellipse)sender;
ellip.SetValue(Ellipse.FillProperty, Brushes.Aquamarine);
I am having a problem on my extendedtabcontol class, I cannot get rid of the dotted box or the visual style box on the selected tab. I have my own DrawItem (see below), I have overridden several methods from the tabcontrol and I have even overridden the WM_SETFOCUS and WM_PAINT in WndProc but the box and highlight will not go away. Is there anyway to turn them off (the box or visual style) or a simple way to draw over them / stop them drawing?
The user can tell which tab is selected because it is drawn in black when the others are grey.
protected void OnDrawItem(object sender, DrawItemEventArgs e)
{
// VisualStyleRenderer renderer =
// new VisualStyleRenderer(VisualStyleElement.Tab.TabItem.Disabled);
DrawItemState ds = e.State;
SolidBrush mybrush = new SolidBrush(Color.FromArgb(255, 151, 0, 11));
Rectangle tabArea2 = new Rectangle(0, 0, this.Size.Width+10, this.Size.Height+10);
//renderer.DrawBackground(e.Graphics, tabArea2);
e.Graphics.FillRectangle(mybrush, tabArea2);
int i = 0;
foreach (TabPage tb in this.TabPages)
{
Rectangle tabArea = this.GetTabRect(i);
Point newp = new Point(tabArea.Location.X,tabArea.Location.Y + 2);
tabArea.Location = newp;
if (this.SelectedIndex != i)
{
RectangleF tabTextArea = (RectangleF)this.GetTabRect(i);
e.Graphics.DrawImage(global::Config.Properties.Resources.Tab2, tabArea.Location);
}
else
{
e.Graphics.DrawImage(global::Config.Properties.Resources.Tab1, tabArea.Location);
}
SizeF size = e.Graphics.MeasureString(tb.Name.ToString().Trim(), drawFont);
PointF pf = new PointF();
pf.X = tabArea.X + (tabArea.Width / 2) - (size.Width/2);
pf.Y = tabArea.Y + (tabArea.Height / 2) - (size.Height/2);
e.Graphics.DrawString(tb.Name.ToString().Trim(), drawFont, drawBrush, pf);
i++;
}
}
I would post an image but I don't have the reputation.
Similar question for example:
Can I remove the dotted focus rectangle over tabs on a TabControl?