-
March 25th, 2022, 10:22 AM
#1
CStatic - disable double click copy to clipboard.
Hi all,
by default double-clicking on the label (CStatic) control, you copy the text of the label to the clipboard. To disable this functionality I followed this site. The problem is, that I want to preserve the previous value of the clipboard. Based on MSDN documentation the WM_GETTEXT should return the text length without null character. I do so, but my code still not working, which means that the clipboard is empty.
Code:
LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam) override
{
if (message == WM_LBUTTONDBLCLK)
{
double_click_flag_ = true;
}
if (message == WM_GETTEXT && double_click_flag_ && !double_click_copy_)
{
double_click_flag_ = false;
HANDLE clip = nullptr;
if (OpenClipboard()) {
clip = GetClipboardData(CF_TEXT);
CloseClipboard();
}
string text;
text = (char*)clip;
std::wstring wsTmp(text.begin(), text.end());
return wsTmp.length();
}
return CStatic::WindowProc(message, wParam, lParam);
}
Any idea?
Thx
Last edited by Tony.star21; March 25th, 2022 at 10:25 AM.
-
March 25th, 2022, 11:20 AM
#2
Re: CStatic - disable double click copy to clipboard.
 Originally Posted by Tony.star21
Hi all,
by default double-clicking on the label (CStatic) control, you copy the text of the label to the clipboard. ...
I never heard about such a feature. In my quick tests it (double-clicking on the label (CStatic) control causes the label text being copied to the Clipboard) does not work.
Victor Nijegorodov
-
March 25th, 2022, 11:26 AM
#3
Re: CStatic - disable double click copy to clipboard.
clip is a handle - not a pointer to data. Once you have the handle then you need to obtain a memory pointer from it. For this use GlobalLock(clip). The return value is a pointer to the data. If not nulllptr then the data can be copied.
Note that once the data has been copied then you use GlobalUnlock() and then CloseClipBoard(). Don't close if no error before the data has been copied.
See https://docs.microsoft.com/en-us/win...-the-clipboard for an example.
Last edited by 2kaud; March 25th, 2022 at 12:04 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.2)
-
March 25th, 2022, 11:50 AM
#4
Re: CStatic - disable double click copy to clipboard.
 Originally Posted by 2kaud
clip is a handle - not a pointer to data. Once you have the handle then you need to obtain a memory pointer from it. or this use GlobalLock(clip). The return value is a pointer to the data. If not nulllptr then the data can be copied.
Note that once the data has been copied then you use GlobalUnlock() and then CloseClipBoard(). Don't close if no error before the data has been copied.
See https://docs.microsoft.com/en-us/win...-the-clipboard for an example.
Well, I may not use the clip the correct way but I successfully get the string so the string length is correct. The question is why this is not enough and the clipboard is empty.
-
March 25th, 2022, 12:21 PM
#5
Re: CStatic - disable double click copy to clipboard.
 Originally Posted by Tony.star21
Well, I may not use the clip the correct way but I successfully get the string so the string length is correct. The question is why this is not enough and the clipboard is empty.
I guess it's because nothing has been placed on the clipboard (at least in the CF_TEXT format).
Victor Nijegorodov
-
March 28th, 2022, 08:06 AM
#6
Re: CStatic - disable double click copy to clipboard.
Can someone send here a working example? I still have not figured it out. :/ Shouldn't be that hard. The problem is that something destroys the actual clipboard calling EmptyClipboard() so I receive WM_DESTROYCLIPBOARD msg.
-
March 28th, 2022, 08:21 AM
#7
Re: CStatic - disable double click copy to clipboard.
 Originally Posted by VictorN
I guess it's because nothing has been placed on the clipboard (at least in the CF_TEXT format). 
Not true. I feel like you do not get the point here. When you double click on the label (since vista and SS_NOTIFY style is set) you get the text of the label to the clipboard. In my custom subclassed label I want to create a flag with which you are able to turn on/off this feature (double_click_copy_). The scenario I want to achieve is the following: The user has "cool text" in the clipboard (let's say he hit ctrl+c in the document). Now he accidentally or for some reason double click on the label. The result I want to have is to preserve the "cool text" in the clipboard. With my piece of code, I can catch WM_GetTExt msg and return whatever. But the result is that the clipboard lose its original content and became empty. So back to your "guess" the clipboard in my case is not empty because in the time of getting the WM_GETTEXT msg I am able the get the original content of the clipboard. But I do not know how to preserve it.
-
March 28th, 2022, 09:13 AM
#8
Re: CStatic - disable double click copy to clipboard.
I've never heard of double clicking copying text to the clipboard either, but could you just turn off the SS_NOTIFY style?
-
March 28th, 2022, 09:20 AM
#9
Re: CStatic - disable double click copy to clipboard.
 Originally Posted by GCDEF
I've never heard of double-clicking copying text to the clipboard either, but could you just turn off the SS_NOTIFY style?
Sure, that is possible but then I get no WM_LBUTTONDBCLICK msg at all and this is not an ideal solution for me.
-
March 28th, 2022, 09:28 AM
#10
Re: CStatic - disable double click copy to clipboard.
How about if it's a WM_LBUTTONDBLCLK return 0 instead of calling the base WindowProc, or set up a message handler in your class that ignores the WM_LBUTTONDBLCLK?
The problem is likely that you're still calling the base WindowProc, so it's going to do whatever it does after you do your processing
-
March 28th, 2022, 02:17 PM
#11
Re: CStatic - disable double click copy to clipboard.
 Originally Posted by Tony.star21
Not true. I feel like you do not get the point here. When you double click on the label (since vista and SS_NOTIFY style is set)
Well you didn't mention about SS_NOTIFY style in your OP! 
OK, there is an MFC class (this Forum is for Visual C++ /MFC related problems) derived from CStatic that can prevent copying the control label text to the clipboard:
Code:
// NotifyStatic.h
#pragma once
class CNotifyStatic : public CStatic
{
DECLARE_DYNAMIC(CNotifyStatic)
public:
CNotifyStatic();
protected:
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg LRESULT OnGettext(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
protected:
bool doubleClicked;
CString clipboardText;
CString label;
};
// NotifyStatic.cpp : implementation file
//
#include "pch.h"
#include "resource.h"
#include "NotifyStatic.h"
IMPLEMENT_DYNAMIC(CNotifyStatic, CStatic)
CNotifyStatic::CNotifyStatic()
{
doubleClicked = false;
}
BEGIN_MESSAGE_MAP(CNotifyStatic, CStatic)
ON_WM_LBUTTONDBLCLK()
ON_MESSAGE(WM_GETTEXT, &CNotifyStatic::OnGettext)
END_MESSAGE_MAP()
void CNotifyStatic::OnLButtonDblClk(UINT nFlags, CPoint point)
{
HANDLE clip = nullptr;
if (::OpenClipboard(m_hWnd))
{
clip = ::GetClipboardData(CF_TEXT);
::CloseClipboard();
}
if (clip != nullptr)
{
void* text = ::GlobalLock(clip);
if (text != nullptr)
{
// save previous clipboard data in a CString member variable
if (::IsClipboardFormatAvailable(CF_TEXT))
{
clipboardText = (char*)text;
}
else if (::IsClipboardFormatAvailable(CF_UNICODETEXT))
{
clipboardText = (wchar_t*)text;
}
::GlobalUnlock(clip);
}
}
doubleClicked = true;
// delete the next line if you want to prevent copying control label to the (or just the emptying) clipboard
CStatic::OnLButtonDblClk(nFlags, point);
}
afx_msg LRESULT CNotifyStatic::OnGettext(WPARAM wParam, LPARAM lParam)
{
if (doubleClicked)
{
doubleClicked = false;
// return zero to prevent setting the text to the clipboard
return 0;
}
// do the default processing
return DefWindowProc(WM_GETTEXT, wParam, lParam);
}
Victor Nijegorodov
-
April 9th, 2022, 02:53 PM
#12
Re: CStatic - disable double click copy to clipboard.
Hey,
i am not sure on this, after reading this, i had the same doubt, lol.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|