110,537
社区成员
发帖
与我相关
我的任务
分享
<Page x:Class="WpfBrowserApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:local="clr-namespace:WpfBrowserApplication1"
d:DesignHeight="700" d:DesignWidth="1000"
Title="Page1">
</Page>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Data;
namespace WpfBrowserApplication1
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
DrawingVisual dv = new DrawingVisual();
DataTable dt = new DataTable();
public Page1()
{
this.VisualEdgeMode = EdgeMode.Aliased;
dt.Columns.Add("A", typeof(int));
dt.Columns.Add("B", typeof(DateTime));
for (int i = 0; i < 60; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = DateTime.Now.AddDays(-14).AddDays(i % 31);
dt.Rows.Add(dr);
}
dt.AcceptChanges();
InitializeComponent();
Pen blackp = new Pen(Brushes.Black, 1);
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("zh-cn");
FlowDirection fd = FlowDirection.LeftToRight;
Typeface tf = new Typeface("宋体");
double d = 12;
using (DrawingContext dc = dv.RenderOpen())
{
for (int i = 0; i < 5; i++)
{
dc.DrawRectangle(null, blackp, new Rect(217 * i, 0, 217, 30));
dc.DrawText(new FormattedText("第" + (i + 1).ToString() + "周", ci, fd, tf, d, Brushes.Black), new Point(217 * i + 70, 10));
}
for (int i = 0; i < 35; i++)
dc.DrawRectangle(null, blackp, new Rect(31 * i, 30, 31, 30));
int w = new DateTime(2011, 3, 1).DayOfWeek.GetHashCode();
for(int i = w;i<w + 31;i++)
dc.DrawText(new FormattedText((i - w + 1).ToString(), ci, fd, tf, d, Brushes.Black), new Point(31 * i + 5, 40));
for (int i = 0; i < 35; i++)
dc.DrawRectangle(null, blackp, new Rect(31 * i, 60, 31, 60));
for (int i = 0; i < dt.Rows.Count; i++)
{
int week = ((DateTime)dt.Rows[i][1]).DayOfWeek.GetHashCode();
int day = ((DateTime)dt.Rows[i][1]).Day;
string s = dt.Rows[i][0].ToString();
double left = (double)(week + (day + week / 7 * 7 + 1) * 31 + 5);
double top = i > 30 ? 100 : 70;
dc.DrawText(new FormattedText(s, ci, fd, tf, d, Brushes.Black), new Point(left, top));
}
}
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
protected override Visual GetVisualChild(int index)
{
return dv;
}
}
}