终于试成功了。总结一下:
一.在*.RC里包含windows头文件,因为不用系统预定义常量的机会不多
#include <windows.h>
二.直接在*.RC里定义自定义常量
#define IDD_MAIN
#define IDC_TEXT1 102
等等
三.用Walter的 rcc.exe编译*rc
rcc.exe -32 myresource.rc
生成myresource.res
四.在D源文件里还要定义一次资源中的常量:
const int IDD_MAIN=101;
const int IDC_TEXT1=102;
等等
五.编译D:
bud -O -release -gui -clean testd.d testd.res
生成testd.exe
以下是测试程序
testd.rc:
#include <windows.h>
#define IDD_MAIN 101
#define IDC_TEXT1 102
IDD_MAIN DIALOG 129, 26, 180, 70
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Show Info"
FONT 8, "MS Sans Serif"
BEGIN
EDITTEXT IDC_TEXT1, 19, 15, 147, 12, ES_AUTOHSCROLL
PUSHBUTTON "&Quit", IDCANCEL, 107, 42, 40, 14
PUSHBUTTON "&OK", IDOK, 28, 43, 40, 14
END
D源文件:testd.d:
module testd;
//import std.c.windows.windows;
import win32.windows;
import std.string;
import std.conv;
import std.stdio;
import core.runtime;
import core.stdc.stdlib;
import core.stdc.stdio;
const int IDD_MAIN=101;
const int IDC_TEXT1=102;
extern(Windows)
int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
int retval;
Runtime.initialize;
runModuleUnitTests;
WNDCLASSEX wc;
wc.cbSize=wc.sizeof;
wc.lpfnWndProc=&DefDlgProc;
wc.cbWndExtra=DLGWINDOWEXTRA;
wc.hInstance=hInstance;
wc.hCursor=LoadCursor(null,IDC_ARROW);
wc.hbrBackground=cast(HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName=toStringz("dialog");
if(!RegisterClassEx(&wc))
{
MessageBox(null,toStringz("Register class failed"),toStringz("failed"),MB_OK|MB_ICONERROR);
return -1;
}
retval= DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), null, &DialogFunc);
// run finalizers; terminate garbage collector
Runtime.terminate;
return retval;
}
extern(Windows)
int DialogFunc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
{
SetFocus(GetDlgItem(hwnd,IDC_TEXT1));
EnableWindow(GetDlgItem(hwnd,IDOK),0);
}
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_TEXT1:
{
switch(HIWORD(wParam))
{
case EN_CHANGE:
{
int nLength=GetWindowTextLength(GetDlgItem(hwnd,IDC_TEXT1));
EnableWindow(GetDlgItem(hwnd,IDOK),nLength>0);
}
break;
default:
break;
}
}
break;
case IDOK:
{
char[1024] prompt;
GetDlgItemText(hwnd,IDC_TEXT1,cast(char*)prompt,cast(char*)prompt.sizeof);
MessageBox(null,cast(char*)prompt,"text in textbox is",0);
}
return 1;
case IDCANCEL:
{
int retval=MessageBox(null,"Are you sure to close the dialog?",
"Confirm on close",MB_YESNO|MB_ICONQUESTION);
if(retval==IDYES)
EndDialog(hwnd,0);
}
return 1;
default:
break;
}
break;
case WM_CLOSE:
EndDialog(hwnd,0);
return 1;
default:
break;
//assert(0);
}
return 0;
}
附程序执行时屏幕截图。