C++ 在子类过程中正确处理WM_粘贴 有关资料:

C++ 在子类过程中正确处理WM_粘贴 有关资料:,c++,winapi,subclass,C++,Winapi,Subclass,我有一个子类过程,需要在粘贴剪贴板之前验证其内容 我已经成功地获得了剪贴板的内容,至少我认为是这样 问题: 我不知道如何构造以下if语句(以下是伪代码): 我为解决这个问题所做的努力: 到目前为止,我的想法是: LRESULT CALLBACK Decimalni( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData ) {

我有一个子类过程,需要在粘贴剪贴板之前验证其内容

我已经成功地获得了剪贴板的内容,至少我认为是这样

问题: 我不知道如何构造以下
if语句(以下是伪代码):

我为解决这个问题所做的努力: 到目前为止,我的想法是:

LRESULT CALLBACK Decimalni( HWND hwnd, 
    UINT message, WPARAM wParam, LPARAM lParam, 
    UINT_PTR uIdSubclass, DWORD_PTR dwRefData )

{
    switch (message)
    {
    case WM_PASTE:
        {
            bool IsTextValid = true; // indicates validity of text

            if( OpenClipboard(hwnd) ) // open clipboard
            {
                HANDLE hClipboardData;

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

                    wchar_t *pchData = (wchar_t*)GlobalLock(hClipboardData);

                    // copy clipboard data so we can free clipboard

                    wchar_t result[10]; // I just need first 9 characters
                    memset( result, L'0', sizeof(result) );

                    // copy clipboard data WITH TRUNCATION!!!
                    wcsncpy_s( result, 10, pchData, _TRUNCATE );

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

                    /*** parse the text for validity ****/
                    // code for parsing text 
                    // update IsTextValid to indicate success or fail
                    /*** end of parsing *******/

                }

                // 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();
            }

            // here should be the problematic if statement
            if( IsTextValid )
                return ::DefSubclassProc( hwnd, message, wParam, lParam);
            else
                return FALSE;
        }
        break;
    case WM_CHAR:
        {
            // filter out some invalid keys
        }
        break;
    case WM_NCDESTROY:
        ::RemoveWindowSubclass( hwnd, Decimalni, 0 ); // remove subclassing
        break;
    }
    return ::DefSubclassProc( hwnd, message, wParam, lParam);
}
我的想法正确吗?或者是否有其他方法来形成我的
if语句

多谢各位


致以最诚挚的问候。

代码似乎合理,具体到所采取的行动。有点笨重,但那是WindowsAPI。也许有更好的办法,但这应该行得通

一个错误:如果文本是OK,那么应该调用defsublassproc,而不是默认的窗口过程


如果文本不好,可以考虑清空剪贴板。关于您的其他需求,这里没有足够的内容来讨论这一点。

谢谢您的帮助。致以最良好的祝愿+1.
LRESULT CALLBACK Decimalni( HWND hwnd, 
    UINT message, WPARAM wParam, LPARAM lParam, 
    UINT_PTR uIdSubclass, DWORD_PTR dwRefData )

{
    switch (message)
    {
    case WM_PASTE:
        {
            bool IsTextValid = true; // indicates validity of text

            if( OpenClipboard(hwnd) ) // open clipboard
            {
                HANDLE hClipboardData;

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

                    wchar_t *pchData = (wchar_t*)GlobalLock(hClipboardData);

                    // copy clipboard data so we can free clipboard

                    wchar_t result[10]; // I just need first 9 characters
                    memset( result, L'0', sizeof(result) );

                    // copy clipboard data WITH TRUNCATION!!!
                    wcsncpy_s( result, 10, pchData, _TRUNCATE );

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

                    /*** parse the text for validity ****/
                    // code for parsing text 
                    // update IsTextValid to indicate success or fail
                    /*** end of parsing *******/

                }

                // 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();
            }

            // here should be the problematic if statement
            if( IsTextValid )
                return ::DefSubclassProc( hwnd, message, wParam, lParam);
            else
                return FALSE;
        }
        break;
    case WM_CHAR:
        {
            // filter out some invalid keys
        }
        break;
    case WM_NCDESTROY:
        ::RemoveWindowSubclass( hwnd, Decimalni, 0 ); // remove subclassing
        break;
    }
    return ::DefSubclassProc( hwnd, message, wParam, lParam);
}