Permalink
Browse files

Merge pull request #127 from AndrejMitrovic/SampleFixes

Updated dhry and winsamp examples. See issue 6158.
  • Loading branch information...
WalterBright committed Jun 18, 2011
2 parents 753f178 + 4cda0dc commit 5315dcd82a74c196e21887212bb58a0327d75235
Showing with 112 additions and 106 deletions.
  1. +3 −1 samples/dhry.d
  2. +109 −105 samples/winsamp.d
@@ -738,7 +738,7 @@ void Proc_6(Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par)
/* then, not executed */
*Enum_Ref_Par = Ident_4;
final switch (Enum_Val_Par)
switch (Enum_Val_Par)
{
case Ident_1:
*Enum_Ref_Par = Ident_1;
@@ -763,6 +763,8 @@ void Proc_6(Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par)
case Ident_5:
*Enum_Ref_Par = Ident_3;
break;
default:
} /* switch */
} /* Proc_6 */
@@ -1,64 +1,155 @@
module winsamp;
/* Compile with:
* dmd winsamp gdi32.lib winsamp.def
*/
/+ Compile with:
+ dmd winsamp winsamp.def
+ or:
+ dmd winsamp -L-Subsystem:Windows
+/
pragma(lib, "gdi32.lib");
import core.runtime;
import std.c.windows.windows;
import std.c.stdio;
import std.string;
const int IDC_BTNCLICK = 101;
const int IDC_BTNDONTCLICK = 102;
enum IDC_BTNCLICK = 101;
enum IDC_BTNDONTCLICK = 102;
extern (Windows)
int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
switch (uMsg)
int result;
void exceptionHandler(Throwable e) { throw e; }
try
{
Runtime.initialize(&exceptionHandler);
result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
Runtime.terminate(&exceptionHandler);
}
catch (Throwable e)
{
MessageBoxA(null, e.toString().toStringz, "Error", MB_OK | MB_ICONEXCLAMATION);
result = 0;
}
return result;
}
int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
string caption = "The Hello Program";
string className = "DWndClass";
HWND hWnd, btnClick, btnDontClick;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = &WindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIconA(null, IDI_APPLICATION);
wndclass.hCursor = LoadCursorA(null, IDC_CROSS);
wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = null;
wndclass.lpszClassName = className.toStringz;
if (!RegisterClassA(&wndclass))
{
MessageBoxA(null, "Couldn't register Window Class!", caption.toStringz, MB_ICONERROR);
return 0;
}
hWnd = CreateWindowA(className.toStringz, // window class name
caption.toStringz, // window caption
WS_THICKFRAME |
WS_MAXIMIZEBOX |
WS_MINIMIZEBOX |
WS_SYSMENU |
WS_VISIBLE, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
600, // initial x size
400, // initial y size
HWND_DESKTOP, // parent window handle
null, // window menu handle
hInstance, // program instance handle
null); // creation parameters
if (hWnd is null)
{
MessageBoxA(null, "Couldn't create window.", caption.toStringz, MB_ICONERROR);
return 0;
}
btnClick = CreateWindowA("BUTTON", "Click Me", WS_CHILD | WS_VISIBLE,
0, 0, 100, 25, hWnd, cast(HMENU)IDC_BTNCLICK, hInstance, null);
btnDontClick = CreateWindowA("BUTTON", "DON'T CLICK!", WS_CHILD | WS_VISIBLE,
110, 0, 100, 25, hWnd, cast(HMENU)IDC_BTNDONTCLICK, hInstance, null);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessageA(&msg, null, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
return msg.wParam;
}
int* p;
extern(Windows)
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_BTNCLICK:
if (HIWORD(wParam) == BN_CLICKED)
MessageBoxA(hWnd, "Hello, world!", "Greeting",
MB_OK | MB_ICONINFORMATION);
break;
case IDC_BTNDONTCLICK:
if (HIWORD(wParam) == BN_CLICKED)
{
MessageBoxA(hWnd, "You've been warned...", "Prepare to GP fault",
MB_OK | MB_ICONEXCLAMATION);
*(cast(int*) null) = 666;
*p = 1;
}
break;
default:
break;
}
break;
}
case WM_PAINT:
{
static string text = "D Does Windows";
enum text = "D Does Windows";
PAINTSTRUCT ps;
HDC dc = BeginPaint(hWnd, &ps);
scope(exit) EndPaint(hWnd, &ps);
RECT r;
GetClientRect(hWnd, &r);
HFONT font = CreateFontA(80, 0, 0, 0, FW_EXTRABOLD, FALSE, FALSE,
FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial");
HGDIOBJ old = SelectObject(dc, cast(HGDIOBJ) font);
SetTextAlign(dc, TA_CENTER | TA_BASELINE);
TextOutA(dc, r.right / 2, r.bottom / 2, text.ptr, text.length);
SelectObject(dc, old);
EndPaint(hWnd, &ps);
TextOutA(dc, r.right / 2, r.bottom / 2, text.toStringz, text.length);
DeleteObject(SelectObject(dc, old));
break;
}
@@ -70,92 +161,5 @@ int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
break;
}
return DefWindowProcA(hWnd, uMsg, wParam, lParam);
}
int doit()
{
HINSTANCE hInst = GetModuleHandleA(null);
WNDCLASS wc;
wc.lpszClassName = "DWndClass";
wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = &WindowProc;
wc.hInstance = hInst;
wc.hIcon = LoadIconA(cast(HINSTANCE) null, IDI_APPLICATION);
wc.hCursor = LoadCursorA(cast(HINSTANCE) null, IDC_CROSS);
wc.hbrBackground = cast(HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = null;
wc.cbClsExtra = wc.cbWndExtra = 0;
auto a = RegisterClassA(&wc);
assert(a);
HWND hWnd, btnClick, btnDontClick;
hWnd = CreateWindowA("DWndClass", "Just a window", WS_THICKFRAME |
WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, HWND_DESKTOP,
cast(HMENU) null, hInst, null);
assert(hWnd);
btnClick = CreateWindowA("BUTTON", "Click Me", WS_CHILD | WS_VISIBLE,
0, 0, 100, 25, hWnd, cast(HMENU) IDC_BTNCLICK, hInst, null);
btnDontClick = CreateWindowA("BUTTON", "DON'T CLICK!", WS_CHILD | WS_VISIBLE,
110, 0, 100, 25, hWnd, cast(HMENU) IDC_BTNDONTCLICK, hInst, null);
MSG msg;
while (GetMessageA(&msg, cast(HWND) null, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
return 1;
}
/**********************************************************/
/* Note the similarity of this code to the console D startup
* code in \dmd\src\phobos\dmain2.d
* You'll also need a .def file with at least the following in it:
* EXETYPE NT
* SUBSYSTEM WINDOWS
*/
extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
// ~ extern (C) void _moduleUnitTests(); //~ errors out
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int result;
gc_init(); // initialize garbage collector
_minit(); // initialize module constructor table
try
{
_moduleCtor(); // call module constructors
// ~ _moduleUnitTests(); // run unit tests (optional) //~ errors out
result = doit(); // insert user code here
}
catch (Exception e) // catch any uncaught exceptions
{
MessageBoxA(null, cast(char *) e.toString(), "Error",
MB_OK | MB_ICONEXCLAMATION);
result = 0; // failed
}
gc_term(); // run finalizers; terminate garbage collector
return result;
return DefWindowProcA(hWnd, message, wParam, lParam);
}

0 comments on commit 5315dcd

Please sign in to comment.