相关信息:
我有一个子类过程,需要在粘贴剪贴板之前先验证剪贴板的内容。
我设法成功获取了剪贴板的内容,至少我是这样认为的。
题:
我不知道如何构造以下if statement
(以下是伪代码):
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 statement
?谢谢。
最好的祝福。
最佳答案
该代码似乎是合理的,具体取决于所采取的措施。有点笨拙,但这是Windows API。也许有更好的方法,但这应该可行。
一个错误:如果文本确定,则应调用DefSubclassProc,而不是默认的窗口过程。
如果文本不正确,则可以考虑清空剪贴板。这里没有足够的关于您的其他要求的讨论。
关于c++ - 在子类过程中正确处理WM_PASTE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22263612/