The board should paste one line into another after receiving text from the clipboard

INTRODUCTION AND RELATED INFORMATION:

I have edit controlone that should only accept signed decimal numbers (something like -12.35).

I decided to implement this through subclassing.

The handler WM_CHARseems to work well, and I need to process a few other messages to completely protect the user from entering invalid text. One such message is WM_PASTE.

So far, I managed to correctly get the text from clipboardand discard or send a message depending on whether the copied string is really a decimal number.

The Edit control has a limited input of 12 characters. This is done using a message EM_SETLIMITTEXT.

I use pure Winapiand C++. The use of libraries such as boostetc. is not allowed .

Problem:

So far, I am checking the case where the edit control is empty, and I refuse to paste if the content is invalid. However, the user can select a portion of the text in the edit control and then paste. Something like this (gray characters represent a choice):

Text in edit control: 123 45678.9

Clipboard text: -1A

The resulting string, if I allow the insert, will be 123-1A78.9, which is invalid.

This is the part I need help with:

, , , .

:

  • , ?

  • , ( !)?

. , " ", ?

:

  • , std::, / /etc, , , . , , string:: insert(...), , .

  • , std:: strtod, StackOverflow , , . , , , , .

, , .

.

, .

.

+3
1

- :

case WM_PASTE:
{
    std::wstring cbtext;

    if( !OpenClipboard(hwnd) ) // open clipboard
        return 0;

    // get clipboard data
    HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);
    if( hClipboardData )
    {
        // Call GlobalLock so that to retrieve a pointer
        // to the data associated with the handle returned
        // from GetClipboardData.

        cbtext = (LPWSTR) GlobalLock(hClipboardData);

        // Unlock the global memory.
        GlobalUnlock(hClipboardData);
    }

    // Finally, when finished I simply close the Clipboard
    // which has the effect of unlocking it so that other
    // applications can examine or modify its contents.

    CloseClipboard();

    if (cbtext.empty())
        return 0;

    // format the new text with the clipboard data inserted as needed

    int len = GetWindowTextLengthW(hwnd);
    std::wstring newtext(len, 0);
    if (len > 0)
        GetWindowTextW(hWnd, &newtext[0], len);

    DWORD start, end;
    SendMessageW(hwnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);

    if (end > start)
        newtext.replace(start, end-start, cbtext);
    else
        newtext.insert(start, cbtext);

    // parse the new text for validity

    // code for parsing text 
    if( IsTextValid )
        SetWindowTextW( hwnd, newtext.c_str() );

    return 0;
}
+2

Source: https://habr.com/ru/post/1527176/


All Articles