NO

Author Topic: DynDlg example  (Read 3337 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1912
DynDlg example
« on: May 19, 2011, 08:44:20 pm »
DynDlg example.
How to make it better?
Code: [Select]
#define WIN32_DEFAULT_LIBS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

typedef struct _tagDLGPARMS {
TCHAR *lpTitle;
} DLGPARMS;

static BOOL OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);
static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);

BOOL CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
return OnInitDialog((hWnd), (HWND)(wParam), lParam);

case WM_COMMAND:
OnCommand(hWnd, (int)LOWORD(wParam) , (HWND)lParam, (int)HIWORD(wParam));
break;

case WM_CLOSE:
EndDialog(hWnd, wParam);
break;
}

return FALSE;

}

static BOOL OnInitDialog(HWND hDlg, HWND hwndFocus, LPARAM lParam)
{
SetWindowText(hDlg, ((DLGPARMS*)lParam)->lpTitle);
HWND hWnd = GetParent(hDlg);
if (!hWnd) hWnd = GetDesktopWindow();
if (hWnd) {
RECT rcP, rcD;
GetWindowRect(hWnd, &rcP);
GetClientRect(hDlg, &rcD);
int iX, iY;
iX = rcP.left + (rcP.right - rcP.left) / 2 - rcD.right / 2;
iY = rcP.top + (rcP.bottom - rcP.top) / 2 - rcD.bottom / 2;
SetWindowPos(hDlg, HWND_TOP, iX, iY, 0, 0, SWP_NOSIZE);
}
HINSTANCE hInst = GetModuleHandle(NULL);
HFONT hFont = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hWnd, WM_SETFONT, (UINT)hFont, 0L);
hWnd = CreateWindow("BUTTON", "Ok", WS_CHILD|WS_VISIBLE, 5, 5, 50, 20, hDlg, (HMENU)IDOK, hInst, 0);
SendMessage(hWnd, WM_SETFONT, (UINT)hFont, 0L);
hWnd = CreateWindow("BUTTON", "Cancel", WS_CHILD|WS_VISIBLE, 5, 35, 50, 20, hDlg, (HMENU)IDCANCEL, hInst, 0);
SendMessage(hWnd, WM_SETFONT, (UINT)hFont, 0L);
return TRUE;
}

static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch (id) {
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
case IDOK:
EndDialog(hwnd, IDOK);
break;
}
}

typedef struct {
  DWORD style;
  DWORD dwExtendedStyle;
  WORD  cdit;
  short x;
  short y;
  short cx;
  short cy;
  WORD w[4]; // menu, class, title, and font arrays
} DLGTEMPLATE2, *LPDLGTEMPLATE2;

int DlgBox(HWND hWnd, LPCTSTR lpCaption)
{
DLGTEMPLATE2 lpDlg = {0};
int rc;
DLGPARMS dlgstr;

dlgstr.lpTitle = (char *)lpCaption;
lpDlg.style = WS_POPUPWINDOW | WS_CAPTION | WS_VISIBLE | WS_MINIMIZEBOX;
lpDlg.cx = 100;
lpDlg.cy = 50;
rc = DialogBoxIndirectParam(GetModuleHandle(NULL), (LPDLGTEMPLATE)&lpDlg, 0, DlgProc, (LPARAM)&dlgstr);
return rc;
}

//int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
int __cdecl WinMainCRTStartup(void)
{
int rc;
char szBuf[100];
rc = DlgBox(0, "Test");
wsprintf(szBuf, "rc=%d", rc);
MessageBox(0, szBuf, 0, MB_OK);
ExitProcess(rc);
return rc;
}
May the source be with you