Write native GUI applications for Windows
Mike Parker
aldacron at gmail.com
Mon Dec 18 09:45:38 UTC 2017
On Monday, 18 December 2017 at 07:55:25 UTC, Andrey wrote:
> Hello!
> I have a question about creating native GUI applications for
> Windows 7 or/and Windows 10.
> I know that exist DWT, DlangUI and other... But I'm interesting
> in native GUI. If it will be C++ then I would use WinAPI from
> SDK.
> And what about D? What should I do? Make some kind of wrapper
> above C WinApi?
You can use the C Windows API out of the box:
import core.sys.windows.windows;
// And away you go
> I also know that on GitHub exists such wrapper
> (https://github.com/AndrejMitrovic/DWinProgramming) but it is
> old - last commit was 4 years ago.
>
The purpose of that repository was to port Petzold's examples to
D. The Win32 API bindings (it's not a wrapper) were included to
make building easier. I'm pretty sure somebody else set up a
github mirror of the original DSource.org repository and Andrej
simply copied it into his directory tree under the WindowsAPI
directory. Meaning, I don't think the purpose of DWinProgramming
was ever to actually host Windows bindings.
Regardless, third-party bindings are no longer necessary. All the
examples in that repository should work (perhaps with minor
changes) if you replace the win32.* imports with
core.sys.windows. For example, the HelloMsg sample in the Chapter
01 folder could use a couple f changes. toUTF16z is now part of
std.utf, so no need for a custom implementation. And the versions
of Runtime.initialize/terminate that take a delegate are
deprecated. So this is what it should look like now:
module HelloMsg;
import core.runtime;
import std.utf;
import core.sys.windows.windows;
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int iCmdShow)
{
int result;
try
{
Runtime.initialize();
result = myWinMain(hInstance, hPrevInstance, lpCmdLine,
iCmdShow);
Runtime.terminate();
}
catch (Throwable o)
{
MessageBox(null, o.toString().toUTF16z, "Error", MB_OK |
MB_ICONEXCLAMATION);
result = 0;
}
return result;
}
int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int iCmdShow)
{
MessageBox(NULL, "Hello, Windows!", "Your Application", 0);
return 0;
}
On the other hand, you can dispense with the WinMain nonsense
just do this:
import core.runtime;
import std.utf;
import core.sys.windows.windows;
void main()
{
MessageBox(null, "Hello, Windows!", "Your Application", 0);
}
Then compile with this to the same behavior as WinMain (i.e. no
console window attached to the program):
dmd -L/SUBSYSTEM:windows HelloMsg.d
Or, when using the MS linker:
dmd -L/SUBSYSTEM:windows -L/ENTRY:mainCRTStartup -m64 HelloMsg.d
user32.lib
Replace -m64 with -m32mscoff for 32-bit COFF output. Also note
that when using the MS linker, you have to explicitly link with
user32.lib. The same is true for the original WinMain version.
More information about the Digitalmars-d-learn
mailing list