使用Windows API改变字体风格

#include 
#pragma comment(linker,"/subsystem:windows")
HWND hs;
typedef struct tagSetFont{
	HWND hwnd;
	char *facename;
}setFont;
int CALLBACK FontCallback(LOGFONT *lf,TEXTMETRIC *tm,DWORD type,LPARAM structSetFont)
{
	HFONT hfont;
	setFont *value=(setFont*) structSetFont;
	if(strstr(lf->lfFaceName,(char *)value->facename))
	{
		hfont=CreateFontIndirect(lf);
		SendMessage(value->hwnd,WM_SETFONT,(WPARAM)hfont,(LPARAM)MAKELONG(TRUE,TRUE));
		return FALSE;
	}
	return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	static HWND hedit;
	setFont sf;
	switch(msg)
	{
	case WM_CREATE:				
		hedit=CreateWindow("static","hello",WS_CHILD | WS_VISIBLE,10,10,100,100,hwnd,NULL,((CREATESTRUCT *)lParam)->hInstance,NULL);
		sf.hwnd=hedit;
		sf.facename="Impact";		
		EnumFonts(GetDC(hedit),NULL,(FONTENUMPROC)FontCallback,(LPARAM)&sf);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hi,HINSTANCE hpi,LPSTR cmdLine,int nCmdShow)
{
	MSG msg;
	HWND hwnd;
	WNDCLASS wcls;
	ZeroMemory(&wcls,sizeof wcls);
	wcls.hInstance=hi;
	wcls.lpfnWndProc=WndProc;
	wcls.lpszClassName="fontDemo";
	wcls.hCursor=LoadCursor(NULL,IDC_ARROW);
	wcls.hbrBackground=(HBRUSH)(COLOR_3DSHADOW);
	RegisterClass(&wcls);
	hwnd=CreateWindow("fontDemo","FontStyleChange",WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hi,NULL);
	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
源代码出自: ORBIT

你可能感兴趣的:(Window,API,C语言)