Ok, I did a quick search on msdn an figured out,
CDialog
class inherits from
CWnd
which has a virtual base method
Cwnd::GetDlgItem
reference:
https://docs.microsoft.com/en-us/cpp/mfc/reference/cwnd-class?view=vs-2019#getdlgitem
this is all you need, you should take a look at memory template to find out control ID which you pass into the
Cwnd::GetDlgItem
then here is pseudo code:
1 2 3
|
CDialog dlg("my_dialog_template", nullptr); // Constructs a CDialog object from template.
CWnd* btn = dlg.GetDlgItem(IDOK); // get pointer to ie. OK button
HWND hButton = btn->GetSafeHwnd(); // get handle to button
|
this is how to do it, but I guess the dialog you are talking about isn't created by you?
well in that case you don't know control ID (and you should not know it) and what that mean is you need to obtain this info somehow because template most likely isn't open source, here are few suggestions how to do it:
1 2 3 4
|
CDialog::GetDefID
CDialog::NextDlgCtrl
CDialog::GotoDlgCtrl
CWnd::GetFocus
|
for description of these see:
https://docs.microsoft.com/en-us/cpp/mfc/reference/cdialog-class?view=vs-2019
OR
if you need a handle you would call below method, and the use C API to "hack the MFC dialog":
CWnd::GetSafeHwnd
see info here:
https://docs.microsoft.com/en-us/cpp/mfc/reference/cwnd-class?view=vs-2019#getsafehwnd
Below is the code that I have tried |
No no, first the code isn't MFC, if you are using MFC then please avoid C style API, and use MFC functionality in a manner similar to my sample, and secondly,
WindowFromPoint
doesn't look like maintanable solution for MFC, and it isn't FMC code or style either.
Instead make use of
CWnd::GetFocus
on the dialog pointer/varaible to get focus to button, that should be better and it's MFC code too.
In your code, where is the dialog pointer you were talking about? or object variable? use that to get the handle.
for example:
HWND hDialog = dlg_pointer->GetSafeHwnd();
Does it has any simple C++ code that I can try and test run in console before I apply it into MFC? |
I'm afraid not without googling out, but all information you need is really on msdn, you just need to take a read.
as I said I don't know MFC I just try to help you with this info here is from msdn, it's not that hard to understand. since MFC is just winapi and C++. known concepts. the rest belongs to reading the reference.