c++ - 正确处理子类过程中的 WM_PASTE

标签 c++ winapi subclass

相关信息:

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

我已经成功获取到剪贴板的内容了,至少我是这么认为的。

问题:

不知如何构造如下if语句(以下为伪代码):

if( clipboard content is OK )
    defaul handler;
else
    discard message;

我为解决这个问题所做的努力:

到目前为止,这是我的想法:

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 语句

谢谢。

最好的问候。

最佳答案

从所采取的行动来看,代码似乎是合理的。有点笨拙,但那是 Windows API。可能有更好的方法,但这应该可行。

一个错误:如果文本没问题,你应该调用 DefSubclassProc,而不是默认的窗口过程。

如果文本不正确,您可以考虑清空剪贴板。这里没有足够的关于你的其他要求来谈论那个。

关于c++ - 正确处理子类过程中的 WM_PASTE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22263612/

相关文章:

c++ - 实现 C++ grpc 异步客户端时如何轮询 CompletionQueue?

c# - 依赖于 Visual C++ 2013 运行时的 NuGet 包

c# - 欺骗 Windows 以查看第二台显示器

c++ - 为什么调用了不合适的重载函数?

windows - 以编程方式捕捉到扬声器的声音的最佳向后兼容方式

c# - 是否可以在自己的 .Net 应用程序中嵌入 Windows Sidebar Widgets

c# - 如何让子类实现接口(interface)?

iOS 从导航 Controller 子类设置工具栏项

ios - 子类化 NSInputStream,重写委托(delegate)?

c++ - 无序映射比 C++ 中的映射慢吗?