Add Clipboard example

This commit is contained in:
haru-s 2023-03-01 00:04:33 +09:00
parent 9a6395f387
commit fa984dcd11
8 changed files with 318 additions and 0 deletions

16
examples/clipboard/.gitignore vendored Normal file
View file

@ -0,0 +1,16 @@
.dub
docs.json
__dummy.html
docs/
/hello_dfl
hello_dfl.so
hello_dfl.dylib
hello_dfl.dll
hello_dfl.a
hello_dfl.lib
hello_dfl-test-*
*.exe
*.pdb
*.o
*.obj
*.lst

13
examples/clipboard/.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (Windows) clipboard",
"type": "cppvsdbg",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "./bin/clipboard_sample.exe",
"console": "internalConsole"
}
]
}

20
examples/clipboard/.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,20 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "dub",
"run": false,
"cwd": ".",
"compiler": "$current",
"archType": "$current",
"buildType": "$current",
"configuration": "$current",
"problemMatcher": [
"$dmd"
],
"group": "build",
"label": "dub: Build clipboard_sample",
"detail": "dub build --compiler=dmd.EXE -a=x86_64 -b=debug -c=application"
}
]
}

View file

@ -0,0 +1,5 @@
# Screen Shot
![screen shot](./image/screenshot1.png "screen shot")
![screen shot](./image/screenshot2.png "screen shot")
![screen shot](./image/screenshot3.png "screen shot")
![screen shot](./image/screenshot4.png "screen shot")

View file

@ -0,0 +1,12 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"d.projectImportPaths": [
"..\\..\\..\\dfl\\source"
]
}
}

View file

@ -0,0 +1,16 @@
{
"authors": ["haru-s"],
"copyright": "Copyright (C) 2023 haru-s",
"description": "DFL sample code.",
"name": "clipboard_sample",
"targetType": "executable",
"targetPath": "bin",
"dependencies": {
"dfl": {
"path": "../../../dfl"
}
},
"lflags-windows-x86_omf-dmd": ["/exet:nt/su:windows:6.0"],
"lflags-windows-x86_mscoff-dmd": ["/SUBSYSTEM:WINDOWS", "/ENTRY:mainCRTStartup"],
"lflags-windows-x86_64-dmd": ["/SUBSYSTEM:WINDOWS", "/ENTRY:mainCRTStartup"]
}

View file

@ -0,0 +1,3 @@
set dmd_path=c:\d\dmd2\windows
set dmc_path=c:\dmc\dm
cmd

View file

@ -0,0 +1,233 @@
import dfl;
import dfl.internal.utf;
import dfl.internal.dlib;
import std.utf;
version(Have_dfl) // For DUB.
{
}
else
{
pragma(lib, "dfl.lib");
}
class MainForm : Form
{
private Panel _leftSide;
private Panel _rightSide;
private TextBox _textbox;
private PictureBox _picturebox;
private Button _copy;
private Button _copyBitmap;
private Button _paste;
private Button _clear;
private Button _formats;
private Button _flush;
public this()
{
this.text = "Clipboard example";
this.size = Size(500, 400);
_leftSide = new Panel();
_leftSide.dock = DockStyle.LEFT;
_leftSide.width = 200;
_leftSide.parent = this;
_rightSide = new Panel();
_rightSide.dock = DockStyle.FILL;
_rightSide.parent = this;
_picturebox = new PictureBox();
_picturebox.parent = _rightSide;
_picturebox.dock = DockStyle.TOP;
_picturebox.height = 100;
_picturebox.backColor = Color(255,255,255);
_textbox = new TextBox();
_textbox.parent = _rightSide;
_textbox.multiline = true;
_textbox.dock = DockStyle.FILL;
_textbox.wordWrap = false;
_textbox.scrollBars = ScrollBars.BOTH;
_copy = new Button();
_copy.parent = _leftSide;
_copy.text = "copy from textbox";
_copy.dock = DockStyle.TOP;
_copy.click ~= (Control c, EventArgs e)
{
_picturebox.image = null;
static if (1) // BUG: workaround
{
Clipboard.setString(_textbox.text, false);
}
else
{
// TODO: Don't work
Clipboard.setData(DataFormats.stringFormat, new Data(_textbox.text));
}
};
_copyBitmap = new Button();
_copyBitmap.parent = _leftSide;
_copyBitmap.text = "copy from sample bitmap";
_copyBitmap.dock = DockStyle.TOP;
_copyBitmap.click ~= (Control c, EventArgs e)
{
_textbox.clear();
Bitmap bitmap = new Bitmap(r".\image\sample.bmp");
static if (1) // BUG: workaround
{
import core.sys.windows.winuser;
OpenClipboard(null);
EmptyClipboard();
SetClipboardData(CF_BITMAP, bitmap.handle);
CloseClipboard();
}
else
{
Clipboard.setImage(bitmap, false); // TODO: Don't work
}
_picturebox.image = bitmap;
};
_paste = new Button();
_paste.parent = _leftSide;
_paste.text = "paste to textbox/picturebox";
_paste.dock = DockStyle.TOP;
_paste.click ~= (Control c, EventArgs e)
{
IDataObject dataObj = Clipboard.getDataObject();
// bitmap
if (Clipboard.containsImage())
{
Data data = dataObj.getData(DataFormats.bitmap, false);
Image image = data.getImage();
if (image !is null)
{
_textbox.clear();
_textbox.appendText = "Read as bitmap\r\n";
_textbox.appendText = "---\r\n";
_picturebox.image = image;
return;
}
}
// file drop (When select files and input ctrl+C on Explorer)
if (Clipboard.containsFileDropList())
{
Data data = dataObj.getData(DataFormats.fileDrop, false);
string[] fileDropList = data.getStrings();
if (fileDropList !is null)
{
_textbox.clear();
_textbox.appendText = "Read as FileDrop\r\n";
_textbox.appendText = "---\r\n";
_picturebox.image = null;
string result;
foreach (string item; fileDropList)
{
result ~= item;
result ~= "\r\n";
}
_textbox.appendText = result;
return;
}
}
// utf8 text
if (Clipboard.containsString())
{
string utf8Str = Clipboard.getString();
if (utf8Str !is null)
{
_textbox.clear();
_textbox.appendText = "Read as UTF-8 string\r\n";
_textbox.appendText = "---\r\n";
_textbox.appendText = utf8Str;
return;
}
}
// unicode text (utf16)
if (Clipboard.containsData(DataFormats.unicodeText))
{
Data data = Clipboard.getData(DataFormats.unicodeText);
wstring str = data.getUnicodeText();
if (str !is null)
{
_textbox.clear();
_textbox.appendText = "Read as UnicodeText\r\n";
_textbox.appendText = "---\r\n";
_textbox.appendText = toUTF8(str);
return;
}
}
// ansi text
if (Clipboard.containsText())
{
enum ubyte[] UBYTE_ZERO = [0];
ubyte[] ansiStrz = Clipboard.getText() ~ UBYTE_ZERO; // Add \0 terminal
if (ansiStrz !is null)
{
_textbox.clear();
_textbox.appendText = "Read as AnsiText\r\n";
_textbox.appendText = "---\r\n";
_textbox.appendText = dfl.internal.utf.fromAnsiz(cast(char*)ansiStrz.ptr);
return;
}
}
};
_clear = new Button();
_clear.parent = _leftSide;
_clear.text = "clear previews and clipboard";
_clear.dock = DockStyle.TOP;
_clear.click ~= (Control c, EventArgs e)
{
_textbox.clear();
_picturebox.image = null;
Clipboard.clear();
};
_formats = new Button();
_formats.parent = _leftSide;
_formats.text = "show formats on clipboard";
_formats.dock = DockStyle.TOP;
_formats.click ~= (Control c, EventArgs e)
{
_textbox.clear();
IDataObject dataObj = Clipboard.getDataObject();
foreach (string f; dataObj.getFormats())
{
_textbox.appendText = "[";
_textbox.appendText = f;
_textbox.appendText = "]";
_textbox.appendText = "\r\n";
}
};
_flush = new Button();
_flush.parent = _leftSide;
_flush.text = "flush clipboard";
_flush.dock = DockStyle.TOP;
_flush.click ~= (Control c, EventArgs e)
{
// NOTE: Call after Clipboard.setDataObject().
Clipboard.flush();
};
}
}
static this()
{
Application.enableVisualStyles();
}
void main()
{
Application.run(new MainForm());
}