Exemple de Boîte de Dialogue Dynamique

Ancien nº de publication de cet article : F06917
Symptômes

Comment puis-je construire une boîte de dialogue de manière entièrement
dynamique ?

Résolution

Voici un exemple mettant en oeuvre la gestion
d'une boîte de dialogue de manière dynamique.

/********************************************/
/* */
/* MAKEFILE */
/* */
/********************************************/

all : dydlg.exe

dydlg.obj : dydlg.c
cl -c -AS -Gsw -Zip dydlg.c makefile

dydlg.res : dydlg.rc
rc -r dydlg.rc

dydlg.exe : dydlg.obj dydlg.res
link /CO dydlg.obj,/A:16,,libw+slibcew,dydlg.def
rc dydlg


/********************************************/
/* */
/* DYDLG.RC */
/* */
/********************************************/

/* pas de ressources */

/********************************************/
/* */
/* DYDLG.DEF */
/* */
/********************************************/

NAME dydlg
DESCRIPTION 'DYDLG'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 8192
EXPORTS
WndProc
DlgProc

/********************************************/
/* */
/* DYDLG.C */
/* */
/********************************************/

#include <Windows.h>
#include <string.h>

#define SIZE_BUF 1024

LONG FAR PASCAL WndProc(HWND, WORD, WORD, LONG);
BOOL FAR PASCAL DlgProc(HWND, WORD, WORD, LONG);
void DynamicDlgExample(HANDLE, HWND, FARPROC);
void lmemcpy(LPSTR, LPSTR, DWORD);

/* ---- Classes des éléments (pourrait être dans WINDOWS.H) */

#define BUTTON 0x80
#define EDIT 0x81
#define STATIC 0x82
#define LISTBOX 0x83
#define SCROLLBAR 0x84
#define COMBOBOX 0x85

typedef struct
{
long dtStyle;
BYTE dtItemCount;
int dtX;
int dtY;
int dtCX;
int dtCY;
char *dtMenuName;
char *dtClassName;
char *dtCaptionText;
} DLGTEMPLATE;

typedef struct
{
int dtilX;
int dtilY;
int dtilCX;
int dtilCY;
int dtilID;
long dtilStyle;
BYTE dtilClass;
char *dtilText;
BYTE dtilInfo;
LPSTR dtilData;
} DLGITEMTEMPLATE;

/* Description boîte de dialogue dynamique */
DLGTEMPLATE Dlg =
{
WS_BORDER | WS_CAPTION | WS_DLGFRAME | DS_MODALFRAME | WS_POPUP |
WS_VISIBLE, 2, 22,17,145,75, "","","Dialog box dynamique"
};

DLGITEMTEMPLATE DlgItem[2] =
{
{5, 5, 140, 8, -1, SS_CENTER | WS_CHILD | WS_VISIBLE,
STATIC, "Je suis dynamique!", 0 },
{53, 59, 32, 14, IDOK, BS_DEFPUSHBUTTON | WS_GROUP | WS_CHILD |
WS_VISIBLE, BUTTON, "Ok", 0 }
};

char szAppName[] = "dydlg";
HANDLE hInst;
FARPROC lpProc;

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS WndClass;

hInst = hInstance;
if (!hPrevInstance)
{
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = WndProc;
WndClass.hInstance = hInstance;
WndClass.hIcon = NULL;
WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = szAppName;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
if (!RegisterClass(&WndClass))
return FALSE;
}
lpProc = MakeProcInstance((FARPROC)DlgProc, hInstance);
hwnd = CreateWindow(szAppName, "Boîte de dialogue dynamique",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MessageBox(hwnd, "Frappez une touche au clavier!",
(LPSTR)szAppName, MB_OK);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

long FAR PASCAL WndProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
switch(message)
{
case WM_KEYDOWN :
DynamicDlgExample(hInst, hwnd, lpProc);
break;

case WM_DESTROY :
PostQuitMessage(0);
break;

default :
return DefWindowProc(hwnd, message, wParam, lParam);
break;
}
return 0L;
}

void DynamicDlgExample(HANDLE hInstance, HWND hwnd, FARPROC lpProc)
{
char buffer[SIZE_BUF], *s;
int i;
HANDLE hMem;
LPSTR lpBuf;

s = buffer;
memcpy(s, &Dlg.dtStyle, 13);
s += 13;
s += (strlen (strcpy (s, Dlg.dtMenuName)) + 1);
s += (strlen (strcpy (s, Dlg.dtClassName)) + 1);
s += (strlen (strcpy (s, Dlg.dtCaptionText)) + 1);

for (i = 0; i < (int)Dlg.dtItemCount; ++i)
{
memcpy(s, &DlgItem[i].dtilX, 15);
s += 15;
s += (strlen (strcpy (s, DlgItem[i].dtilText)) + 1);
*s++ = DlgItem[i].dtilInfo; // Data non traités
}

if (hMem = GlobalAlloc(GHND, s - buffer))
{
if (lpBuf = GlobalLock(hMem))
{
lmemcpy(lpBuf, (LPSTR)buffer, s - buffer);
GlobalUnlock(hMem);
DialogBoxIndirect(hInstance, hMem, hwnd, lpProc);
}
GlobalFree(hMem);
}
}

BOOL FAR PASCAL DlgProc(HWND hDlg, WORD message, WORD wParam, LONG lParam)
{
switch(message)
{
case WM_INITDIALOG :
break;

case WM_COMMAND :
switch(wParam)
{
case IDOK :
EndDialog(hDlg, TRUE);
break;

default :
return FALSE;
}
break;

default :
return FALSE;
}
return TRUE;
}

void lmemcpy(LPSTR s, LPSTR t, DWORD dw)
{
while (dw--)
*s++ = *t++;
Propriétés

ID d'article : 456917 - Dernière mise à jour : 03/05/1993 00:00:00 - Révision : 1.0

Microsoft Windows Software Development Kit 3.1

  • KB456917
Commentaires