I. Font dialog▲
En annexe au chapitre Boîtes de dialogue communes. Voici le code de l'exemple avec la possibilité de changer la police de caractères du contrôle d'édition. La fonction d'appel de la boîte de dialogue se nomme ChooseFont. Elle utilise une structure CHOOSEFONT pour s'initialiser. Elle a entre autres un champ recevant un pointeur sur une structure LOGFONT. Il permet d'initialiser la boîte de dialogue avec la police en cours et de recevoir celle qui a été choisie dans la boîte de dialogue.
static HFONT hFont;
static LOGFONT lf;
/*... */
case WM_COMMAND:
if(LOWORD(wParam) == IDM_FONT)
{
CHOOSEFONT cf;
ZeroMemory(&cf, sizeof(CHOOSEFONT));
cf.lStructSize = sizeof (CHOOSEFONT);
cf.hwndOwner = hwnd;
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
if (ChooseFont(&cf))
{
DeleteObject(hFont);
hFont = CreateFontIndirect(&lf);
SendMessage(hEdit,WM_SETFONT,(UINT)hFont,TRUE);
}
}Pour plus de précisions sur les polices de caractères, vous pouvez consulter ce chapitre : Dessiner avec Windows.
II. Code complet▲
#include <windows.h>
#define IDM_QUIT 1
#define IDM_OPEN 2
#define IDM_FONT 3
HINSTANCE hinst;
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
HMENU hMenu, hSousMenu;
hinst = hinstance;
wc.style = 0 ;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "MaWinClass";
if(!RegisterClass(&wc)) return FALSE;
hSousMenu = CreateMenu();
AppendMenu(hSousMenu, MF_STRING, IDM_OPEN, "Ouvrir...");
AppendMenu(hSousMenu, MF_STRING, IDM_FONT, "Font...");
AppendMenu(hSousMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hSousMenu, MF_STRING, IDM_QUIT, "Quitter");
hMenu = CreateMenu();
AppendMenu(hMenu,MF_POPUP,(UINT)hSousMenu,"Fichier");
hwnd = CreateWindow("MaWinClass", "Titre", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 300,
NULL, hMenu, hinstance, NULL);
if (!hwnd) return FALSE;
ShowWindow(hwnd, nCmdShow);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/******************************************************************************/
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit;
static HFONT hFont;
static LOGFONT lf;
static BOOL EditNotChg = TRUE;
switch (uMsg)
{
case WM_CREATE:
{
hEdit = CreateWindow("edit", "Texte", WS_CHILD | WS_VISIBLE |
ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL,
0, 0, 0, 0, hwnd, NULL, hinst, NULL);
ZeroMemory(&lf, sizeof(LOGFONT));
lstrcpy(lf.lfFaceName,"Courier");
lf.lfHeight = 10;
hFont = CreateFontIndirect(&lf);
SendMessage(hEdit,WM_SETFONT,(UINT)hFont,TRUE);
SendMessage(hEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN,
MAKELONG(5, 5));
return 0;
}
case WM_CLOSE:
if(EditNotChg ||
MessageBox(hwnd,"Le texte a été modifié.\r\nEtes vous sûr de \
vouloir fermer l'application ?"
,"Question ?",MB_YESNO | MB_ICONQUESTION ) == IDYES)
DestroyWindow(hwnd);
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == IDM_OPEN)
{
OPENFILENAME ofn;
CHAR szFile[MAX_PATH]={0};
ZeroMemory(&ofn, sizeof(OPENFILENAME));
#ifdef OPENFILENAME_SIZE_VERSION_400
ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
#else
ofn.lStructSize = sizeof(OPENFILENAME);
#endif
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter =
"Fichier source C\0*.c\0Fichier source CPP\0*.cpp\0";
ofn.nFilterIndex = 1;
ofn.Flags =
OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn))
{
HANDLE hf;
DWORD FileSize,nbcharRead ;
char *buffer;
hf = CreateFile(szFile, GENERIC_READ, 0,NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
FileSize = GetFileSize(hf, NULL);
buffer = (char*)LocalAlloc(LMEM_FIXED, FileSize+1);
ReadFile(hf, buffer, FileSize, &nbcharRead, NULL) ;
buffer[FileSize] = 0;
SendMessage(hEdit, WM_SETTEXT, 0, (LPARAM)buffer);
LocalFree(buffer);
CloseHandle(hf);
}
} /*if IDM_OPEN*/
if(LOWORD(wParam) == IDM_FONT)
{
CHOOSEFONT cf;
ZeroMemory(&cf, sizeof(CHOOSEFONT));
cf.lStructSize = sizeof (CHOOSEFONT);
cf.hwndOwner = hwnd;
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
if (ChooseFont(&cf))
{
DeleteObject(hFont);
hFont = CreateFontIndirect(&lf);
SendMessage(hEdit,WM_SETFONT,(UINT)hFont,TRUE);
}
} /*if IDM_FONT*/
if(LOWORD(wParam) == IDM_QUIT) PostMessage(hwnd, WM_CLOSE,0,0);
if(HIWORD(wParam) == EN_CHANGE) EditNotChg = FALSE;
return 0;
case WM_SIZE:
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
return 0;
case WM_DESTROY:
DeleteObject(hFont);
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}J'ai testé les compilations avec C++ Builder et DevC++.
À vos PC.


