Position of Button  
Author Message
Cassiopeiaxy





PostPosted: Visual C++ General, Position of Button Top

Hi,

I am currently working on 2 different codes which i m unable to solve. Both coding involve a dialog that is 300 by 500, a button near the top left of the dialog.

The first project is to find set the cursor to move to the top left corner of the button during the start of the program to enable faster clicking. IDC_LEFT is the name of the button.

BOOL CCursorClickerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
.
.
// TODO: Add extra initialization here
CWnd* ButtonLeft = GetDlgItem(IDC_LEFT);
ASSERT(ButtonLeft);
CRect Rect;
ButtonLeft->GetWindowRect(&Rect);
ButtonLeft->ClientToScreen(Rect);
int x = Rect.left;
int y = Rect.top;
SetCursorPos(Rect.left, Rect.top);
return TRUE; // return TRUE unless you set the focus to a control
}

I did the above codes but the cursor seem to move to the top left corner of desktop screen instead of the button.

The second project is to check whether the cursor lies in the button.

BOOL CCursorClickerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
.
.
// TODO: Add extra initialization here
CWnd* ButtonLeft = GetDlgItem(IDC_LEFT);
ASSERT(ButtonLeft);
CRect Rect;
ButtonLeft->GetWindowRect(&Rect);
int point = ASSERT(Rect.PtInRect(pt1));
if(point != 0)
{
//Do something
}
return TRUE; // return TRUE unless you set the focus to a control
}
I tried to place the cursor within the button but somehow it is not detected.
I have tried several ways but still unable to solve the above 2 problems.

Any help will be very much appreciated!

Thanks for the help



Visual C++15  
 
 
Simple Samples





PostPosted: Visual C++ General, Position of Button Top

Look at the documentation of GetWindowRect; I think you don't need to call ClientToScreen.

For determining where the cursor is at, look at the member functions of CWnd, especially the Window Access Functions. Perhaps you can use the function that determines which, if any, of the child windows that contains a specified point, but there are probably other possibilities.

I think however that checking the cursor position in OnInitDialog won't work; by the time you see the button, OnInitDialog will have returned long (in computer time) before.

Also see The scope of this forum; your question will get faster and better answers in a MFC newsgroup.