/*-----------------------------------------
从书本源代码拷贝过来,编译后发现 360 会把这个程序终结
-----------------------------------------*/
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("StokFont") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Stock Fonts"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL, //WS_VSCROLL窗口存在垂直滚动条
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//创建一个 stockfont 数组 ,大小为 sizeof stockfont / sizeof stockfont[0]
static struct
{
int idStockFont ;
TCHAR * szStockFont ;
}
stockfont [] = { OEM_FIXED_FONT, TEXT ("OEM_FIXED_FONT"),
ANSI_FIXED_FONT, TEXT ("ANSI_FIXED_FONT"),
ANSI_VAR_FONT, TEXT ("ANSI_VAR_FONT"),
SYSTEM_FONT, TEXT ("SYSTEM_FONT"),
DEVICE_DEFAULT_FONT, TEXT ("DEVICE_DEFAULT_FONT"),
SYSTEM_FIXED_FONT, TEXT ("SYSTEM_FIXED_FONT"),
DEFAULT_GUI_FONT, TEXT ("DEFAULT_GUI_FONT") } ;
//iFont 作为一个下标, cFonts作为一个最大越界值(cFonts - 1 为最大下标)
static int iFont, cFonts = sizeof stockfont / sizeof stockfont[0] ;
HDC hdc ;
//作为字体排版的位置, 这里没有设置 WM_SIZE 消息,所以不用使用 静态
int i, x, y, cxGrid, cyGrid ;
PAINTSTRUCT ps ;
//LF_FACESIZE 为微软给予的字体名称最大长度:比如"宋体",
// szBuffer 写入第一行的字符串 LF_FACESIZE + 64,保证能容纳字体名称
TCHAR szFaceName [LF_FACESIZE], szBuffer [LF_FACESIZE + 64] ;
//保存字体的规格
TEXTMETRIC tm ;
switch (message)
{
case WM_CREATE:
SetScrollRange (hwnd, SB_VERT, 0, cFonts - 1, TRUE) ;
return 0 ;
//改变分辨率消息响应
case WM_DISPLAYCHANGE:
//使窗口无效,产生 WM_PAINT 消息
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
//垂直滚动条消息响应
case WM_VSCROLL:
switch (LOWORD (wParam))
{
case SB_TOP: iFont = 0 ; break ;
case SB_BOTTOM: iFont = cFonts - 1 ; break ;
case SB_LINEUP:
case SB_PAGEUP: iFont -= 1 ; break ;
case SB_LINEDOWN:
case SB_PAGEDOWN: iFont += 1 ; break ;
case SB_THUMBPOSITION: iFont = HIWORD (wParam) ; break ;
}
//保证不超出最大值的个数
//一个名称保存一个值.
iFont = max (0, min (cFonts - 1, iFont)) ;
//设置滚动条
SetScrollPos (hwnd, SB_VERT, iFont, TRUE) ;
//使窗口无效,从而产生 WM_PAINT 消息
InvalidateRect (hwnd, NULL, TRUE) ;
return 0 ;
//虚拟按钮消息响应
case WM_KEYDOWN:
switch (wParam)
{
case VK_HOME: SendMessage (hwnd, WM_VSCROLL, SB_TOP, 0) ; break ;
case VK_END: SendMessage (hwnd, WM_VSCROLL, SB_BOTTOM, 0) ; break ;
case VK_PRIOR:
case VK_LEFT:
case VK_UP: SendMessage (hwnd, WM_VSCROLL, SB_LINEUP, 0) ; break ;
case VK_NEXT:
case VK_RIGHT:
case VK_DOWN: SendMessage (hwnd, WM_VSCROLL, SB_PAGEDOWN, 0) ; break ;
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
//设置字体样式(设置字体种样), 一页一种样式
SelectObject (hdc, GetStockObject (stockfont[iFont].idStockFont)) ;
//获取字体, LF_FACESIZE 为默认最大字体大小,szFaceName为局部变量
GetTextFace (hdc, LF_FACESIZE, szFaceName) ;
GetTextMetrics (hdc, &tm) ;
cxGrid = max (3 * tm.tmAveCharWidth, 2 * tm.tmMaxCharWidth) ;
cyGrid = tm.tmHeight + 3 ;
TextOut (hdc, 0, 0, szBuffer,
wsprintf (szBuffer, TEXT (" %s: Face Name = %s, CharSet = %i"),
stockfont[iFont].szStockFont,
szFaceName, tm.tmCharSet)) ;
SetTextAlign (hdc, TA_TOP | TA_CENTER) ;
// 画表格
for (i = 0 ; i < 17 ; i++)
{
MoveToEx (hdc, (i + 2) * cxGrid, 2 * cyGrid, NULL) ;
LineTo (hdc, (i + 2) * cxGrid, 19 * cyGrid) ;
MoveToEx (hdc, cxGrid, (i + 3) * cyGrid, NULL) ;
LineTo (hdc, 18 * cxGrid, (i + 3) * cyGrid) ;
}
// 画表头(横向首行和纵向首竖)
for (i = 0 ; i < 16 ; i++)
{
TextOut (hdc, (2 * i + 5) * cxGrid / 2, 2 * cyGrid + 2, szBuffer,
wsprintf (szBuffer, TEXT ("%X-"), i)) ;
TextOut (hdc, 3 * cxGrid / 2, (i + 3) * cyGrid + 2, szBuffer,
wsprintf (szBuffer, TEXT ("-%X"), i)) ;
}
// 写字, szBuffer 的使用不用那个纠结,
//只需要知道他只是作为缓存输出就足够
//(注)很多情况下不用创建多个变量,
//创建一个变量用作多种用途也可以
//只要你的空间足够容纳
//比如:创建一个int(4字)
/*
#include <stdio.h>
void Vid(int Q,int p)
{
printf("%d,%d",Q,p);
}
void D(void (* p)())
{
p(122,p);
}
int main()
{
int d;
printf("%d\n",Vid);
//输入 Vid 的值给 d ,你会发现同样可以执行并且调用到 Vid 函数
scanf("%d",&d);
D((void (* )())d);
return 0;
}
*/
for (y = 0 ; y < 16 ; y++)
for (x = 0 ; x < 16 ; x++)
{
TextOut (hdc, (2 * x + 5) * cxGrid / 2,
(y + 3) * cyGrid + 2, szBuffer,
wsprintf (szBuffer, TEXT ("%c"), 16 * x + y)) ;
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}