2017-10-17 19 views
0

我正在繪製一個wpf畫布中的五角形,它工作正常,但我真的不知道如何在畫布中繪製一個簡單的點/點。我問過谷歌,我找不到合適/簡單的答案。如何使用x和y座標在c#wpf畫布中繪製一個簡單點?

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    string x = xCoo.Text; 
    string y = yCoo.Text; 
    commandText = "Select f.p_ID, f.bezeichnung from figure05 f " + 
    "where SDO_CONTAINS(f.shape, " + 
    "SDO_GEOMETRY(2001, NULL, " + 
    "SDO_POINT_TYPE("+Convert.ToInt32(x)+", "+Convert.ToInt32(y)+", NULL), NULL, NULL" + 
    ")) = 'TRUE';"; 
    using (OleDbConnection conn = new OleDbConnection(cs)) 
    { 
     try 
     { 
      conn.Open(); 
      MessageBox.Show("Connection open!"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     cmd = new OleDbCommand(commandText, conn); 
     OleDbDataReader reader = cmd.ExecuteReader(); 
     if (reader.Read()) 
     { 
      MessageBox.Show("Point is in Pentagon"); 
      //Drawing point here 
     } 

    } 
} 

我的畫布:

<Canvas Name="myCanvas" Background="LightBlue" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="318"/> 

感謝任何形式的幫助!

+0

可在畫一條線具有零長度,即相同的開始和結束點,一個適當的StrokeThickness,並將StrokeStartLineCap和StrokeEndLineCap設置爲Square或Round。 – Clemens

回答

-2

你可以只繪製一個橢圓喜歡這裏解釋:

也看看以下內容:

編輯: 就像在這裏的評論中提到的一個例子如何在位置50,50的橢圓對象畫布上畫一個像素。

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApp1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <Button Grid.Column="0" Grid.Row="0" Name="drawButton" Content="Draw!" Click="DrawButton_OnClick" /> 

     <Canvas Grid.Column="0" Grid.Row="1" Name="myCanvas" Background="LightBlue" /> 
    </Grid> 
</Window> 

MainWindow.xaml.cs

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 
using System.Windows.Shapes; 

namespace WpfApp1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void DrawButton_OnClick(object sender, RoutedEventArgs e) 
     { 
      var brush = new SolidColorBrush(Colors.Black); 

      var ellipse = new Ellipse 
      { 
       Stroke = brush, 
       Fill = brush, 
       StrokeThickness = 1, 
       Height = 1, 
       Width = 1, 
      }; 

      Canvas.SetLeft(ellipse, 50); 
      Canvas.SetTop(ellipse, 50); 

      myCanvas.Children.Add(ellipse); 
     } 
    } 
} 
+0

是不是有一個簡單的方法來繪製一個點?像點p =新點(x,y)並將其添加到我的畫布?我在互聯網上找不到任何東西。或者我必須繪製一個橢圓? –

+0

你也可以用一條線畫出一個像素。這裏是另一個正則表達式:[WPF Canvas - Single Pixel Grid](https://stackoverflow.com/questions/2916496/wpf-canvas-single-pixel-grid) – ChW

相關問題