/////////////////////////////////////////////////////////////// // 04FirstWindow.cpp文件 #include <windows.h> #include<stdio.h> // 窗口函数的函数原形 LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char szClassName[] = "MainWClass"; WNDCLASSEX wndclass; // 用描述主窗口的参数填充WNDCLASSEX结构 wndclass.cbSize = sizeof(wndclass); // 结构的大小 wndclass.style = CS_HREDRAW|CS_VREDRAW; // 指定如果大小改变就重画 wndclass.lpfnWndProc = MainWndProc; // 窗口函数指针 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 = szClassName ; // 窗口类的名称 wndclass.hIconSm = NULL; // 没有类的小图标 // 注册这个窗口类 ::RegisterClassEx(&wndclass); // 创建主窗口 HWND hwnd = ::CreateWindowEx( 0, // dwExStyle,扩展样式 szClassName, // lpClassName,类名 "My first Window!", // lpWindowName,标题 WS_OVERLAPPEDWINDOW, // dwStyle,窗口风格 CW_USEDEFAULT, // X,初始 X 坐标 CW_USEDEFAULT, // Y,初始 Y 坐标 640, // nWidth,宽度 480, // nHeight,高度 NULL, // hWndParent,父窗口句柄 NULL, // hMenu,菜单句柄 hInstance, // hlnstance,程序实例句柄 NULL) ; // lpParam,用户数据 if(hwnd == NULL) { ::MessageBox(NULL, "创建窗口出错!", "error", MB_OK); return -1; } // 显示窗口,刷新窗口客户区 ::ShowWindow(hwnd, nCmdShow); ::UpdateWindow(hwnd); // 从消息堆中取出消息 MSG msg; while(::GetMessage(&msg, NULL, 0, 0)) { // 转化键盘消息 ::TranslateMessage(&msg); // 将消息发送到相应的窗口函数 ::DispatchMessage(&msg); } // 当GetMessage返回0时程序结束 return msg.wParam; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { char s[45]; int rowwidth; int ig = 0, ir = 0, ie = 0; char Test[] = "Yesterday, All my troubles seemed so far away, Now it looks as though they're here to stay, Oh, I believe in yesterday. Suddenly, I'm not half the man I used to be, There's a shadow hanging over me, Oh, yesterday came suddenly. Why she Had to go I don't know, she wouldn't say. I said, Something wrong, now I long for yesterday. Yesterday, Love was such an easy game to play, Now I need a place to hide away, Oh, I believe in yesterday. Why she Had to go I don't know, she wouldn't say. I said, Something wrong, now I long for yesterday. Yesterday, Love was such an easy game to play, Now I need a place to hide away, Oh, I believe in yesterday."; char szT[]= "Yesterday, "; char szText[] = "最简单的窗口程序!"; int sum=0; switch (message) { case WM_PAINT: // 窗口客户区需要重画 { HDC hdc; PAINTSTRUCT ps; // 使无效的客户区变的有效,并取得设备环境句柄 hdc = ::BeginPaint (hwnd, &ps) ; // 显示文字 ::TextOut(hdc, 10, 10, szText, strlen(szText)); ::EndPaint(hwnd, &ps); return 0; } case WM_CREATE: HDC hdc ; HFONT hFont; int arr[91]; FILE * pFile; SIZE sz; fopen_s (&pFile,"file.txt","w+"); hdc = GetDC(hwnd); hFont = CreateFont(-15, 0,0,0, FW_BOLD, false, false, false, DEFAULT_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH,"Arial"); SelectObject(hdc, hFont); //GetTextExtentPoint32(hdc,szT,strlen(szT),&sz); //fprintf(pFile,"w=%ld,h=%ld ",sz.cx,sz.cy); GetCharWidth32(hdc,32,122,arr); //for(int i=0;i<91;i++) // fprintf(pFile,"%c-->%d ",' '+i,arr[i]); /* for(int i=0;(szT[i]!=' ')&&(szT[i]!=' ');i++) { fprintf(pFile,"%c--> %d ",szT[i],arr[int(szT[i])-32]); sum=sum+arr[int(szT[i])-32]; }*/ while(ie < 520) { //如果句子不是换行或者结尾,s数组保存每个句子 if ((Test[ig] != ' ') && (Test[ig] != 0)) { s[ir++] = Test[ig]; } //如果是结尾 else { sum=0; s[ir] = 0; //添加字符串结束符 for(int i=0;s[i]!='