windows编程之文本输出(字体渐变)


代码如下:


#include
#include
#include
const double PI = 3.1415926;

LRESULT CALLBACK WndProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
	WNDCLASS wndclass;

	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hInstance = hInstance;
	wndclass.lpfnWndProc = WndProc;
	wndclass.lpszClassName = "我的窗口";
	wndclass.lpszMenuName = NULL;
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wndclass); //注册窗口类

	HWND hwnd;
	hwnd = CreateWindow("我的窗口", "窗口", WS_OVERLAPPEDWINDOW, 
		0, 0, 640, 480, NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, SW_SHOWNORMAL);
	UpdateWindow(hwnd);

	MSG Msg;
	while(GetMessage(&Msg, NULL, 0, 0))
	{
		TranslateMessage(&Msg); 
        DispatchMessage(&Msg); 
	}

	return 0;
}

LRESULT CALLBACK WndProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	HDC hdc;
	PAINTSTRUCT ps;
	HFONT hfont;
	TEXTMETRIC tm;
	SIZE size;
	char str[] = "文字渐变练习";
	int i, nchlen;
	switch(uMsg)
	{
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		hfont = CreateFont(20, 0, 0, 0, FW_HEAVY, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "粗体字");
		SelectObject(hdc, hfont);
		GetTextMetrics(hdc, &tm);
		GetTextExtentPoint32(hdc, str, strlen(str), &size);

		SetTextColor(hdc, RGB(0, 0, 0));
		GetTextMetrics(hdc, &tm);
		TextOut(hdc, 200, 200, str, strlen(str));

		MoveToEx(hdc, 200, 200, NULL);
		SetTextColor(hdc, RGB(0, 255, 0));
		SetBkColor(hdc, RGB(255, 0, 0));
		
	
		nchlen = size.cx / strlen(str);
		for(i=0; i


你可能感兴趣的:(windows编程)