#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;
}