现在的位置: 首页 > 综合 > 正文

Text02 源代码

2013年09月19日 ⁄ 综合 ⁄ 共 3065字 ⁄ 字号 评论关闭

#include "sysmets.h"
#include <windows.h>
#include <tchar.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{
 static TCHAR szAppName[] = TEXT("text 02");
 HWND  hwnd  = NULL;
 MSG  msg  = {0};
 WNDCLASS wndclass = {0};

 //init 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;

 //register class
 if (!RegisterClass(&wndclass))
 {
  MessageBox(NULL, TEXT("This program next winnt"), TEXT("run error!"), MB_ICONERROR);

  return 0;
 }

 hwnd = CreateWindow(szAppName, TEXT("Text02 for text"), WS_OVERLAPPEDWINDOW | WS_VSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

 ShowWindow(hwnd, nCmdShow);
 UpdateWindow(hwnd);

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

 return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 static int cxChar  = 0;
 static int cxCaps  = 0;
 static int cyChar  = 0;
 static int cyClient = 0;
 static int nVscrollPos = 0;

 HDC  hdc  = NULL;
 PAINTSTRUCT ps  = {0};
 TCHAR  szBuffer[10] = "/0";
 TEXTMETRIC tm  = {0};

 int  i  = 0;
 int  y  = 0;

 switch (message)
 {
  case WM_CREATE:
  {
   hdc = GetDC(hwnd);
   
   GetTextMetrics(hdc, &tm);
   cxChar = tm.tmAveCharWidth;
   cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
   cyChar = tm.tmHeight + tm.tmExternalLeading;

   ReleaseDC(hwnd, hdc);

   SetScrollRange(hwnd, SB_VERT, 0, NUMLINES - 1L, FALSE);
   SetScrollPos(hwnd, SB_VERT, nVscrollPos, TRUE);
  }

  return 0;

  case WM_SIZE:
  {
   cyClient = HIWORD(lParam);
  }

  return 0;

  case WM_VSCROLL:
  {
   switch (LOWORD(wParam))
   {
    case SB_LINEUP:
    {
     nVscrollPos -= 1;     
    }

    break;

    case SB_LINEDOWN:
    {
     nVscrollPos += 1;
    }
    
    break;

    case SB_PAGEUP:
    {
     nVscrollPos -= cyClient / cyChar;
    }

    break;

    case SB_PAGEDOWN:
    {
     nVscrollPos += cyClient / cyChar;
    }

    break;

    case SB_THUMBPOSITION:
    {
     nVscrollPos = HIWORD(wParam);
    }
    
    break;

    default:

     break;
   }
  
  }

  nVscrollPos = max(0, min(nVscrollPos, NUMLINES - 1));

  if (nVscrollPos != GetScrollPos(hwnd, SB_VERT))
  {
   SetScrollPos(hwnd, SB_VERT, nVscrollPos, TRUE);
   InvalidateRect(hwnd, NULL, TRUE);
  }

  return 0;

  case WM_PAINT:
  {
   hdc = BeginPaint(hwnd, &ps);

   for (i = 0; i < NUMLINES; i++)
   {
    y = cyChar * (i - nVscrollPos);

    TextOut(hdc, 0, y, sysmetrics[i].szLabel, strlen(sysmetrics[i].szLabel));
    TextOut(hdc, 22 * cxCaps, y, sysmetrics[i].szDesc, strlen(sysmetrics[i].szDesc));

    SetTextAlign(hdc, TA_RIGHT | TA_TOP);
    TextOut(hdc, 22 * cxCaps + 40 * cxChar, y, szBuffer, wsprintf(szBuffer, "%5d", GetSystemMetrics(sysmetrics[i].iIndex)));

    SetTextAlign(hdc, TA_RIGHT | TA_TOP);
   }

   EndPaint(hwnd, &ps);
  }

  return 0;

  case WM_DESTROY:
   PostQuitMessage(0);

   return 0;
 }

 return DefWindowProc(hwnd, message, wParam, lParam); 
}
 

抱歉!评论已关闭.