Clicking a button of another application fails - c++

I am working with a dialog window, which has three buttons: "Save...", "Decrypt" and "OK". According to Spy++, each button has class "XButton". I am trying to press "Decrypt" from another application, so this is what i do:
// "buffer" below contains dialog's caption
hProgramWindow = FindWindowEx(NULL, NULL, NULL, buffer);
// "caption" below contains string "Decrypt"
HWND hButton = FindWindowEx(hProgramWindow, NULL, TEXT("XButton"), caption);
SetFocus(hProgramWindow);
SetActiveWindow(hProgramWindow);
PostMessage(hProgramWindow, WM_COMMAND,
MAKEWORD(GetDlgCtrlID(hButton), BN_CLICKED), (LPARAM)hButton);
// After posting the message, GetLastError() returns "5" (Access denied),
// but the button is clicked
When caption variable has value "Save...", the "Save..." button is clicked (still GetLastError() = 5), the appropriate save dialog appears. But when caption variable has value "Decrypt", the "Decrypt" button is not clicked, no dialog appears. Any suggestions, why it can happen?
By the way, calling EnableWindow(hButton, FALSE); for "Decrypt" button (as well as for "Save..." button) works fine, the it becomes disabled, so that means that the handle is correct.

Related

Get controller who has focus inside CWnd

I've a dialog, CFormView, which holds some buttons and a panel which holds Tabcontrol, radiobuttons, text input fields etc.
So, on my panel, the CWnd, I create my input fields like this:
pEdit = new CEdit();
pEdit->CreateEx(WS_EX_CLIENTEDGE, _T("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP | nAttrMultiline | m_clRect, pclPanel, iID)
Where m_clRect is a CRect, pclPanel is my CWnd, and iID is just the controller ID.
I want to fill my CEdit with text when a button is clicked, but somehow I can't get the controller who has focus.
My first attempt was to call GetFocus(), cast it into a CEdit and add the text, but this just changes the text on my button, of course.
Second attempt was to check for WM_SETFOCUS with ON_WM_SETFOCUS() and keep the previous wnd and cast it and add text, but that just changes the text on my dialog.
Third attempt was to move this to my CWnd but as far as I can see, WM_SETFOCUS is never called.
Edit:
Tried ON_WM_ACTIVATE with ::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) inside my CWnd.
But that's not being called either.
Anybody has an idea what to try next?
You just answered yourself. The correct way to do it is: on the function that handles the
ON_COMMAND(...)
of each button call
pEdit->SetWindowText(_T("text"));.
GetFocus() is wrong, because it will return the button, as when you clicked it, you just finished to put the focus on it. You can get the edit using
CEdit* pEdit= ( CEdit*) GetDlgItem(ID_OF_EDIT);
where ID_OF_EDIT is the value you passed to CreateEx as iId parameter.

MFC editbox get cursor, In OnInitDialog function

How do I get cursor when dialog is start at OnInitDialog function,
without user to move click it, like as follow pic
enter link description here
Other problem is, when the editbox already fill with text, how do I select the text ?
On OnInitDialog function, I see as follow description,
// return TRUE unless you set the focus to a control
so I define a variable to the editbox, the set focus on it,
then return false in OnInitDialog function.
BOOL CInputTestPoint::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此加入額外的初始化
m_editTestPointName.SetFocus();
//return TRUE; // return TRUE unless you set the focus to a control
return false;
// EXCEPTION: OCX 屬性頁應傳回 FALSE
}
For select whole text method is as follow
define CEdit to the edit box and m_editTestPointName.SetSel(0, strTestPointName.GetLength(), true);
((CEdit *)GetDlgItem(IDC_EDIT_INPUT_TP))->SetSel(0, strTestPointName.GetLength(), true);

C++ Win32 Creating a Popup Menu From Resource

I am trying to load a popup "right click" menu, and use the resource file to define the menu items. The picture shows what is happening when I right click, it displays room for 2 items, which is correct, but doesnt show any text.
In the .cpp:
POINT pt;
pt.x = LOWORD (lParam);
pt.y = HIWORD (lParam);
ClientToScreen (hwnd, &pt);
HMENU hMenu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_POPUPMENU));
TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL);
and the resource:
IDR_POPUPMENU MENU DISCARDABLE
BEGIN
MENUITEM "test", IDM_TEST
MENUITEM "Close", IDM_CLOSE
END
any idea on what I am donig wrong?
Thanks.
EDIT: I just tested, and clicking in the "no text displayed" areas, and it sends the correct message. What could be causing it to not display the text?
Found the solution:
HMENU hMenu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_POPUPMENU));
hMenu = GetSubMenu(hMenu, 0);
and resource:
IDR_POPUPMENU MENU DISCARDABLE
BEGIN
POPUP "TEST"
BEGIN
MENUITEM "Test", IDM_TEST
MENUITEM "Close", IDM_CLOSE
END
END
Just had to start the resource entry with a beginning sub menu, TEST does not display, only its menu items do.
Your menu resource is incorrect. It must be a popupmenu.
eg:
IDR_MENU_TRAY MENU
BEGIN
POPUP "ContextMenu"
BEGIN
MENUITEM "ShowWindow", ID_POPUP_SHOWWINDOW
MENUITEM "Exit", ID_POPUP_EXIT
END
END
TrackPopupMenu first parameter is a handle to a submenu associated with an existing menu item.
You can see the examples here: http://msdn.microsoft.com/EN-US/library/ms647558(v=VS.85,d=hv.2).aspx

MFC tooltips only show up on special occasions

I have been tasked with assigning tooltips to each item in a configuration menu. I have completed "adding" the tooltip to each control on the page, but it seems sometimes the tooltip shows up and sometimes it does not, depending on the position of the control on the screen.
To tooltip-erize the pages I first
EnableToolTips(TRUE);
In each CPropertyPage's OnInitDialog method.
I then add the notification map
ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipText)
With the function OnToolTipText looking as such
BOOL CCfgPrefPage::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
UINT nID = pNMHDR->idFrom;
if (pTTT->uFlags & TTF_IDISHWND)
{
nID = ::GetDlgCtrlID((HWND)nID);
if(nID)
{
if( nID == GetDlgItem(IDC_PICKDIST_EDIT)->GetDlgCtrlID())
_tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);
else if( nID == GetDlgItem(IDC_ENDPTTOL_EDIT)->GetDlgCtrlID())
_tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);
pTTT->lpszText = pTTT->szText; // Sanity Check
pTTT->hinst = AfxGetResourceHandle(); // Don't think this is needed at all
return TRUE;
}
}
return FALSE;
}
It seems for some of my controls the tool tip will not show up. For most of the check box controls the tool tip displays, but for a few they just do not show. There are no other controls covering them up, they are not disabled.
Another thing, if I use the non-standard cursor windows repeatedly flashes the tool tip, so much so it is unreadable in some cases. How can I fix this? This is not a problem on CEdit controls, so why is it a problem elsewhere?
EDIT: Update, the controls that have been on the pages for years seem to show tool tips. Any control that I try to add now/today will not show tool tips at all. No matter the position, control type, settings, I cannot get a single tool tip to show on a newly inserted control.
If you do not want to use helper class I have proposed then fix the problems in your code.
First, use ON_NOTIFY_EX_RANGE macro when mapping the even handler, like this (this will cover all IDs):
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)
Next, you need to fix your function. I see a few problems here. First, when testing for TTF_IDISHWND flag you only need to re-initalise the nID. You do not need to apply this to the whole function. Second, after all manipulations, your nID will be the actual dialog ID. There is no need to GetDlgItem() function
BOOL CCfgPrefPage::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
UINT nID = pNMHDR->idFrom;
if (pTTT->uFlags & TTF_IDISHWND)
{
nID = ::GetDlgCtrlID((HWND)nID);
}
if(nID)
{
if( nID == IDC_PICKDIST_EDIT)
_tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);
else if( nID == IDC_ENDPTTOL_EDIT)
_tcsncpy_s(pTTT->szText, _T("Tool Tip Text"), _TRUNCATE);
//pTTT->lpszText = pTTT->szText; // Sanity Check
*pResult = 0;
return TRUE;
}
return FALSE;
}
Working with a toolbar which repeats some menu items from menus of an older MFC application, I have worked on this issue of tool tips as well as (1) modifying the toolbar bit map to include additional icons and (2) providing user feedback on current application state. My problem is that I have to do most of this by hand rather than using the various wizards and tools.
What I have done is to (1) add new members to the CView derived class to handle additional messages, (2) modified the toolbar bit map to add in the additional icons using both MS Paint and the Resource Editor, and (3) added new event ids and event handlers to the message map for the CView derived class.
One problem I ran into with the toolbar bitmap change was that since I was inserting an icon, I had to shift an existing icon in the bitmap to the right. My first attempt at this resulted in the shifted icon showing as a blank on the application toolbar. Then I realized that I needed to add a bit more to the length of the toolbar bitmap. After adding a couple more columns to the last icon in the toolbar bitmap to make it a standard width in pixels, the icon displayed properly.
For tooltips I added the following to the message map:
ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipText)
I then added the following method to my class to handle the notifications for my menu items. As a side note, it appears that OnToolTipText() is the standard method used in CFrameWnd class and CMDIChildWnd class however CView derives from CWnd as does CFrameWnd so I doubt it makes a difference as to what the method is named.
inline BOOL CPCSampleView::OnToolTipText( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
static wchar_t toolTextToggleExportSylk [64] = L"Toggle SYLK export.";
static wchar_t toolTextClearWindow [64] = L"Clear the log displayed.";
static wchar_t toolTextConnectLan [64] = L"Log on the POS terminal through the LAN.";
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
switch (pNMHDR->idFrom) {
case ID_TOGGLE_SYLK_EXPORT:
pTTT->lpszText = toolTextToggleExportSylk;
return TRUE;
case ID_WINDOW_CLEAR:
pTTT->lpszText = toolTextClearWindow;
return TRUE;
case ID_CONNECT_LAN_ON:
pTTT->lpszText = toolTextConnectLan;
return TRUE;
}
// if we do not handle the message then return FALSE to let someone else do it.
return FALSE;
}
For the user feedback on a menu item which toggles a file export when doing reports I provided the following changes to the message map and then implemented the necessary methods. There are two types of messages involved so I had to add two methods and two new message map entries:
// New message map entries to handle the menu item selection event
// and to update the menu item and the toolbar icon with state changes
ON_COMMAND(ID_TOGGLE_SYLK_EXPORT, OnToggleExportSylk)
ON_UPDATE_COMMAND_UI(ID_TOGGLE_SYLK_EXPORT, OnUpdateToggleExportSylk)
// New methods added to the CView derived class
// handle the menu selection event generated by either selecting the menu item
// from the menu or by clicking on the icon in the toolbar.
void CPCSampleView::OnToggleExportSylk()
{
// Exclusive Or to toggle the indicator bit from 0 to 1 and 1 to 0.
GetDocument()->ulReportOptionsMap ^= CPCSampleDoc::ulReportOptionsExportSylk;
}
// handle the request from the MFC framework to update the displayed state this
// not only does a check mark against the menu item it also causes the toolbar
// icon to appear depressed if click is set or non-depressed if click is not set
inline void CPCSampleView::OnUpdateToggleExportSylk (CCmdUI* pCmdUI)
{
if (GetDocument()->ulReportOptionsMap & CPCSampleDoc::ulReportOptionsExportSylk)
{
// SYLK export is turned on so indicate status to the user. This will
// put a check mark beside the menu item and show the toolbar button depressed
pCmdUI->SetCheck (1);
}
else
{
// SYLK export is turned off so indicate status to the user. This will
// remove the check mark beside the menu item and show the toolbar button as raised.
pCmdUI->SetCheck (0);
}
}
The resource file changes were needed to provide a new button for the toggle action as well as to add a new menu item for the toggle action. I am using the same resource id for several different things since these are all separate. So the id for the resource string is the same as for the menu item and is same for the toolbar button so as to simplify my life and make it easy to find all the particular bits and pieces.
The toolbar resource file definition looks like:
IDR_MAINFRAME TOOLBAR 16, 15
BEGIN
BUTTON ID_CONNECT_LAN_ON
SEPARATOR
BUTTON ID_WINDOW_CLEAR
SEPARATOR
BUTTON ID_TOGGLE_SYLK_EXPORT
SEPARATOR
BUTTON ID_APP_ABOUT
END
And the specific part of the menu, which uses the same resource id for the toggle event id looks like:
MENUITEM "Export to SYLK file", ID_TOGGLE_SYLK_EXPORT
Then to provide the status bar text which shows up with a mouse over there is a string table addition:
ID_TOGGLE_SYLK_EXPORT "Toggle export of SYLK format report files for spreadsheets."
The lpszText member of the struct is describe in the MSDN documentation for the TOOLINFO struct as:
Pointer to the buffer that contains the text for the tool, or
identifier of the string resource that contains the text. This member
is sometimes used to return values. If you need to examine the
returned value, must point to a valid buffer of sufficient size.
Otherwise, it can be set to NULL. If lpszText is set to
LPSTR_TEXTCALLBACK, the control sends the TTN_GETDISPINFO notification
code to the owner window to retrieve the text.
Reviewing the existing answer to this question, I wondered about the if statement check for the TTF_IDISHWND flag. The MSDN documentation for the TOOLINFO struct has this to say:
Indicates that the uId member is the window handle to the tool. If
this flag is not set, uId is the tool's identifier.

Custom control drawing - focus state

I've created two instances of Fred Acker's CHoverButtonEx class with a slight modification to include a disabled state.
These buttons exist on modeless dialog which contains the following properties:
IDD_MY_DIALOG DIALOGEX 0, 0, 162, 27
STYLE DS_SETFONT | WS_POPUP
EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
FONT 9, "Arial", 400, 0, 0x0
BEGIN
CONTROL 146,IDC_STATIC_BKGND,"Static",SS_BITMAP,0,0,162,27
LTEXT "",IDC_STATIC_1,6,4,101,9,SS_WORDELLIPSIS
LTEXT "",IDC_STATIC_2,6,15,101,9
CONTROL "",IDC_BUTTON_1,"Button",BS_OWNERDRAW | WS_TABSTOP,108,4,24,19
CONTROL "",IDC_BUTTON_2,"Button",BS_OWNERDRAW | WS_TABSTOP,134,4,24,19
END
Everything is working well with the buttons except that now I need to implement a focus state but the behavior is strange and unexpected.
In my DrawItem message handler, I have the following code which functions pretty much exactly the same as the original minus some stuff I cleaned out which wasn't needed:
void CHoverButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// Do other stuff above and now find the state and draw the bitmap
if(lpDrawItemStruct->itemState & ODS_SELECTED)
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,m_ButtonSize.cx,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,m_ButtonSize.cx,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
else
{
if(m_bHover)
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,m_ButtonSize.cx*2,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,m_ButtonSize.cx*2,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
else
{
if (IsWindowEnabled())
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,0,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,0,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
else
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,m_ButtonSize.cx*3,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,m_ButtonSize.cx*3,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
}
}
if (lpDrawItemStruct->itemAction & ODA_FOCUS)
{
RECT rcFocus;
int iChange = 3;
rcFocus.top = lpDrawItemStruct->rcItem.top + iChange;
rcFocus.left = lpDrawItemStruct->rcItem.left + iChange;
rcFocus.right = lpDrawItemStruct->rcItem.right - iChange;
rcFocus.bottom = lpDrawItemStruct->rcItem.bottom - iChange;
pMemDC->DrawFocusRect(&rcFocus);
}
// clean up
pMemDC -> SelectObject(pOldBitmap);
delete pMemDC;
}
What is occurring is that when the dialog is the active window and I press tab once, the focus box jumps to the second button, even though I can confirm through the button's click handler that the first button has the real focus. Then when I press tab again the focus box jumps to include both buttons. Then another tab press moves the focus box to the other button and finally another tab press removes the focus box entirely. This sequence keeps occurring. Even holding Shift-Tab won't affect it.
I sniffed out the windows messages using spy++ and it looks pretty normal. I get a WM_DRAWITEM message for both button controls and they are successfully handled.
I'll mention one last thing; in my dialog code, when I'm initializing the buttons, I'm forced to place the buttons in the bottom of the z-order or else the IDC_STATIC_BKGND would draw over the buttons. This didn't seem normal to me because they should already be at the bottom of the z-order. (Just adding it in case it's a part of the cause to my problem).
m_button1.SetHorizontal(true);
m_button1.SetMoveable(FALSE);
m_button1.LoadBitmap(IDB_BUTTON_1);
m_button1.SetToolTipText(_T("Some Text1"));
// Draws the button after the background is drawn
m_button1.SetWindowPos(&CWnd::wndBottom, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
m_button2.SetHorizontal(true);
m_button2.SetMoveable(FALSE);
m_button2.LoadBitmap(IDB_BUTTON_2);
m_button2.SetToolTipText(_T("Some Text2"));
// Draws the button after the background is drawn
m_button2.SetWindowPos(&CWnd::wndBottom, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
Does anyone know how to correctly add a focus box for my situation?
Thanks.
Update:
After trying BrendanMcK's suggestion and not resolving the issue, I dug some more in the captured messages in spy++ and noticed something that seems like odd behavior. The following messages represent a single tab press in the dialog.
<00283> 00870794 R WM_CTLCOLORBTN hBrush:01900015
<00284> 00870794 S WM_DRAWITEM idCtl:112 lpdis:002AEF2C
<00285> 00870794 R WM_DRAWITEM fProcessed:True
<00286> 00870794 S DM_GETDEFID
<00287> 00870794 R DM_GETDEFID wHasDef:DC_HASDEFID wDefID:0001
<00288> 00870794 S WM_CTLCOLORBTN hdcButton:110114A7 hwndButton:01090502
<00289> 00870794 R WM_CTLCOLORBTN hBrush:01900015
<00290> 00870794 S WM_DRAWITEM idCtl:113 lpdis:002AF2A0
<00291> 00870794 R WM_DRAWITEM fProcessed:True
As evident there are two separate WM_DRAWITEM messages delivered. The details for each message is:
Message #284: action= ODA_FOCUS, state= 0x0110 (ODS_FOCUS = 0x0010)
Message #290: action= ODA_DRAWENTIRE, state= ODS_NOACCEL
I would have expected that in message #290, the action would again be ODA_FOCUS to allow the other button to "un-draw" the focus box.
I'm not sure why I'm even receiving an ODS_NOACCEL state as I'm using Win7. Is there something I've forgot to disable?
From MSDN on DRAWITEMSTATE:
ODA_FOCUS
The control has lost or gained the keyboard focus. The itemState member should be checked to determine whether the control has the focus.
Because you're redrawing the control, you should only be drawing the focus rect if the itemState indicates that the control has focus. Instead, you're drawing it in all cases, regardles of whether the control gains or loses focus. Add a check to see if itemState & ODS_FOCUS and you should be good.