WPF接口实例

1完成Drawing工程

接口IColor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace Drawing
{
interface IColor
{
void SetColor(Color color);
}
}
接口IDraw

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace Drawing
{
interface IDraw
{
void SetLocation(int xCoord,int yCoord);
void Draw(Canvas canvas);
}
}

画方块类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;

namespace Drawing
{
class Square:IDraw,IColor
{
private int sideLengh;
private int locX = 0, locy = 0;
private Rectangle rect = null;

public Square(int sideLength)
{
this.sideLengh = sideLength;
}

void IDraw.SetLocation(int xCoord, int yCoord)
{
// throw new NotImplementedException();
this.locX = xCoord;
this.locy = yCoord;
}

void IDraw.Draw(Canvas canvas)
{
//throw new NotImplementedException();
if (this.rect != null)
{
canvas.Children.Remove(this.rect);
}
else
{
this.rect = new Rectangle();
}
this.rect.Height = this.sideLengh;
this.rect.Width = this.sideLengh;
Canvas.SetTop(this.rect,this.locy);
Canvas.SetLeft(this.rect,this.locX);
canvas.Children.Add(rect);
}


void IColor.SetColor(Color color)
{
//throw new NotImplementedException();
if (rect != null)
{
SolidColorBrush brush = new SolidColorBrush(color);
rect.Fill = brush;
}
}
}
}

画圆类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;

namespace Drawing
{
class Circle:IDraw,IColor
{
private int radius;
private int locX = 0, locY = 0;
private Ellipse circle = null;
public Circle(int radius)
{
this.radius = radius;
}
void IDraw.SetLocation(int xCoord, int yCoord)
{
this.locX = xCoord;
this.locY = yCoord;
}

void IDraw.Draw(Canvas canvas)
{
if (this.circle != null)
{
canvas.Children.Remove(this.circle);
}
else
{
this.circle = new Ellipse();
}

this.circle.Height = this.radius;
this.circle.Width = this.radius;
Canvas.SetTop(this.circle, this.locY);
Canvas.SetLeft(this.circle, this.locX);
canvas.Children.Add(circle);
}
void IColor.SetColor(Color color)
{
if (circle != null)
{
SolidColorBrush brush = new SolidColorBrush(color);
circle.Fill = brush;
}
}
}
}

鼠标事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Drawing
{
public partial class DrawingPadWindow : Window
{
public DrawingPadWindow()
{
InitializeComponent();
}

private void drawingCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)//鼠标点击事件
{
Point mouseLocation=e.GetPosition(this.drawingCanvas);
Square mySquare = new Square(100);
if (mySquare is IDraw)
{
IDraw drawSquare = mySquare;
drawSquare.SetLocation((int)mouseLocation.X,(int)mouseLocation.Y);
drawSquare.Draw(drawingCanvas);

}
if (mySquare is IColor)
{
IColor colorSquare = mySquare;
colorSquare.SetColor(Colors.White);
}

}

private void drawingCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)//鼠标右击事件
{
Point mouseLocation = e.GetPosition(this.drawingCanvas);
Circle myCircle = new Circle(100);
if (myCircle is IDraw)
{
IDraw drawCircle = myCircle;
drawCircle.SetLocation((int)mouseLocation.X, (int)mouseLocation.Y);
drawCircle.Draw(drawingCanvas);
}
if (myCircle is IColor)
{
IColor colorCircle = myCircle;
colorCircle.SetColor(Colors.Red);
}
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值