扩展 Sudoku 对打印当前填数游戏的支持几乎不需要任何额外的工作。例如,您可以选择一个特定键,扩展 PuzzleGrid 上的 OnKeyDown 方法,以在按下此键时执行代码。
using(PrintDialog pd = new PrintDialog())
using(PrintDocument doc = new PrintDocument())
{
doc.PrintPage += new PrintPageEventHandler(HandlePrintPage);
pd.Document = doc;
if (pd.ShowDialog() == DialogResult.OK) doc.Print();
}
然后,您可以使用下列代码处理 PrintPage 事件。
private void Document_PrintPage(object sender, PrintPageEventArgs e)
{
DrawToGraphics(e.Graphics, Bounds);
e.HasMorePages = false;
}
这就是对扩展打印支持所做的全部工作。当然,这不会产生最佳的输出效果。填数游戏的大小将基于 PuzzleGrid 的当前大小,它不会位于页面中央,并且打印的输出将包括像当前建议的单元格之类的内容。此问题可通过创建一个新的完全用于打印支持的 PuzzleGrid 来解决。
private void Document_PrintPage(object sender, PrintPageEventArgs e)
{
using(PuzzleGrid pg = new PuzzleGrid())
{
pg.State = State;
pg.ShowSuggestedCells = false;
pg.ShowIncorrectNumbers = false;
if (e.MarginBounds.Width > e.MarginBounds.Height)
{
pg.Height = e.MarginBounds.Height;
pg.Width = pg.Height;
}
else
{
pg.Width = e.MarginBounds.Width;
pg.Height = pg.Width;
}
using(Matrix m = new Matrix())
{
GraphicsState state = e.Graphics.Save();
m.Translate((e.PageBounds.Width - pg.Width) / 2,
(e.PageBounds.Height - pg.Height) / 2);
e.Graphics.Transform = m;
pg.DrawToGraphics(e.Graphics,
new Rectangle(0, 0, pg.Width, pg.Height));
e.Graphics.Restore(state);
}
}
e.HasMorePages = false;
}
您可以进一步打印出填数游戏的整个集合。每次调用 PrintPage 处理器时,会使用 Generator 类创建一个全新的 PuzzleState(而不是使用当前网格的 PuzzleState,如上例所示),然后使用此代码来打印输出。这样,您就可以打印出您想要的任何数量的填数游戏,将 HasMorePages 设置为 true 直至您打印出所需的数量为止。

RSS订阅