在子类过程中正确处理WM_PASTE [英] Properly handle WM_PASTE in subclass procedure

查看:204
本文介绍了在子类过程中正确处理WM_PASTE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a subclass procedure that needs to validate the content of the clipboard before it gets pasted.

我已成功地获取剪贴板的内容,至少我认为这样。

I have managed to get the content of the clipboard successfully, at least I think so.

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

I do not know how to construct the following if statement ( the following is pseudo code ):

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



我的努力来解决这个问题:



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

MY EFFORTS TO SOLVE THIS:

So far this is what I have in mind:

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

Is my idea correct or is there another way to form my if statement?

谢谢。


推荐答案

代码似乎是可信的,直到采取的行动。 Bit clunky,但是这是Windows API。

The code seems plausible, down to the action taken. Bit clunky, but that's Windows API. There may be better ways, but this should work.

一个错误:如果文本正常,您应该调用DefSubclassProc,而不是默认的窗口过程。

One mistake: if the text is OK, you should call DefSubclassProc, not the default window procedure.

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

If the text is not OK you could consider emptying the clipboard. There is not enough here about your other requirements to talk about that.

这篇关于在子类过程中正确处理WM_PASTE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆