【Windows编程】系列第十篇:文本插入符

你们知道,在使用微软的编程环境建立工程时会让你选择是控制台模式仍是Windows应用程序。若是选择控制台的console模式,就会在运行时出现一个黑洞洞的字符模式窗口,里面就有等待输入一闪一闪的插入符。输入光标从DOS时代就存在,可是在Win32中赋予了更强大的功能。下图就是Windows的CMD窗口,其中的输入点就是插入光标:java

cmd_window

要注意的是这里的插入符或插入光标并非Windows中另一个“光标”,这里是指示插入字符的位置,而不是用于鼠标,手写输入等能够定位、移动的光标(Cursor),而是插入符Caret,本文也成为插入光标,注意插入二字,为了方便,如下在本文中也简称为光标或插入符,但要注意此光标非彼光标。程序员

为何会有插入光标(插入符)?了解了这个基本问题,就成功了一半了。咱们知道计算机能够经过键盘来输入各类字符和控制符,那么天然就存在一个问题,输入的字符应该放到屏幕的什么位置?这个就是光标产生的缘由,光标实际上就是一个字符插入标识。简单的说就是咱们想把字符输入到哪一个位置,就先把插入符设置到那里,设置时其实就是告诉电脑输入的字符你给我在哪里显示!从这个咱们也能够推断,插入符在同一时刻只能有一个。编程

  • 光标相关API函数小程序

要使用光标,首先得建立一个光标,建立光标的API函数为:windows

BOOL CreateCaret(HWND hWnd, HBITMAP hBitmap, int nWidth, int nHeight);

hWnd参数表示光标是属于哪一个窗口。微信

hBitmap参数是一个位图的句柄,计算机将使用这个句柄的位图来做为光标的形状。app

既然光标是给使用电脑的人插入字符用的,那就得有形状让使用者能看到,所以光标须要有一个可见的小图标。固然为了避免同的状况和需求,Windows让咱们能够自定义光标的形状。常见的位图建立或者加载API函数如CreateBitmap、CreateDIBitmap、LoadBitmap等均可以建立或加载一个位图,将句柄做为该参数。微信公众平台

nWidth和nHeight分别是位图的宽和高。ide

光标建立以后是不会自动显示的,也就是默认是隐藏状态,须要主动调用下面的显示函数:函数

BOOL ShowCaret(HWND hWnd);

固然有显示光标也能够隐藏光标:

BOOL HideCaret(HWND hWnd);

以上两个函数的参数很简单,都是要显示窗口的句柄。

要设置插入符的位置,使用下面API函数:

BOOL SetCaretPos(int X, int Y);

参数X、Y分别是相对于窗口客户区的坐标。

咱们能够用以下API函数获取当前光标的位置:

BOOL GetCaretPos(LPPOINT lpPoint);

参数lpPoint返回当前光标所在的位置。

咱们知道光标会闪烁,这个闪烁的时间间隔是能够设置的,咱们能够用以下API来设置和获取插入光标的闪烁时间:

BOOL SetCaretBlinkTime(UINT uMSeconds);
UINT GetCaretBlinkTime(VOID);

参数uMSeconds为闪烁的间隔毫秒数。

最后再也不使用时须要销毁光标:

BOOL DestroyCaret(VOID);

 

  • 光标处理相关消息

与插入光标相关的消息主要有WM_SETFOCUS、WM_KILLFOCUS。一般在WM_SETFOCUS中建立和显示光标,而在WM_KILLFOCUS中销毁光标。通常应有中再结合WM_KEYDOWN和WM_CHAR消息,实现文本的输入。

  • 光标应用实例

如下是一个简单的虚拟终端,咱们常见的不少终端软件都是这样来实现的,好比常见的SecureCRT、Tera Term、XShell、putty等等。本实例就是用了插入光标来实现字符输入、插入,完整实例代码以下:

#include <windows.h>

static TCHAR szWinName[] = TEXT("VirtualTerminal");
#define TEXTMATRIX(x, y)  *(pTextMatrix + ((y) * nLineChars) + (x))
static TEXTMETRIC tm; //metric dimention of virtual terminal
static TCHAR *pTextMatrix; //VT character buffer
static int nCharWidth, nCharHeight; //the width and height of characeters
static int nCaretPosX, nCaretPosY; //the current position of caret
static int nVTWidth, nVTHeight; //the width and height of virtual terminal window
static int nLineChars, nRowChars; //character number in one line and row
static int nCaretOffsetY; //the offset of vertical height
static COLORREF TextColor; //text color for terminal window, that is fore color

static LRESULT CALLBACK VTWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

HWND CreateVirtualTerminal(HWND hWndParent, int left, int top, int width, int height, int id)
{
    HWND hWnd;
    WNDCLASS wndclass;

    HINSTANCE hInst = GetModuleHandle(NULL);
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = VTWndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInst;
    wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    wndclass.lpszMenuName  = NULL;
    wndclass.lpszClassName = szWinName;
    if (!RegisterClass(&wndclass))
    {
        return 0;
    }
    hWnd = CreateWindowEx(0, szWinName, NULL, WS_CHILD|WS_VISIBLE, 
                        left, top, width, height, hWndParent, (HMENU)id, hInst, NULL);
    return hWnd;
}

static void DrawChar(HDC hDC, int x, int y, TCHAR *str, int num)
{
    RECT rect;
    SelectObject(hDC, GetStockObject(SYSTEM_FIXED_FONT));
    SetTextColor(hDC, TextColor);
    SetBkMode(hDC, TRANSPARENT);
    rect.left = x;
    rect.top = y;
    rect.right = x + num * nCharWidth;
    rect.bottom = y + nCharHeight;
    FillRect(hDC, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
    TextOut(hDC, nCaretPosX * nCharWidth, nCaretPosY * nCharHeight, 
            &TEXTMATRIX(nCaretPosX, nCaretPosY), nLineChars - nCaretPosX);
}

static LRESULT CALLBACK VTWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int x, y;
    HDC hDC;

    switch (message)
    {
    case WM_CREATE:
        hDC = GetDC(hWnd);
        SelectObject(hDC, GetStockObject(SYSTEM_FIXED_FONT));
        GetTextMetrics(hDC, &tm);
        ReleaseDC(hWnd, hDC);
        nCharWidth = tm.tmAveCharWidth;
        nCharHeight = tm.tmHeight;
        TextColor = RGB(255, 255, 255);
        nCaretOffsetY = 12;
        return 0;

    case WM_SIZE:
        nVTWidth = LOWORD(lParam);
        nLineChars = max(1, nVTWidth/nCharWidth);
        nVTHeight = HIWORD(lParam);
        nRowChars = max(1, nVTHeight/nCharHeight);
        
        if (pTextMatrix != NULL)
        {
            free(pTextMatrix);
        }
        pTextMatrix = (TCHAR *)malloc(nLineChars * nRowChars);
        if (pTextMatrix)
        {
            for (y=0; y<nRowChars; y++)
            {
                for (x=0; x<nLineChars; x++)
                {
                    TEXTMATRIX(x, y) = TEXT(' ');
                }
            }
        }
        SetCaretPos(0, nCaretOffsetY);
        return 0;

    case WM_LBUTTONDOWN:
        SetFocus(hWnd);
        break;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_HOME:       // Home 
            nCaretPosX = 0;
            break; 

        case VK_END:        // End 
            nCaretPosX = nLineChars - 1;
            break; 

        case VK_PRIOR:      // Page Up 
            nCaretPosY = 0; 
            break; 

        case VK_NEXT:       // Page Down 
            nCaretPosY = nRowChars -1; 
            break; 

        case VK_LEFT:       // Left arrow 
            nCaretPosX = max(nCaretPosX - 1, 0); 
            break; 

        case VK_RIGHT:      // Right arrow 
            nCaretPosX = min(nCaretPosX + 1, 
                nLineChars - 1); 
            break; 

        case VK_UP:         // Up arrow 
            nCaretPosY = max(nCaretPosY - 1, 0); 
            break; 

        case VK_DOWN:       // Down arrow 
            nCaretPosY = min(nCaretPosY + 1, 
                nRowChars - 1); 
            break; 

        case VK_DELETE:     // Delete 
            // Move all the characters that followed the 
            // deleted character (on the same line) one 
            // space back (to the left) in the matrix. 
             for (x = nCaretPosX; x < nLineChars; x++) 
                TEXTMATRIX(x, nCaretPosY) = TEXTMATRIX(x + 1, nCaretPosY); 
            // Replace the last character on the 
            // line with a space. 
            TEXTMATRIX(nLineChars - 1, nCaretPosY) = ' '; 
            // The application will draw outside the 
            // WM_PAINT message processing, so hide the caret. 
            HideCaret(hWnd); 
            // Redraw the line, adjusted for the 
            // deleted character. 
            hDC = GetDC(hWnd); 
            DrawChar(hDC, nCaretPosX * nCharWidth, nCaretPosY * nCharHeight,
                    &TEXTMATRIX(nCaretPosX, nCaretPosY), nLineChars - nCaretPosX/nCharWidth);
            ReleaseDC(hWnd, hDC); 

            // Display the caret. 
            ShowCaret(hWnd); 
        break; 
        } 
        // Adjust the caret position based on the 
        // virtual-key processing. 
        SetCaretPos(nCaretPosX * nCharWidth, nCaretPosY * nCharHeight + nCaretOffsetY); 
        return 0;

    case WM_SHOWWINDOW:
        SetFocus(hWnd);
        break;

    case WM_SETFOCUS:
        CreateCaret(hWnd, NULL, nCharWidth, 2);
        SetCaretPos(nCaretPosX * nCharWidth, nCaretPosY * nCharHeight + nCaretOffsetY);
        ShowCaret(hWnd);
        break;

    case WM_KILLFOCUS:
    case WM_DESTROY:
        HideCaret(hWnd);
        DestroyCaret();
        break;

    case WM_CHAR:
        switch (wParam)
        {
        case 0x08: //process a backspace.
            if (nCaretPosX > 0)
            {
                nCaretPosX--;
                SendMessage(hWnd, WM_KEYDOWN, VK_DELETE, 1L);
            }
            break;
        case 0x09: //process a tab.
            do
            {
                SendMessage(hWnd, WM_CHAR, TEXT(' '), 2L);
            } while (nCaretPosX % 4 != 0);
            break;
        case 0x0D: //process a carriage return.
            nCaretPosX = 0;
            if (++nCaretPosY == nRowChars)
            {
                nCaretPosY = 0;
            }
            break;
        case 0x1B:
        case 0x0A: //process a linefeed.
            MessageBeep((UINT)-1);
            break;
        default:
            TEXTMATRIX(nCaretPosX, nCaretPosY) = (TCHAR)wParam;
            HideCaret(hWnd);
            hDC = GetDC(hWnd);
            DrawChar(hDC, nCaretPosX * nCharWidth, nCaretPosY * nCharHeight, &TEXTMATRIX(nCaretPosX, nCaretPosY), 1);
            ReleaseDC(hWnd, hDC);
            ShowCaret(hWnd);
            if (++nCaretPosX == nLineChars)
            {
                nCaretPosX = 0;
                if (++nCaretPosY == nRowChars)
                {
                    nCaretPosY = 0;
                }
            }
            break;
        }
        SetCaretPos(nCaretPosX * nCharWidth, nCaretPosY * nCharHeight + nCaretOffsetY);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            hDC = BeginPaint(hWnd, &ps);
            SelectObject(hDC, GetStockObject(SYSTEM_FIXED_FONT));
            SetBkMode(hDC, TRANSPARENT);
            SetTextColor(hDC, TextColor);
            for (y=0; y<nLineChars; y++)
            {
                TextOut(hDC, 0, y * nCharHeight, &TEXTMATRIX(0, y), nLineChars);
            }
            EndPaint(hWnd, &ps);
        }
        return 0;
    default:
        break;
    }

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

下面是主程序文件,该文件调用上面的CreateVirtualTerminal函数来建立一个窗口。

#include <windows.h>

#define IDC_VTID  9876
static TCHAR szAppName[] = TEXT("Caret demo");
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
extern HWND CreateVirtualTerminal(HWND hWndParent, int left, int top, int width, int height, int id);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
     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("This program requires Windows NT!"), szAppName, MB_ICONERROR);
          return 0;
     }
     
     hWnd = CreateWindow(szAppName,             // window class name
                          szAppName,            // window caption
                          WS_OVERLAPPEDWINDOW,  // window style
                          CW_USEDEFAULT,        // initial x position
                          CW_USEDEFAULT,        // initial y position
                          400,              // initial x size
                          300,              // initial y size
                          NULL,                 // parent window handle
                          NULL,                 // window menu handle
                          hInstance,            // program instance handle
                          NULL);                // creation parameters
     
     ShowWindow(hWnd, iCmdShow);
     UpdateWindow(hWnd);
     
     while (GetMessage(&msg, NULL, 0, 0))
     {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
     }

     return msg.wParam;
}

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC         hDC;
    PAINTSTRUCT ps;

    switch (message)
    {
    case WM_CREATE:
        CreateVirtualTerminal(hWnd, 10, 10, 360, 240, IDC_VTID);
        return 0;

    case WM_PAINT:
        hDC = BeginPaint(hWnd, &ps);
        ;
        EndPaint(hWnd, &ps);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0 ;
    }

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

示例程序运行后,在窗口中输入部分文本(模仿上面的cmd窗口^_^),效果以下:

my_cmd

本例实现了一个简单的终端模拟小程序,为了读者重用方便,我将终端模拟的小窗口单独做为一个完整的源文件,而且把窗口背景设为黑色,前景色设为白色,看起来更像CMD、Linux等命令行窗口。

 更多经验交流能够加入Windows编程讨论QQ群454398517


关注微信公众平台:程序员互动联盟(coder_online),你能够第一时间获取原创技术文章,和(java/C/C++/Android/Windows/Linux)技术大牛作朋友,在线交流编程经验,获取编程基础知识,解决编程问题。程序员互动联盟,开发人员本身的家。

【Windows编程】系列第八篇:建立通用对话框

转载请注明出处http://www.coderonline.net/?p=1905,谢谢合做!

相关文章
相关标签/搜索