2017-09-12 2 views
0

J'ai essayé de changer la police de la sortie dans la fenêtre en utilisant l'API Win32. Voici ce que j'ai fait, mais ça ne marche pas. Que devrais-je faire?Changer la police d'un texte dans Win32

hdc = BeginPaint(hWnd, &ps); 
hFont = CreateFont(y_position, closest_match, escapement, orientation, FW_DONTCARE, 
    no_italic, no_ul, no_xout, ANSI_CHARSET, 
    OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, VARIABLE_PITCH, 
    TEXT("Tekton Pro")/*"SYSTEM_FIXED_FONT"*/); 
SetTextColor(hdc, RGB(255, 0, 0)); 
TextOut(hdc, 50, y_position, TEXT("test"), strlen("test")); 
// TODO: Add any drawing code that uses hdc here... 
EndPaint(hWnd, &ps); 
DeleteObject(hFont); 
+0

ressources Leaking grand temps ici –

+0

Oui .... Espérons que vous appelez DeleteObject (hFont); après utilisation – Naidu

Répondre

5

Après avoir créé votre objet de police hFont, vous devez appeler SelectObject() pour l'affecter à la hdc. Lorsque vous avez fini d'utiliser votre police, appelez SelectObject() nouveau pour restaurer l'ancienne police (il ne fuit pas) avant de détruire votre police:

hdc = BeginPaint(hWnd, &ps); 
hFont = CreateFont(y_position, closest_match, escapement, orientation, FW_DONTCARE, 
    no_italic, no_ul, no_xout, ANSI_CHARSET, 
    OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, VARIABLE_PITCH, 
    TEXT("Tekton Pro")/*"SYSTEM_FIXED_FONT"*/); 
hOldFont = (HFONT) SelectObject(hdc, hFont); // <-- add this 
SetTextColor(hdc, RGB(255, 0, 0)); 
TextOut(hdc, 50, y_position, TEXT("Hello from Ugur"), strlen("Hello From Ugur")); 
SelectObject(hdc, hOldFont); // <-- add this 
DeleteObject(hFont); // <-- add this 
// TODO: Add any drawing code that uses hdc here... 
EndPaint(hWnd, &ps);