Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
RELEVANT INFORMATION:

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.

QUESTION:

I do not know how to construct the following if statement ( the following is pseudo code ):
C++
if( clipboard content is OK )
    defaul handler;
else
    discard message;


MY EFFORTS TO SOLVE THIS:

So far this is what I have in mind:
C++
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);
}


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

Thank you.

Best regards.
Posted

1 solution

Looks very good, I would solely check the return value of GlobalLock() like this:
C++
// ...
wchar_t *pchData = (wchar_t*)GlobalLock(hClipboardData);

if (NULL != pchData)
{
   // ... whatever
   GlobalUnlock(hClipboardData);
} 
// ...


You can take a look at this exsample: http://msdn.microsoft.com/en-us/library/windows/desktop/ms649016%28v=vs.85%29.aspx#_win32_Pasting_Information_from_the_Clipboard[^]
 
Share this answer
 
v3
Comments
AlwaysLearningNewStuff 8-Mar-14 13:41pm    
Thank you. Best regards. 5ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900