Schriftart in RichEditControl lässt sich nicht ändern!
-
Hallo,
ich versuche die Schriftart in einem RichEditControl zu ändern. Ich weiß nicht, warum das nicht klappt:CHARFORMAT chfmt ={0}; CHOOSEFONT cf = {0}; HFONT hfont; //... ChooseFont(&cf); hfont = CreateFontIndirect( cf.lpLogFont ); // Bis hier ok, member von cf haben Inhalt bekommen, wenn Schrift gewählt und hfont != NULL. chfmt.cbSize = sizeof(CHARFORMAT); chfmt.dwMask = CFM_FACE|CFM_CHARSET|CFM_SIZE; chfmt.bPitchAndFamily = lf.lfPitchAndFamily; chfmt.bCharSet = lf.lfCharSet; chfmt.dwEffects = CFE_AUTOCOLOR; chfmt.yHeight = lf.lfHeight; strcpy(chfmt.szFaceName, lf.lfFaceName); SendMessage( hTop, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) &chfmt); //...Der Rückgabewert von SendMessage und von GetLastError ist 0.
Was mache ich falsch?
Eine Webseite mit einem funktionierenden Beispiel habe ich leider auch nicht finden können.
Gruß,
mr. problem
-
Du musst cf erstmal mit Werten füllen, danach kann CreateFontIndirect erst einen Handle zu einer Schriftart liefern die ungefähr oder genau der Schriftart entspricht, die du in cf angegeben hast.
-
Helmut schrieb:
Du musst cf erstmal mit Werten füllen, danach kann CreateFontIndirect erst einen Handle zu einer Schriftart liefern die ungefähr oder genau der Schriftart entspricht, die du in cf angegeben hast.
cf ist doch mit Werten gefüllt, wenn man eine Schrift im Dialog gewählt hat.
-
p.s.
siehe auch kommentar in zeile 7.
-
Win32 FAQ,
siehe in der fortgeschrittenen Gruppe win32
news://nntp.aioe.org/comp.os.ms-windows.programmer.win32
-
fred100 schrieb:
Win32 FAQ,
siehe in der fortgeschrittenen Gruppe win32
news://nntp.aioe.org/comp.os.ms-windows.programmer.win32da kann ich auch nichts anderes finden, als was ich schon gemacht habe.
-
ich poste jetzt einfach mal das komplette programm. vielleicht hat ja jemand lust und zeit es durch seinen compiler zu jagen und mir meinen fehler zu zeigen.
es ist ein einfaches fenster mit zwei RichEditControl.
wenn das programm gestartet ist, erscheint der FontDialog, nachdem man die Taste 'a' gedrückt hat. Dann soll es möglich sein, dem oberen RichEdit eine Schriftart zuzuweisen.#include <windows.h> #include <richedit.h> #include <tchar.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LPCSTR lpszAppName = "AppName"; LPCSTR lpszTitle = "Meine erste Applikation"; HWND hTop = NULL, hBottom = NULL; CHOOSEFONT cf = {0}; LOGFONT lf = {0}; int border=3; void ErrExit ( TCHAR* msg ) { if (msg) MessageBox ( NULL, msg, "Error", MB_OK ); exit(1); } void InitFontStruct () { // Initialisation cf.lStructSize = sizeof( CHOOSEFONT ); cf.lpLogFont = &lf; cf.Flags = CF_EFFECTS | CF_BOTH | CF_INITTOLOGFONTSTRUCT ; // If not set, no dialog appears ! :-( } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hWnd; MSG msg; WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL,IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszClassName = lpszAppName; wc.lpszMenuName = lpszAppName;//MAKEINTRESOURCE(IDR_MENU1); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if( RegisterClassEx(&wc) == 0) return 0; hWnd = CreateWindowEx(NULL, lpszAppName, lpszTitle, WS_OVERLAPPEDWINDOW, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if( hWnd == NULL) return 0; InitFontStruct (); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT umsg, WPARAM wParam, LPARAM lParam) { TCHAR tbuf[256]; static RECT r; int mouse_y_prev=-1,mouse_y_now=0; GetClientRect (hWnd,&r); switch (umsg) { case WM_CHAR: { switch(wParam) { HFONT hfont; CHARFORMAT chfmt ={0}; /* LRESULT lres = SendMessage(hTop, EM_GETLANGOPTIONS, 0, 0); lres &= ~IMF_AUTOFONT; SendMessage(hTop, EM_SETLANGOPTIONS, 0, lres); */ case 'a': ChooseFont(&cf); if ( *cf.lpLogFont->lfFaceName == 0 ) // Keine Schrift gewählt. break; if ( NULL == ( hfont = CreateFontIndirect( cf.lpLogFont ))) // Create the chosen font. ErrExit( "CreateFontIndirect failed." ); chfmt.cbSize = sizeof(CHARFORMAT); chfmt.dwMask = CFM_FACE|CFM_CHARSET|CFM_SIZE; chfmt.bPitchAndFamily = lf.lfPitchAndFamily; chfmt.bCharSet = lf.lfCharSet; chfmt.yHeight = lf.lfHeight; _tcscpy (chfmt.szFaceName, lf.lfFaceName); if ( 0 == SendMessage( hTop, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) &chfmt)) { _stprintf(tbuf,"Can't set font %s. LastError: %d\n", lf.lfFaceName, GetLastError()); ErrExit(tbuf); } } // switch(wParam) return 0; } // case WM_CHAR: case WM_LBUTTONDOWN: { mouse_y_prev = HIWORD(lParam); // HIWORD(lParam) +3 > topRect.bottom ) // MessageBox(hWnd,"KLICK 1", NULL, MB_OK ); return 0; } case WM_MOUSEMOVE: { mouse_y_now = HIWORD(lParam); if (wParam==MK_LBUTTON){ // MessageBox(NULL,"KLICK 2", NULL, MB_OK ); MoveWindow( hTop, 0, 0, r.right, mouse_y_now-border, 1 ); MoveWindow( hBottom,0, mouse_y_now+border, r.right, r.bottom-mouse_y_now, 1 ); // SetWindowPos( hTop, hWnd, 0, 0, r.right, mouse_y_now-border, SWP_SHOWWINDOW ); // SetWindowPos( hBottom,hWnd, 0, mouse_y_now+border, r.right, r.bottom-mouse_y_now, SWP_SHOWWINDOW ); } mouse_y_prev = -1; // HIWORD(lParam) +3 > topRect.bottom ) // MessageBox(NULL,"KLICK", NULL, MB_OK ); return 0; } case WM_CREATE: { hTop = CreateWindowExW(WS_EX_CLIENTEDGE, L"edit", L"Top", // <- das ist der Inhalt der Editfelds WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 0, 0, r.right, r.bottom/2-border, hWnd, NULL, ((LPCREATESTRUCT) lParam) -> hInstance, NULL); hBottom = CreateWindowExW(WS_EX_CLIENTEDGE, L"edit", L"Bottom", // <- das ist der Inhalt der Editfelds WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 0, r.bottom/2+border, r.right, r.bottom/2, hWnd, NULL, ((LPCREATESTRUCT) lParam) -> hInstance, NULL); if ( hBottom==NULL||hTop==NULL ) ErrExit ( "CreateWindowExW failed." ); return 0; } case WM_SIZE: { MoveWindow( hTop, 0, 0, r.right, r.bottom/2-border, 1 ); MoveWindow( hBottom,0, r.bottom/2+border, r.right, r.bottom/2, 1 ); return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } // switch (umsg) return DefWindowProc(hWnd, umsg, wParam, lParam); }
-
Hat sich erledigt, danke.