Edit Control Font

How do you change the font of an edit control? When I make them using CreateWindow, their font defaults to a bolded system font, which doesn't look very nice.
1
2
3
4
5
6
7
8
9
     
         HFONT hFont=CreateFont(18,0,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
                    CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY, VARIABLE_PITCH,TEXT("Courier New"));

         SendMessage(hEdit,             // Handle of edit control
                    WM_SETFONT,         // Message to change the font
                    (WPARAM) hFont,     // handle of the font
                    MAKELPARAM(TRUE, 0) // Redraw text
                    );

You can read about CreateFont function here:
http://msdn.microsoft.com/en-us/library/dd183499(VS.85).aspx
http://msdn.microsoft.com/en-us/library/bb775460(VS.85).aspx#changing_fonts
Thanks! Now, after some research I figured out how to change the color too, but after changing it, the backspace doesn't work... Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//header

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define WINDOW 1000

#define OUTPUT 1001
#define INPUT  1002
#define SEND   1003

#define WIDTH (470)
#define HEIGHT (350)

#define OUTW (WIDTH-48+10)
#define OUTH (HEIGHT-176+10)

#define INW (WIDTH-64+10)
#define INH (HEIGHT-272+10)

HWND outputbox,inputbox;
HDC hdc;
HFONT font=CreateFont(-17,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,"Courier New");
HINSTANCE hinstance;

LRESULT CALLBACK mainproc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){
	switch(msg){
		case WM_CREATE:
			outputbox=CreateWindow("static",">Please enter your username",WS_VISIBLE|WS_CHILD,16,16,OUTW,OUTH,hwnd,NULL,hinstance,NULL);
			inputbox=CreateWindow("edit","",WS_VISIBLE|WS_CHILD|ES_MULTILINE|ES_AUTOVSCROLL,16,OUTH+32,INW,INH,hwnd,NULL,hinstance,NULL);
			SendMessage(outputbox,WM_SETFONT,(WPARAM)font,MAKELPARAM(true,0));
			SendMessage(inputbox,WM_SETFONT,(WPARAM)font,MAKELPARAM(true,0));
			break;
		case WM_CTLCOLORSTATIC:
			SetTextColor((HDC)wparam,RGB(0,255,0));
			SetBkMode((HDC)wparam,TRANSPARENT);
			return (LRESULT)GetStockObject(BLACK_BRUSH);
		case WM_CTLCOLOREDIT:
			SetTextColor((HDC)wparam,RGB(0,255,0));
			SetBkMode((HDC)wparam,TRANSPARENT);
			return (LRESULT)GetStockObject(BLACK_BRUSH);}
	return DefWindowProc(hwnd,msg,wparam,lparam);}

//main

#include "header.h"

int WINAPI WinMain(HINSTANCE hinst,HINSTANCE hprev,char* cmdparam,int cmdshow){
	//variable declarations
	HWND hwnd;
	MSG messages;
	WNDCLASSEX wndclass;
	//register window class
	wndclass.hInstance=hinst;
	wndclass.lpszClassName="MyClass";
	wndclass.lpfnWndProc=mainproc;
	wndclass.style=CS_DBLCLKS;
	wndclass.cbSize=sizeof(WNDCLASSEX);
	wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.lpszMenuName=NULL;
	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	if(!RegisterClassEx(&wndclass)){
		MessageBox(0,"ERROR: Failed to register window class","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return 1;}
	//create the window
	hwnd=CreateWindowEx(0,"MyClass","MyApp",WS_OVERLAPPEDWINDOW&~WS_THICKFRAME,CW_USEDEFAULT,CW_USEDEFAULT,WIDTH,HEIGHT,HWND_DESKTOP,NULL,hinst,NULL);
	if(!hwnd){
		MessageBox(0,"ERROR: Failed to create window","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return 2;}
	ShowWindow(hwnd,cmdshow);
	//enter main loop
	while(GetMessage(&messages,NULL,0,0)){
		TranslateMessage(&messages);
		DispatchMessage(&messages);}
	return messages.wParam;}



Why isn't it working and how do I fix it?
Last edited on
You need to redraw background. I can't remember how exactly to solve your problem but it's something to do with EN_UPDATE or EN_CHANGE messages. (I'll look at your code tomorrow )
1
2
3
4
5
6
7
		case WM_CTLCOLOREDIT:
			SetTextColor((HDC)wparam,RGB(0,255,0));
			SetBkColor((HDC)wparam,RGB(0,0,0));     // Set background color too
			SetBkMode((HDC)wparam,OPAQUE); // the background MUST be opaque

			return (LRESULT)GetStockObject(BLACK_BRUSH);
 

Now it should work.
Topic archived. No new replies allowed.