How to disable MFC Edit control popup menu additional items? - c++

Is there a clean and easy way to disable "Right to left reading order" and Unicode related messages from a context popup menu for an edit control. Yes, I know that I can subclass and intercept WM_CONTEXTPOPUP, then walk the menu. Attached is the image with menu items in question.
I

I know you said you don't want to subclass, but I don't think it's that painful.
Derive from CEdit, in this case I used the class name CEditContextMenu and add WM_CONTEXTMENU to your message map:
EditContextMenu.cpp
// ...
BEGIN_MESSAGE_MAP(CEditContextMenu, CEdit)
ON_MESSAGE(WM_CONTEXTMENU, &CEditContextMenu::OnContextMenu)
END_MESSAGE_MAP()
// CEditContextMenu message handlers
LRESULT CEditContextMenu::OnContextMenu(WPARAM wParam, LPARAM lParam){
HWINEVENTHOOK hWinEventHook{
SetWinEventHook(EVENT_SYSTEM_MENUPOPUPSTART, EVENT_SYSTEM_MENUPOPUPSTART, NULL,
[](HWINEVENTHOOK hWinEventHook, DWORD Event, HWND hWnd, LONG idObject,
LONG idChild, DWORD idEventThread, DWORD dwmsEventTime){
if (idObject == OBJID_CLIENT && idChild == CHILDID_SELF){
CMenu* pMenu{
CMenu::FromHandle((HMENU)::SendMessage(
hWnd, MN_GETHMENU, NULL, NULL))
};
pMenu->EnableMenuItem(32768, MF_DISABLED);
}
},
GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_OUTOFCONTEXT)
};
LRESULT ret{ Default() };
UnhookWinEvent(hWinEventHook);
return ret;
}
// ...
Maybe you could do something fancy and watch for WS_EX_RTLREADING and block it some how.
At the end of the day you want to change how the OS functions at a low level. I don't think there is an elegant way to do it organically.

Related

C++ Win32 notification menu changes

I have two issues, which may have the same solution, if not I'll split it.
My notification tray icon and menu work well. I can respond to a click and open a window or perform an action.
I'm just trying to interact(change) with the menu itself. I'm using Visual Studio 2019, Win32 API (not MFC) and a resource for a menu.
How can I change text in a menu? (example, initial display "Start Action", once clicked change to "Stop Action")
How can I add / change an icon next to the text? (example, "Start Action" has bitmap1, "Stop Action" as bitmap2)
Everything is in classes, so I've just included the relevant menu code.
constructor:
hmenu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_MENU1));
menuTrackPopup = GetSubMenu(hmenu, 0);
WndProc:
case WM_RBUTTONUP:
{
POINT pCursor;
GetCursorPos(&pCursor);
SetForegroundWindow(hWnd);
TrackPopupMenu(hmenuTrackPopup, TPM_BOTTOMALIGN | TPM_LEFTALIGN, pCursor.x, pCursor.y, 0, hWnd, NULL);
}
I've tried different things like SetMenuItemBitmaps and MenuItemInfo and others but hopefully I'm just loading it wrong.
-- EDIT--
Due to frustrations in answers quoting references to MSDN, with no other explanation or backup code, and still getting up votes, I'm going to pad my questions with the references people post after I have stated I have tried it.
I would have though an answer on how to change the text or icon on a context menu be easy, it's a common feature on any windows application.
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setmenuitembitmaps
BOOL SetMenuItemBitmaps(
HMENU hMenu,
UINT uPosition,
UINT uFlags,
HBITMAP hBitmapUnchecked,
HBITMAP hBitmapChecked
);
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setmenuiteminfow
BOOL SetMenuItemInfoW(
HMENU hmenu,
UINT item,
BOOL fByPositon,
LPCMENUITEMINFOW lpmii
);
https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-menuiteminfow
typedef struct tagMENUITEMINFOW {
UINT cbSize;
UINT fMask;
UINT fType;
UINT fState;
UINT wID;
HMENU hSubMenu;
HBITMAP hbmpChecked;
HBITMAP hbmpUnchecked;
ULONG_PTR dwItemData;
LPWSTR dwTypeData;
UINT cch;
HBITMAP hbmpItem;
} MENUITEMINFOW, *LPMENUITEMINFOW;

Display formatted text on selecting item in the Combobox

I have a combobox in that I want to display different string on selecting an item in Combo.
My combo box is a dropdown combobox.
For eg: I have following in my combobox.
Alex - Manager
Rain - Project Lead
Shiney - Engineer
Meera - Senior Engineer
OnSelecting an item in combobox I want to diaply only name i.e. Alex.
I tried below code
struct details{
CString name;
CString des;
};
BOOL CComboTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
details d1;
d1.name = _T("alex");
d1.des =_T("manager");
m_vec.push_back(d1);
details d2;
d2.name = _T("Rain");
d2.des =_T("Engineer");
m_vec.push_back(d2);
// TODO: Add extra initialization here
for(int i=0;i<m_vec.size();i++)
{
m_ctrlCombo.AddString(m_vec[i].name+m_vec[i].des);
m_ctrlCombo.SetItemData(i,(DWORD_PTR)&m_vec[i]);
}
m_ctrlCombo.SelectString(-1,m_vec[0].name);
m_ctrlCombo.SetWindowText(m_vec[0].name);
return TRUE; // return TRUE unless you set the focus to a control
}
void CComboTestDlg::OnCbnSelchangeCombo1()
{
int nItem = m_ctrlCombo.GetCurSel();
details* det = (details*)m_ctrlCombo.GetItemData(nItem);
PostMessage(SETCOMBOTEXT,IDC_COMBO1,(LPARAM)(LPCTSTR)det->name);
}
BOOL CComboTestDlg::PreTranslateMessage(MSG* pMsg)
{
MSG msg1=*pMsg;//I am loosing the value after checking ..so storing temp.
MSG msg;
CopyMemory(&msg, pMsg, sizeof(MSG));
HWND hWndParent = ::GetParent(msg.hwnd);
while (hWndParent && hWndParent != this->m_hWnd)
{
msg.hwnd = hWndParent;
hWndParent = ::GetParent(hWndParent);
}
if (pMsg->message==SETCOMBOTEXT && (pMsg->wParam == IDC_COMBO1))
SetDlgItemText(IDC_COMBO1, (LPCTSTR)pMsg->lParam);
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==VK_RETURN && msg.hwnd ==m_ctrlCombo.m_hWnd )
{
OnCbnSelchangeCombo1();
}
}
return CDialog::PreTranslateMessage(pMsg);
}
I am able to achieve my requirement OnComboSelChange() and Arrow Keys event but on pressing enter key after using arrow keys in combo box, it is not showing formatted text in combo box.
I think the most reliable and easy to implement solution is to subclass the edit control of the combobox. Intercept the WM_SETTEXT message and change the text as you like before forwarding it to the rest of the chain (finally the original window proc).
Install the sub class proc in OnInitDialog():
COMBOBOXINFO cbi{ sizeof(cbi) };
if( m_ctrlCombo.GetComboBoxInfo( &cbi ) )
{
SetWindowSubclass( cbi.hwndItem, ComboEditSubClassProc, 0, 0 );
}
ComboEditSubClassProc() could look like this:
LRESULT CALLBACK ComboEditSubClassProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
{
switch( uMsg )
{
case WM_SETTEXT:
{
CString text = reinterpret_cast<LPCTSTR>( lParam );
// Extract the name (everything before "-").
CString name = text.SpanExcluding( _T("-") );
name.TrimRight();
// Forward the modified text to any other sub class procs, aswell
// as the original window proc at the end of the chain.
return DefSubclassProc( hWnd, uMsg, 0, reinterpret_cast<LPARAM>( name.GetString() ) );
}
case WM_NCDESTROY:
{
// We must remove our subclass before the subclassed window gets destroyed.
// This message is our last chance to do that.
RemoveWindowSubclass( hWnd, ComboEditSubClassProc, uIdSubclass );
break;
}
}
return DefSubclassProc( hWnd, uMsg, wParam, lParam );
}
Notes:
Contrary to my original solution of processing CBN_SELCHANGE, the current solution also works correctly if the combobox drop-down list is closed by pressing Return or is dismissed.
I think it is in general more reliable because we don't have to rely on the order of the notifications. The combobox has to finally call WM_SETTEXT to change the content of the edit control so this message will always be received.
There will also be no flickering as in the original solution where the text was first changed by the combobox and then modified by our code only after the fact.

How to detect whether the focused window is an Edit 'type' control?

How can I detect whether the focused window is an Edit 'type' control? One method I am aware of is using Microsoft Active Accessibility which is looking like it will involve alot of effort using this method.
Is there another method I could use that is simpler?
My use-case is: when an edit control has the focus, store that hwnd.
// Callback set by SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS, NULL, (WINEVENTPROC)&winEventProc, 0, 0, WINEVENT_SKIPOWNPROCESS);
void CALLBACK KeyboardComponent::winEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject,
LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
// if hwnd == "Edit Control" store hwnd to send key input events
// One technique but not comprehensive
TCHAR wndClassName[255];
GetClassName(hwnd, wndClassName, 255);
if (_tcsicmp(wndClassName, _T("edit")) == 0)
targetEdit = hwnd;
// Class names I am receiving are subclassed or new window classes that look and operate like Edit controls.
// Ie when clicking the Firefox address bar I get: MozillaWindowClass
// Ie when clicking the Chrome address bar I get: Chrome_WidgetWin_1
}
Active Accessibility is the correct solution. You can use AccessibleObjectFromEvent() to get an IAccessible interface for the HWND that is triggering your winEventProc hook, and then check the IAccessible::AccRole property for ROLE_SYSTEM_TEXT:
ROLE_SYSTEM_TEXT
The object represents selectable text that allows edits or is designated as read-only.
For example:
// Callback set by SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS, NULL, (WINEVENTPROC)&winEventProc, 0, 0, WINEVENT_SKIPOWNPROCESS);
void CALLBACK KeyboardComponent::winEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject,
LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
IAccessible* pAcc = NULL;
VARIANT varChild;
HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);
if ((hr == S_OK) && (pAcc != NULL))
{
VARIANT varRole;
hr = pAcc->get_accRole(varChild, &varRole);
if ((hr == S_OK) && (varRole.vt == VT_I4) && (varRole.lVal == ROLE_SYSTEM_TEXT))
{
// ...
}
pAcc->Release();
}
}
For reliable result try using RealGetWindowClass, it should deal with a case when window was subclassed. If you don't care about that use regular GetClassName.
In case you want to handle controls that are not derived from standard Edit class, none of that will work of course.
You can use this code to know if focused window is Edit control
CWnd* pControl;
pControl = this->GetFocus();
if(pControl->IsKindOf(RUNTIME_CLASS(CEdit))){
//----
-----
----//
}
Get more details Here
Convert Cwnd to HWND
pControl->GetSafeHwnd();

PostMessage(), SendMessage not working in ATL dll (event handling)

sorry, my english skill is very low.
i make a ATL(C++) dll. and handled by VB.
i make under base code.
WaitAndReadData, Thread_WaitAndReadData is working.
but, ::SendMessage, ::PostMessage is not working in Thread_WaitAndReadData or WaitAndReadData.
and breakpoint not working in Get_Data_Messagehandler.
(+ another function call.)
#define WM_SERVERTHREADFIREEVENT (WM_USER+2)
BEGIN_MSG_MAP(CHello)
CHAIN_MSG_MAP(CComControl<CHello>)
MESSAGE_HANDLER(WM_SERVERTHREADFIREEVENT, GetData_Messagehandler)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
-
static DWORD WINAPI Thread_WaitAndReadData(LPVOID pParam)
-
STDMETHODIMP CHello::WaitAndReadData(BSTR* ret_Result)
{
// TODO: Add your implementation code here
DWORD dwThreadID;
thread = CreateThread(NULL, 0, Thread_WaitAndReadData, (LPVOID)this, 0, &dwThreadID);
return S_OK;
}
-
DWORD WINAPI CHello::Thread_WaitAndReadData(LPVOID pParam)
{
CHello* hello = (CHello*)pParam;
::SendMessage(hello->m_hWnd, WM_SERVERTHREADFIREEVENT, (WPARAM)NULL, (LPARAM)NULL);
return S_OK;
}
-
LRESULT CHello::GetData_Messagehandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
MessageBox(L"GetData_Messagehandler", L"asd", MB_OK);
return 0;
}
Even though MSDN states that there is no marshaling of WM_USER + x messages in cross-process sending, if my memory serves me right you might have troubles with cross-thread sending as well. In this case use RegisterWindowMessage API to obtain "sendable" WM_xxx identifier rather than harcoding it using a #define
Don't use bare CreateThread, use AtlCreateThread instead (or, _beginthreadex). See why.
Another reason to not receive messages on window thread is thread deadlock or window creation on thread that does not have a message pump later on, in both cases a message might be sent but there is no dispatching it to window. You can also use Spy++ tool (spyxx.exe in Visual Studio Comment\Tools directory) to make sure that the message in question is indeed being sent to the window.

Activate Window from code problem

I have a window on my desktop called: "Kaspersky Anti-Virus Configuration Wizard"
Here is some info about the window:
>>>> Window <<<<
Title: Kaspersky Anti-Virus Configuration Wizard
Class: AVP.ConfigureWizard
Position: 612, 247
Size: 499, 388
Style: 0x94CA0044
ExStyle: 0x00010100
Handle: 0x00081308
The window does not appear in the Windows Task Manager Tasks list (only it's process exists in processes list as "avp.exe" what, as far as i think, making it hard for me to acheive my goal. First of all i would appriciate that someone will explain how can Kaspersky Programmed i window that does not exists in "Application" Tab in "Windows Task Manager". Secondly I would be very thankful if you can help me solve my problem which is detailed here:
I want to make the window activeate (Set Focus on the window) from code (C++ \ Autoit).
I tried to use the FindWindow function of WinAPI but couldn't get the handle to this window.
I got the handle with GetForegroundWindow function and I've found out that when i use EnumWindows function the handle to kaspersky configuration window was not in the list..
this was my code:
BOOL CALLBACK EnumWindowsProc(__in HWND hwnd, __in LPARAM lParam)
{
if(g_hWnd == hwnd)
{
cout << "Found window";
return FALSE;
}
return TRUE;
}
BOOL CALLBACK EnumDesktopProc(
__in LPTSTR lpszDesktop,
__in LPARAM lParam
)
{
EnumDesktopWindows(OpenDesktop(lpszDesktop,DF_ALLOWOTHERACCOUNTHOOK,FALSE,DESKTOP_ALL),EnumWindowsProc, NULL);
return true;
}
BOOL CALLBACK EnumWindowStationProc(
__in LPTSTR lpszWindowStation,
__in LPARAM lParam
)
{
EnumDesktops(OpenWindowStation(lpszWindowStation,FALSE, WINSTA_ALL_ACCESS),EnumDesktopProc, NULL );
return true;
}
int main()
{
Sleep(3000);
g_hWnd = GetForegroundWindow(); //Here i switch to kaspersky window to get it's handle
EnumWindowStations(EnumWindowStationProc, NULL); //I call EnumDesktopWindows in EnumDesktops in EnumWindowStations to search in all HWND of my Operation System.
}
the cout << "Found Window" statement never executed.
I would be very grateful if you could help me slove this and show me the ability to make this window active.