[Visual C# WPF] Shape name, Label, Text
Shape 위(or 안)에 Shape 이름, 라벨이 필요한 경우
GDI+ 는 또 한번 갑갑함의 극치를 보여준다
Transparency, 투명화 할려면 할 수 있으려나???
편법으로 하는건 GDI+로는 안되고 라벨을 만들어 미리 숨겨두고 필요할 때 값을 주어서 사용
이렇게 만들었을 시 드래그 하면 잔상이 너무 심하다
기능적으로 문제가 있는게 아니라 하여간 보기 싫다
WPF에서는 text를 shape위에 쓰는건 쉽다...문제는 drag...결론은 됨
Text 추가 방법(XAML, C#)
<XAML 코드> - 캔버스 안에 추가
<Window x:Class="DragShapes.MainWindow"
<Canvas x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="testText" Text="IDL" MouseLeftButtonDown="shape_MouseLeftButtonDown" MouseMove="shape_MouseMove" MouseLeftButtonUp="shape_MouseLeftButtonUp" Canvas.Left="200" Canvas.Top="100"/>
</Canvas>
</Window>
<C# 코드>
TextBlock testText = new TextBlock();
testText.Text = "IDL";
Canvas.SetLeft(testText, 200);
Canvas.SetTop(testText, 100);
LayoutRoot.Children.Add(testText);
두가지를 대충 섞어서 ㄱㄱ
<C# 코드> - 기본 코드에서 수정 된 부분
public MainWindow()
{
InitializeComponent();
testText.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
testText.Arrange(new Rect(testText.DesiredSize));
testEllipse.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
testEllipse.Arrange(new Rect(testEllipse.DesiredSize));
x_shape = Canvas.GetLeft(testEllipse);
y_shape = Canvas.GetTop(testEllipse);
x_textBlock = x_shape + testEllipse.Width / 2 - testText.ActualWidth / 2;
Canvas.SetLeft(testText, x_textBlock);
y_textBlock = y_shape + testEllipse.Height / 2 - testText.ActualHeight / 2;
Canvas.SetTop(testText, y_textBlock);
}
private void shape_MouseMove(object sender, MouseEventArgs e)
{
if (captured)
{
double x = e.GetPosition(LayoutRoot).X;
double y = e.GetPosition(LayoutRoot).Y;
x_shape += x - x_canvas;
x_textBlock += x - x_canvas;
Canvas.SetLeft(source, x_shape);
Canvas.SetLeft(testText, x_textBlock);
x_canvas = x;
y_shape += y - y_canvas;
y_textBlock += y - y_canvas;
Canvas.SetTop(source, y_shape);
Canvas.SetTop(testText, y_textBlock);
y_canvas = y;
}
}
<XAML 코드> - 기본 코드에서 수정
<Canvas x:Name="LayoutRoot" Background="White">
<Ellipse x:Name="testEllipse" Fill="Blue" Height="100" Stroke="Black" Width="100" MouseLeftButtonDown="shape_MouseLeftButtonDown" MouseMove="shape_MouseMove" MouseLeftButtonUp="shape_MouseLeftButtonUp" Canvas.Left="200" Canvas.Top="100"/>
<TextBlock x:Name="testText" Text="IDL" Width="{Binding Path=ActualWidth, ElementName=testText}" MouseLeftButtonDown="shape_MouseLeftButtonDown" MouseMove="shape_MouseMove" MouseLeftButtonUp="shape_MouseLeftButtonUp" />
</Canvas>
캔버스에 TextBlock 추가
**** shape 드래그 시 text를 같이 이동하기 위한 canvas setting 참고 ****
Dynamic Data Display - WPF - Need to add text to canvas - C#
I am using the dynamic data display WPF chart. I have a requirement to display a label next to every point on the curves plotted on ...
stackoverflow.com
**** 문자 길이 계산해서 원센터에 맞추기 위한 방법 참고 ****
[SOLVED] How to calculate WPF TextBlock width for its known font s... ...
Let's say I have TextBlock with text
solvedstack.com