#include <tchar.h>
#include <Windows.h>
#include <windowsx.h>
#define ID_HELP 150
#define ID_TEXT 200
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int nLen;
int wmId, wmEvent;
LPTSTR lpstr;
switch (uMsg)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg, wmId);
break;
case ID_HELP:
MessageBox(hDlg, TEXT("帮助内容"), TEXT("帮助标题"), MB_ICONINFORMATION);
break;
case ID_TEXT:
if (wmEvent == STN_CLICKED)
{
nLen = Static_GetTextLength((HWND)lParam);
lpstr = (LPTSTR)malloc((nLen + 1) * sizeof(TCHAR));
Static_GetText((HWND)lParam, lpstr, nLen + 1);
MessageBox(hDlg, lpstr, TEXT("文本内容"), MB_ICONINFORMATION);
free(lpstr);
}
}
break;
}
return FALSE;
}
// 使地址能被32整除
void align32(LPWORD *ptr)
{
//*ptr = (LPWORD)((((ULONG)*ptr + 3) >> 2) << 2);
ULONG ul;
ul = (ULONG)*ptr;
ul += 3;
ul >>=2;
ul <<=2;
*ptr = (LPWORD)ul;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
LPWORD p;
struct
{
DLGTEMPLATE tpl;
WORD menu;
WORD cls;
WCHAR title[6];
// 剩下的内容无法在结构体中表示, 因为无法确保地址能够被32整除
BYTE controls[1000];
} dlg;
struct
{
DLGITEMTEMPLATE item;
WORD menu;
WORD cls;
WCHAR text[3];
WORD creation_data;
} btn;
// 对话框样式
dlg.tpl.style = WS_POPUP | WS_BORDER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | DS_CENTER | DS_MODALFRAME; // 对话框样式
dlg.tpl.dwExtendedStyle = (DWORD)NULL; // 对话框扩展样式
dlg.tpl.cdit = 1; // 控件数量
dlg.tpl.x = dlg.tpl.y = 0; // 对话框坐标, 单位: DU (对话框单位, Dialog Box Unit), 由于已在style中指定了DS_CENTER, 所以居中显示
dlg.tpl.cx = 100; // 对话框宽度 (DU)
dlg.tpl.cy = 100; // 对话框高度 (DU)
dlg.menu = dlg.cls = (WORD)NULL; // 无菜单、对话框类
lstrcpyW(dlg.title, L"示例对话框"); // 对话框标题, 必须为宽字符串
// 确定按钮
btn.item.style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;
btn.item.dwExtendedStyle = (DWORD)NULL;
btn.item.x = 10;
btn.item.y = 70;
btn.item.cx = 80;
btn.item.cy = 20;
btn.item.id = IDOK;
btn.menu = 0xffff;
btn.cls = 0x80;
lstrcpyW(btn.text, L"确定");
btn.creation_data = (WORD)NULL;
ZeroMemory(&dlg.controls, sizeof(dlg.controls));
p = (LPWORD)&dlg.controls;
align32(&p); // 使地址能够被32整除
memcpy(p, &btn, sizeof(btn));
p += sizeof(btn);
// 帮助按钮
btn.item.style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;
btn.item.dwExtendedStyle = (DWORD)NULL;
btn.item.x = 55;
btn.item.y = 10;
btn.item.cx = 40;
btn.item.cy = 20;
btn.item.id = ID_HELP;
btn.menu = 0xffff;
btn.cls = 0x80;
lstrcpyW(btn.text, L"帮助");
btn.creation_data = (WORD)NULL;
align32(&p);
memcpy(p, &btn, sizeof(btn));
p += sizeof(btn);
// 弹出对话框
DialogBoxIndirect(hInstance, &dlg.tpl, NULL, DlgProc);
return 0;
}