VC++编的一个26个英文字母跳动程序

by admin on 2020年1月18日

这是win32平台下的一个应用程序,希望大家看了以后有所感想。

亲们,这是一个简单的记事本程序,但是第二行代码#define BUFFER(x,y) *
(pBuffer + y * cxBuffer +
x)是什么意思,到都有什么用途请详细解答。拜托哦

vs中编写了个关于垂直滚动条的代码,其中有几处不理解,求解答
这是代码:
#include
#include”resource.h”
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
{
static char szAppName[] = “SysMets2”;
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
if (!hPrevInstance)
{
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 = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
RegisterClass(&wndclass);

 

 

}hwnd = CreateWindow(szAppName, "Get System Metrics No.2", WS_OVERLAPPEDWINDOW |WS_VSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL );ShowWindow(hwnd, nCmdShow);UpdateWindow;while (GetMessage(&msg, NULL, 0, 0)){ TranslateMessage; DispatchMessage;}return msg.wParam;

#include<windows.h>
#include<stdio.h>

#include<windows.h>

}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
static short cxChar, cxCaps, cyChar, cxClient, cyClient, nVscrollPos;
char szBuffer[10];
HDC hdc;
short i, y;
PAINTSTRUCT ps;
TEXTMETRIC tm;
switch
{
case WM_CREATE:
hdc = GetDC;
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, FALSE);
SetScrollPos(hwnd, SB_VERT, nVscrollPos, TRUE);
return 0;
case WM_SIZE:
cxClient = LOWORD;
cyClient = HIWORD;
return 0;
case WM_VSCROLL:
switch
{
case SB_LINEUP:
nVscrollPos -= 1;
break;
case SB_LINEDOWN:
nVscrollPos += 1;
break;
case SB_PAGEDOWN:
nVscrollPos -= cyClient / cyChar;
break;
case SB_PAGEUP:
nVscrollPos -= cyClient / cyChar;
break;
default:
break;
}
nVscrollPos = max(0, min(nVscrollPos, NUMLINES));
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*(1-nVscrollPos+i);
TextOut(hdc, cxChar, y, sysmetrics[i].szLabel,
lstrlen(sysmetrics[i].szLabel));
TextOut(hdc, cxChar+18*cxCaps, y, sysmetrics[i].szDesc,
lstrlen(sysmetrics[i].szDesc));
SetTextAlign(hdc, TA_RIGHT | TA_TOP);
TextOut(hdc, cxChar+18*cxCaps+40*cxChar,
y,szBuffer,wsprintf(szBuffer,”%5d”,GetSystemMetrics(sysmetrics[i].nIndex)));
SetTextAlign(hdc, TA_LEFT | TA_TOP);

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

#define BUFFER(x,y) * (pBuffer + y * cxBuffer + x)

 } EndPaint(hwnd, &ps); return 0;case WM_DESTROY: PostQuitMessage; return 0;}return DefWindowProc(hwnd, message, wParam, lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nCmdShow)

 WNDCLASS wndcls;
 wndcls.cbClsExtra    = 0;
 wndcls.cbWndExtra    = 0;
 wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wndcls.hCursor       = LoadCursor(NULL,IDC_ARROW);        
//光标(鼠标)
 wndcls.hIcon         = LoadIcon(NULL,IDI_QUESTION);        //图标
 wndcls.hInstance     = hInstance;   
 wndcls.lpfnWndProc   = WndProc;
 wndcls.lpszClassName = “SIN”;
 wndcls.lpszMenuName  = NULL;
 wndcls.style         = CS_HREDRAW|CS_VREDRAW;
 RegisterClass(&wndcls);  
 HWND
hwnd=CreateWindow(“SIN”,”字符跳动”,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,1000,500,NULL,NULL,hInstance,NULL); 
 ShowWindow(hwnd,SW_RESTORE);
 UpdateWindow(hwnd); 
 MSG msg;
 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR
szCmdLine, int iCmdShow)
{
     HWND         hwnd;
     WNDCLASS     wndclass;
  MSG msg;
    
     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 = “文本编辑器”;
    
     RegisterClass (&wndclass);
     hwnd = CreateWindow(“文本编辑器”,
“文本编辑器”,WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
                          CW_USEDEFAULT, 0,600, 500,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)
{
 HDC hdc;
 PAINTSTRUCT ps;
 TEXTMETRIC tm;
 HFONT hF;
 char lpsz_1[] =
{‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’g’,’k’,’l’,’m’,’n’,
                  ‘o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’};
 int nCharlen = 26;
 static int nChar = 0;
 static int i;
 static int X,Y;
 static bool bRight =TRUE,bLeft = FALSE;

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
     static DWORD   dwCharSet = DEFAULT_CHARSET;   //1
     static int     cxChar, cyChar, cxClient, cyClient, cxBuffer,
cyBuffer, xCaret, yCaret;
     static TCHAR  *pBuffer = NULL;
     HDC            hdc;
     int            x, y, i;
     PAINTSTRUCT    ps;
     TEXTMETRIC     tm;
  HFONT          ft;  //字体句柄
  HGDIOBJ        GD,gd;
  static  int  V_ScrollValue, H_ScrollValue;
    
     switch(message)
     {
     case WM_INPUTLANGCHANGE:
          dwCharSet = wParam;                               // fall
through
     case WM_CREATE:
    SetScrollRange(hwnd,SB_VERT,0,30,FALSE);
    SetScrollRange(hwnd,SB_HORZ,0,30,FALSE);
    SetScrollPos(hwnd,SB_VERT,0,TRUE);
    SetScrollPos(hwnd,SB_HORZ,0,TRUE);
          hdc = GetDC(hwnd);
    ft  = CreateFont(0, 0, 0, 0, 0, 0, 0, 0,dwCharSet, 0, 0, 0,
FIXED_PITCH, NULL);
                   //返回一个当前字体的句柄         
//1                    //1
    SelectObject(hdc,ft);
          GetTextMetrics(hdc, &tm);
          cxChar = tm.tmAveCharWidth;
          cyChar = tm.tmHeight;
    GD = GetStockObject(SYSTEM_FONT);//获取固定字体
    gd = SelectObject(hdc, GD);      //字体写入环境中
          DeleteObject(gd);
          ReleaseDC(hwnd, hdc);            // fall
through               
     case WM_SIZE:
    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);
          // calculate window size in characters
          cxBuffer = max(1, cxClient /
cxChar);//当前窗口每行可以写入字体的个数
          cyBuffer = max(1, cyClient / cyChar);
          // allocate memory for buffer and clear it
          if(pBuffer != NULL)
     free(pBuffer);
          pBuffer = (TCHAR
*)malloc(cxBuffer*cyBuffer*sizeof(TCHAR)); 
          for(y = 0; y < cyBuffer; y++)
     for(x = 0; x < cxBuffer; x++)
      BUFFER(x,y) = ‘ ‘; 
          // set caret to upper left corner
          xCaret = 0;
          yCaret = 0;
                   
          if(hwnd == GetFocus())//GetFocus()函数没参数返回一个句柄
               SetCaretPos(xCaret * cxChar, yCaret * cyChar);

其中 cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2)*cxChar / 2;
还有case SB_THUMBPOSITION:
nVscrollPos = LOWORD;
break;
还有 if (nVscrollPos!=GetScrollPos(hwnd,SB_VERT))
{
SetScrollPos(hwnd, SB_VERT, nVscrollPos, TRUE);
InvalidateRect(hwnd, NULL, TRUE);
}
还有 nVscrollPos = max(0, min(nVscrollPos, NUMLINES));
这些到底是什么意思呢? 有点长请见谅。

 switch(Message)
 {
 case WM_CREATE:
  //PlaySound(TEXT(“One people think people.wav”), NULL, SND_FILENAME |
SND_ASYNC);
  SetTimer(hwnd,1,500,NULL);
  break;
 case WM_TIMER:
  InvalidateRect(hwnd,NULL,TRUE);
  break;
 case WM_PAINT:
  hdc = BeginPaint(hwnd,&ps);
  GetTextMetrics(hdc,&tm);
  Y=tm.tmExternalLeading+80;         //设置输出字符的Y坐标
        for(i=0;i<nChar;i++)
  {
   SetTextColor(hdc,RGB(0,0,0)); //设置字体的颜色为黑色
   X=X+tm.tmAveCharWidth*4;  //设置输出字符的X坐标
   TextOut(hdc,X,Y,&lpsz_1[i],1);  //输出从第0个到第nChar-1个字符
  }

          InvalidateRect(hwnd, NULL, TRUE);
          return 0 ;       
     case WM_SETFOCUS:
          CreateCaret(hwnd, NULL, cxChar, cyChar);//创建插入符
          SetCaretPos(xCaret * cxChar, yCaret * cyChar);
          ShowCaret (hwnd);
          return 0;
     case WM_KILLFOCUS:
          // hide and destroy the caret
          HideCaret(hwnd);
          DestroyCaret();
          return 0;
  case WM_VSCROLL:
    switch(LOWORD(wParam))
    {
    case SB_LINEDOWN:
     V_ScrollValue += 1;
     //yCaret += cyChar;
     break;
    case SB_LINEUP:
     V_ScrollValue -= 1;
     break;
    case SB_PAGEUP:
     V_ScrollValue -= 5;
     break;
    case SB_PAGEDOWN:
     V_ScrollValue += 5;
     break;
    case SB_THUMBTRACK:
     V_ScrollValue = HIWORD(wParam);
     break;
    }
    SetScrollPos(hwnd,SB_VERT,V_ScrollValue,TRUE);
    InvalidateRect(hwnd,NULL,TRUE);
    return 0;
  case WM_HSCROLL:
    switch(LOWORD(wParam))
    {
    case SB_LINELEFT:
     H_ScrollValue -= 1;
     break;
    case SB_LINERIGHT:
     H_ScrollValue += 1;
     break;
    case SB_PAGELEFT:
     H_ScrollValue -= 5;
     break;
    case SB_PAGERIGHT:
     H_ScrollValue += 5;
     break;
    case SB_THUMBTRACK:
     H_ScrollValue = HIWORD(wParam);
     break;
    }
    SetScrollPos(hwnd,SB_HORZ,H_ScrollValue,TRUE);
    InvalidateRect(hwnd,NULL,TRUE);
    return 0;
     case WM_KEYDOWN:
          switch(wParam)
          {
          case VK_HOME:
               xCaret = 0;
               break;  
          case VK_END:
               xCaret = cxBuffer – 1;
               break;
          case VK_PRIOR:
               yCaret = 0;
               break;
          case VK_NEXT:
               yCaret = cyBuffer – 1;
               break;
              
          case VK_LEFT:
               xCaret = max(xCaret – 1, 0);
               break;
              
          case VK_RIGHT:
               xCaret = min(xCaret + 1, cxBuffer – 1);
               break;
          case VK_UP:
               yCaret = max(yCaret – 1, 0);
               break;
          case VK_DOWN:
               yCaret = min(yCaret + 1, cyBuffer – 1);
               break;
          case VK_DELETE:
               for(x = xCaret; x < cxBuffer – 1; x++)
                    BUFFER(x, yCaret) = BUFFER(x + 1, yCaret);
              
               BUFFER(cxBuffer – 1, yCaret) = ‘ ‘;
               HideCaret(hwnd);
               hdc = GetDC(hwnd);
               SelectObject(hdc, CreateFont (0, 0, 0, 0, 0, 0, 0,
0,dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
                   
               TextOut(hdc, xCaret * cxChar, yCaret * cyChar,
&BUFFER(xCaret, yCaret),cxBuffer – xCaret);

  X=X+tm.tmAveCharWidth*4;         //设置输出字符的X,Y坐标
  Y=tm.tmExternalLeading+70;          //提高10个像素
  hF
=CreateFont(30,0,0,0,FW_THIN,1,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,”字号”);

               DeleteObject(SelectObject(hdc,
GetStockObject(SYSTEM_FONT)));
               ReleaseDC(hwnd, hdc);
               ShowCaret(hwnd);
               break;
          }
          SetCaretPos(xCaret * cxChar, yCaret * cyChar);
          return 0;
         
     case WM_CHAR:
          for(i = 0; i < (int)LOWORD(lParam); i++)
          {
               switch(wParam)
               {
               case ‘b’:                    // backspace
                    if (xCaret > 0)
                    {
                         xCaret–;
                         SendMessage(hwnd, WM_KEYDOWN, VK_DELETE,
1);
                    }
                    break;
                   
               case ‘t’:                    // tab
                    do
                    {
                         SendMessage(hwnd, WM_CHAR, ‘ ‘, 1);
                    }
                    while (xCaret % 8 != 0);
                    break;
                   
               case ‘n’:                    // line feed换行
                    if (++yCaret == cyBuffer)
                         yCaret = 0;
                    break;
                   
               case ‘r’:                    // carriage return回车
                    xCaret = 0;
                   
                    if (++yCaret == cyBuffer)
                         yCaret = 0;
                    break;
                   
               case ‘x1B’:                  // escape   ESC键
                    for (y = 0; y < cyBuffer; y++)
                         for (x = 0; x < cxBuffer; x++)
                              BUFFER (x, y) = ‘ ‘;
                        
                    xCaret = 0;
                    yCaret = 0;
                        
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
                        
               default:                      // character codes
                    BUFFER(xCaret, yCaret) = (TCHAR)wParam;
                    HideCaret(hwnd);
                    hdc = GetDC(hwnd);
                    SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0,
0,dwCharSet, 0, 0, 0, FIXED_PITCH, NULL));
                    TextOut(hdc, xCaret * cxChar, yCaret * cyChar,&
BUFFER(xCaret, yCaret), 1);
                    DeleteObject(SelectObject(hdc,
GetStockObject(SYSTEM_FONT)));
                    ReleaseDC(hwnd, hdc);
                    ShowCaret(hwnd);

  SetTextColor(hdc,RGB(255,0,0));    //设置字体的颜色为红色
  SelectObject(hdc,hF);         //选入字体
  TextOut(hdc,X,Y,&lpsz_1[nChar],1); //输出第nChar个字符

                    if(++xCaret == cxBuffer)
                    {
                         xCaret = 0;
                         if(++yCaret == cyBuffer)
                              yCaret = 0;
                    }
                    break;
               }
          }
          SetCaretPos(xCaret * cxChar, yCaret * cyChar);
          return 0;
     case WM_PAINT:
          hdc = BeginPaint(hwnd, &ps);
          SelectObject(hdc, CreateFont(0, 0, 0, 0, 0, 0, 0, 0,dwCharSet,
0, 0, 0, FIXED_PITCH, NULL));                      
          for (y = 0; y < cyBuffer; y++)
               TextOut(hdc, 0, y * cyChar, &BUFFER(0,y), cxBuffer);

  Y=tm.tmExternalLeading+80; 
  for(i=nChar+1;i<nCharlen;i++)
  { 
   SetTextColor(hdc,RGB(0,0,0));   //设置字体的颜色为黑色
     X=X+tm.tmAveCharWidth*4;        //设置输出字符的X坐标
   TextOut(hdc,X,Y,&lpsz_1[i],1); //输出后面的字符
  }
 
  if(nChar == nCharlen-1)              //当输出到最后的一个字符时
  {
   bRight = FALSE;                  //改变红色字移动的方向为向左
   bLeft = TRUE;
  }
  else if(nChar == 0)                  //当输出到第一个字时
  {
   bRight = TRUE;                  //改变红色字移动的方向为向右
   bLeft = FALSE;
  }
  if(bRight == TRUE)
   nChar ++;
  else
   nChar –;

          DeleteObject(SelectObject(hdc,
GetStockObject(SYSTEM_FONT)));
          EndPaint(hwnd, &ps);
          return 0;
         
     case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
     }
     return DefWindowProc (hwnd, message, wParam, lParam);
}

  X = 0;
  DeleteObject(hF);
  EndPaint(hwnd,&ps);
  break;
 case WM_CLOSE:
  DestroyWindow(hwnd);
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd, Message, wParam, lParam);
 }
 return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图