Update commondialog example for printing module

This commit is contained in:
haru-s 2024-02-25 16:41:27 +09:00
parent d30fa31ecd
commit 0d436d2757
5 changed files with 128 additions and 107 deletions

View file

@ -10,6 +10,7 @@ DFL is a Win32 windowing library for the D language.
![screen shot](./examples/splitter/image/screenshot.png "screen shot")
![screen shot](./examples/imagelist/image/screenshot.png "screen shot")
![screen shot](./examples/commondialog/image/screenshot.png "screen shot")
![screen shot](./examples/commondialog/image/screenshot2.png "screen shot")
![screen shot](./examples/tooltip/image/screenshot.png "screen shot")
![screen shot](./examples/progressbar/image/screenshot3.png "screen shot")
![screen shot](./examples/clipboard/image/screenshot.png "screen shot")
@ -22,7 +23,10 @@ DFL is a Win32 windowing library for the D language.
![screen shot](./examples/richtextbox/image/screenshot.png "screen shot")
## Recent major features
- **PrintDialog and PrintSetupDialog is now support (Experimental).**
- **Module "dfl.printing" is now comming.**
- **PrintDialog**
- **PrintSetupDialog**
- **PrintPreviewDialog**
- DUB is available for DFL.
- Remove dflexe.
- Remove GTK-based DFL.

View file

@ -1,2 +1,3 @@
# Screen Shot
![screen shot](./image/screenshot.png "screen shot")
![screen shot](./image/screenshot2.png "screen shot")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Before After
Before After

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 KiB

View file

@ -20,6 +20,7 @@ class MainForm : Form
private Button _colorButton;
private Button _printButton;
private Button _pageSetupButton;
private Button _printPreviewButton;
private OpenFileDialog _openFileDialog;
private SaveFileDialog _saveFileDialog;
@ -30,6 +31,7 @@ class MainForm : Form
private PrintDocument _document;
private PrintDialog _printDialog;
private PageSetupDialog _pageSetupDialog;
private PrintPreviewDialog _previewDialog;
private void doOpenFileDialog(Control sender, EventArgs e)
{
@ -189,112 +191,6 @@ class MainForm : Form
private void doPrintDialog(Control sender, EventArgs e)
{
_printDialog.document.printRange ~= (PrintDocument doc, PrintRangeEventArgs e) {
final switch (e.printRange.kind)
{
case PrintRangeKind.ALL_PAGES:
e.printRange.addPrintRange(PrintRange(1, 2));
break;
case PrintRangeKind.SELECTION:
e.printRange.addPrintRange(PrintRange(1, 1));
break;
case PrintRangeKind.CURRENT_PAGE:
e.printRange.addPrintRange(PrintRange(2, 2));
break;
case PrintRangeKind.SOME_PAGES:
// The page range is determined by the printer dialog, so we don't do anything here.
}
};
_printDialog.document.beginPrint ~= (PrintDocument doc, PrintEventArgs e) {
// Do something.
};
_printDialog.document.queryPageSettings ~= (PrintDocument doc, QueryPageSettingsEventArgs e) {
// User modify page settings here.
// TODO: Paper orientation cannot be changed.
};
_printDialog.document.printPage ~= (PrintDocument doc, PrintPageEventArgs e) {
Graphics g = e.graphics;
int dpiX = e.pageSettings.printerResolution.x; // dpi unit.
int dpiY = e.pageSettings.printerResolution.y; // dpi unit.
// Draw margin border to all pages.
Rect marginRect = Rect(
e.marginBounds.x * dpiX / 100, // e.marginBounds is 1/100 dpi unit.
e.marginBounds.y * dpiY / 100,
e.marginBounds.width * dpiX / 100,
e.marginBounds.height * dpiY / 100);
g.drawRectangle(new Pen(Color.green, 10), marginRect);
if (e.currentPage == 1) // Draw page 1.
{
string str =
"PrintDcoument.DocumentName: " ~ to!string(doc.documentName) ~ "\n\n" ~
"PrintDcoument.defaultPageSettings: " ~ to!string(doc.printerSettings.defaultPageSettings) ~ "\n\n" ~
"PrintDcoument.printerSettings: " ~ to!string(doc.printerSettings) ~ "\n\n" ~
"PrintPageEventArgs.pageSettings: " ~ to!string(e.pageSettings) ~ "\n\n" ~
"PrintPageEventArgs.pageBounds: " ~ to!string(e.pageBounds) ~ "\n\n" ~
"PrintPageEventArgs.marginBounds: " ~ to!string(e.marginBounds);
Rect paramPrintRect = Rect(
e.marginBounds.x * dpiX / 100, // e.marginBounds is 1/100 dpi unit.
e.marginBounds.y * dpiY / 100,
e.marginBounds.width * dpiX / 100,
e.marginBounds.height * dpiY / 100
);
g.drawText(
str,
new Font("MS Gothic", 8/+pt+/ * dpiX / 72),
Color.black,
paramPrintRect
);
e.hasMorePage = true;
}
else if (e.currentPage == 2) // Draw page 2.
{
Rect redRect = Rect(1 * dpiX, 1 * dpiY, 1 * dpiX, 1 * dpiY); // 1 x 1 inch.
redRect.offset(marginRect.x, marginRect.y);
g.fillRectangle(new SolidBrush(Color.red), redRect);
Rect blueRect = Rect(dpiX, dpiY, 3 * dpiX, 3 * dpiY); // 3 x 3 inch.
blueRect.offset(marginRect.x, marginRect.y);
g.drawRectangle(new Pen(Color.blue, 10), blueRect);
Rect textRect = Rect(1 * dpiX, 1 * dpiY, 1 * dpiX, 1 * dpiY); // 1 x 1 inch.
textRect.offset(marginRect.x, marginRect.y);
g.drawText(
"ABCDEあいうえお",
new Font("MS Gothic", 12/+pt+/ * dpiX / 72),
Color.black,
textRect
);
Rect purpleRect = Rect(3 * dpiX, 3 * dpiY, 1 * dpiX, 1 * dpiY); // 1 x 1 inch.
purpleRect.offset(marginRect.x, marginRect.y);
g.drawEllipse(new Pen(Color.purple, 10), purpleRect);
Pen pen = new Pen(Color.black, 10);
enum lineNum = 20;
for (int x; x < lineNum; x++)
{
g.drawLine(
pen,
marginRect.x + cast(int)(x / 4.0 * dpiX),
e.marginBounds.y * dpiY / 100,
marginRect.x + cast(int)((lineNum - x - 1)/4.0 * dpiX),
e.marginBounds.bottom * dpiY / 100);
}
e.hasMorePage = false;
}
};
_printDialog.document.endPrint ~= (PrintDocument doc, PrintEventArgs e) {
// Do nothing.
};
_printDialog.allowSomePages = true;
_printDialog.showHelp = true;
@ -325,6 +221,15 @@ class MainForm : Form
}
}
private void doPrintPreviewDialog(Control sender, EventArgs e)
{
DialogResult r = _previewDialog.showDialog();
if (r == DialogResult.OK)
{
// Do nothing.
}
}
public this()
{
this.text = "Common dialogs example";
@ -345,6 +250,7 @@ class MainForm : Form
_document = new PrintDocument();
_printDialog = new PrintDialog(_document);
_pageSetupDialog = new PageSetupDialog(_document);
_previewDialog = new PrintPreviewDialog(_document);
with(_openButton = new Button())
{
@ -402,6 +308,116 @@ class MainForm : Form
size = Size(100, 23);
click ~= &doPageSetupDialog;
}
with(_printPreviewButton = new Button())
{
parent = this;
text = "Print Preview";
location = Point(10, 220);
size = Size(100, 23);
click ~= &doPrintPreviewDialog;
}
_document.printRange ~= (PrintDocument doc, PrintRangeEventArgs e) {
final switch (e.printRange.kind)
{
case PrintRangeKind.ALL_PAGES:
e.printRange.addPrintRange(PrintRange(1, 2));
break;
case PrintRangeKind.SELECTION:
e.printRange.addPrintRange(PrintRange(1, 1));
break;
case PrintRangeKind.CURRENT_PAGE:
e.printRange.addPrintRange(PrintRange(2, 2));
break;
case PrintRangeKind.SOME_PAGES:
// The page range is determined by the printer dialog, so we don't do anything here.
}
};
_document.beginPrint ~= (PrintDocument doc, PrintEventArgs e) {
// Do something.
};
_document.queryPageSettings ~= (PrintDocument doc, QueryPageSettingsEventArgs e) {
// User modify page settings here.
// TODO: Paper orientation cannot be changed.
};
_document.printPage ~= (PrintDocument doc, PrintPageEventArgs e) {
Graphics g = e.graphics;
int dpiX = e.pageSettings.printerResolution.x; // dpi unit.
int dpiY = e.pageSettings.printerResolution.y; // dpi unit.
// Draw margin border to all pages.
Rect marginRect = Rect(
e.marginBounds.x * dpiX / 100, // e.marginBounds is 1/100 dpi unit.
e.marginBounds.y * dpiY / 100,
e.marginBounds.width * dpiX / 100,
e.marginBounds.height * dpiY / 100);
g.drawRectangle(new Pen(Color.green, 10), marginRect);
if (e.currentPage == 1) // Draw page 1.
{
string str =
"PrintDcoument.DocumentName: " ~ to!string(doc.documentName) ~ "\n\n" ~
"PrintDcoument.defaultPageSettings: " ~ to!string(doc.printerSettings.defaultPageSettings) ~ "\n\n" ~
"PrintDcoument.printerSettings: " ~ to!string(doc.printerSettings) ~ "\n\n" ~
"PrintPageEventArgs.pageSettings: " ~ to!string(e.pageSettings) ~ "\n\n" ~
"PrintPageEventArgs.pageBounds: " ~ to!string(e.pageBounds) ~ "\n\n" ~
"PrintPageEventArgs.marginBounds: " ~ to!string(e.marginBounds);
Rect paramPrintRect = Rect(
e.marginBounds.x * dpiX / 100, // e.marginBounds is 1/100 dpi unit.
e.marginBounds.y * dpiY / 100,
e.marginBounds.width * dpiX / 100,
e.marginBounds.height * dpiY / 100
);
g.drawText(
str,
new Font("MS Gothic", 8/+pt+/ * dpiX / 72),
Color.black,
paramPrintRect
);
}
else if (e.currentPage == 2) // Draw page 2.
{
Rect redRect = Rect(1 * dpiX, 1 * dpiY, 1 * dpiX, 1 * dpiY); // 1 x 1 inch.
redRect.offset(marginRect.x, marginRect.y);
g.fillRectangle(new SolidBrush(Color.red), redRect);
Rect blueRect = Rect(dpiX, dpiY, 3 * dpiX, 3 * dpiY); // 3 x 3 inch.
blueRect.offset(marginRect.x, marginRect.y);
g.drawRectangle(new Pen(Color.blue, 10), blueRect);
Rect textRect = Rect(1 * dpiX, 1 * dpiY, 1 * dpiX, 1 * dpiY); // 1 x 1 inch.
textRect.offset(marginRect.x, marginRect.y);
g.drawText(
"ABCDEあいうえお",
new Font("MS Gothic", 12/+pt+/ * dpiX / 72),
Color.black,
textRect
);
Rect purpleRect = Rect(3 * dpiX, 3 * dpiY, 1 * dpiX, 1 * dpiY); // 1 x 1 inch.
purpleRect.offset(marginRect.x, marginRect.y);
g.drawEllipse(new Pen(Color.purple, 10), purpleRect);
Pen pen = new Pen(Color.black, 10);
enum lineNum = 20;
for (int x; x < lineNum; x++)
{
g.drawLine(
pen,
marginRect.x + cast(int)(x / 4.0 * dpiX),
e.marginBounds.y * dpiY / 100,
marginRect.x + cast(int)((lineNum - x - 1)/4.0 * dpiX),
e.marginBounds.bottom * dpiY / 100);
}
}
};
_document.endPrint ~= (PrintDocument doc, PrintEventArgs e) {
// Do nothing.
};
}
}