|
I am assuming you mean getting who has the focus in a different process, so try this[^] for size.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
I have a template function to check the type passed to it. I am templatizing it on a derived type but when I break in the function, the template parameter is the base class. A sample app is below. Can someone please explain where I am going wrong?
Thanks
class Base
{
public:
virtual ~Base() {}
};
class Derived1 : public Base {};
class Derived2 : public Base {};
template<class T> bool CheckType(boost::shared_ptr<Base> pBase)
{
T* pTemplate = NULL;
Base* pBaseRaw = pBase.get();
if(typeid(pBaseRaw) == typeid(pTemplate))
{
return true;
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
boost::shared_ptr<Base> pBase1(new Derived1);
boost::shared_ptr<Base> pBase2(new Derived2);
bool bIsDerived = CheckType<Derived1>(pBase1);
bIsDerived = CheckType<Derived2>(pBase2);
return 0;
}
|
|
|
|
|
A few questions:
- If I understand you correctly, CheckType is returning true, but you expected false?
- What compiler (& version) are you using?
I've just tried your code with g++ 4.0.1 and it gives the results I'd expect. I added some code to print out type names:
template<class T> bool CheckType(boost::shared_ptr<Base> pBase)
{
std::cout << typeid(T).name() << std::endl;
T* pTemplate = NULL;
Base* pBaseRaw = pBase.get();
std::cout << typeid(pBaseRaw).name() << std::endl;
if(typeid(pBaseRaw) == typeid(pTemplate))
{
return true;
}
return false;
} . I also printed out the result of CheckType and got these results:
8Derived1
P4Base
0
8Derived2
P4Base
0
So - the template parameter is Derived, the parameter is a pointer to Base, CheckType is returning false. I'm suspecting that the compiler you're using isn't fully standard compliant if it does something different?
|
|
|
|
|
Thanks for the response. Your output is what I expected. I was looking at the pointer types in the debugger, not printing out the type info to the console. The print statements give me what you got. I am using VS2008 v9 SP1. I was performing the wrong check to get a type match. I changed the function to the following:
template<class T> bool CheckType(boost::shared_ptr<Base> pBase)
{
Base* pBaseRaw = pBase.get();
if(pBaseRaw == null)
{
return false;
}
if(typeid(*pBaseRaw) == typeid(T))
{
return true;
}
return false;
}
I am now dereferencing the base class pointer returned from shared_ptr which casts it to the right type. I believe this is a sketchy implicit cast to the derived type. Should I just dynamic_cast or does the typeid comparison take care of type issues?
Thanks
|
|
|
|
|
IIRC, typeid uses the same mechanism as dynamic_cast, so typeid on its own is fine.
|
|
|
|
|
Member 2603772 wrote: T* pTemplate = NULL; //Why is pTemplate Base* and not Derived1* when passing a Derived1 to template parameter?
nope, you confused with the result, check by printing std::cout << typeid(pTemplate).name()
I think you expected the result as true and you got false (bIsDerived = true you expected, as the pointer you passed is derived).
to get what inside the pointer you can dereference it say typeid(*pBaseRaw)
so your code can be,
template<class T> bool CheckType(boost::shared_ptr<Base> pBase)
{
Base* pBaseRaw = pBase.get();
<big>if(typeid(*pBaseRaw) == typeid(T))</big> {
return true;
}
return false;
}
modified on Friday, January 23, 2009 1:13 PM
|
|
|
|
|
Thanks. I just posted a similar result based on the previous response. I appreciate the help.
|
|
|
|
|
I've started moving an MFC application to VS2008 MFC Feature Pack and got some problem with MainFrame menu manipulation.
Now the menu class is CMFCMenuBar is derived from CMFCToolbar and not rely anymore on CMenu. Seems I need to rewrite whole menu manipulation chunk the new way.
I didn't found any example of dynamic menu manipulation in Internet(like former: RemoveMenu, GetSubMenu, InsertMenu, GetSubMenu, AppendMenu, ModifyMenu, etc.)
- Is there any web resources/solutions on CMFCMenuBar dynamic menu manipulations?
Appreciate any suggestions.
Thanks.
|
|
|
|
|
use CMenu* pMenu = CMenu::FromHandle(mBar->GetHMenu());
where mBar is your CMFCMenuBar, now you can apply all the CMenu function on pMenu .
Never complain,never explain,just do your work.
|
|
|
|
|
Thanks, it works on pop-up menus, but don't work for top level menu.
|
|
|
|
|
The problem is a "glitch" (that's how I refer to it) in BCG's design of these extensions. The correct code is below (an example for removing an item), but be sure to look below the code for the important part. NOTE: "n" = your sub menu and "ID" = the ID of the menu item you want to remove.
CMenu* pMenu = CMenu::FromHandle( m_wndMenuBar.GetHMenu() );
CMenu* pSubMenu = pMenu ? pMenu->GetSubMenu( 0 ) : NULL;
if( pSubMenu ) pSubMenu->RemoveMenu( ID, MF_BYCOMMAND );
IMPORTANT (see below, too): You must FIRST go to the registry and DELETE the entry for you product. HKCU/Software/Your Product. You can just delete the correct folder for the menu, but that's up to you to figure out which one it is.
PROBLEM: This will solve it for you and new users of your product. However, existing users will never see the changes. There is a function CWinAppEx::CleanState(), which is supposed to remove memory from the registry. But, you will then have an app with no memory of toolbars, menus, etc...meaning customization is pointless.
Good luck!
|
|
|
|
|
Hi all.
In my SDI application, the window frame shrinks everytime i click FILE>NEW.
My application starts maximized(SW_MAXIMIZE instead of SW_SHOW in InitInstance() of the App Class)
Anyway, as soon a click file>new, the window shrinks a bit and still shows as maximized.
My application has a splitter window, i dont know if that got to do anything this.
I am kinda new to C++ and MFC. appreciate your help.
Regards
|
|
|
|
|
theAce89 wrote:
Anyway, as soon a click file>new, the window shrinks a bit and still shows as maximized.
My application has a splitter window, i dont know if that got to do anything this.
That is strange... it could be a number of things. The best thing to do is use the debugger to locate the responsible class. Here is how I would go about it:
1.) Locate your IDE path: (example) C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc
2.) Open the core MFC file 'winocc.cpp' in your IDE.
3.) Run a Debug build of your project.
4.) Set a breakpoint on CWnd::MoveWindow and click File->New.
You may also want to check if CWnd::OnNcCalcSize or CWnd::SetWindowPos is the culprit, there are a few ways to move a CWnd derived window.
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks David.
The culprit was actually my fault... i used the following code in the OnInitInstance() of the view classes:
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
regards.
|
|
|
|
|
Hey Friends
I am looking to get a list of applications running, or to be more specific i need main window handle only as task manager displays.
Tried EnumWinndows, but it gives many many windows, which i do not want
Many Window(s) have parent Window as 0
Looks like EnumWindows is correct way
Need Help.
Regards
|
|
|
|
|
In addition to that you should check the window styles. That is you can avoid the invisible windows, child windows, etc.
- ns ami -
|
|
|
|
|
Check this[^]
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
Hi experts,
How I can change length of dialog box at run time?
|
|
|
|
|
CWnd::SetWindowPos[^] to the rescue.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
|
|
Hello,
i've a problem with a subclassed CEdit on WindowsCE 5.0.
If i use SetPasswordChar and SetFont in the same subclassed CEdit it generates an acces violation
[Stack fault]: Thread=9a431400 Proc=98328d20 'xxxxxx.exe'
AKY=00004001 PC=0017ba6c(xxxxxx.exe+0x0016ba6c) RA=000318e8(xxxxxx.exe+0x000218e8) BVA=1e2a1ee4 FSR=00000007
my Code:
...
if(edit->SubclassDlgItem(IDC_EDIT1, this)){
edit->SetFont(m_Font, FALSE);
edit->SetPasswordChar(0x25CF); // --> stack fault
}
...
If i don't use SetFont OR SetPasswordChar it works fine...
whats wrong ???
can anybody help me?
Thanx for replys...
edit:
// Initialization
Controls.edit2 = new CMyEdit();
Controls.edit2->__SubClassControl(this, IDC_EDIT2);
Controls.edit2->__SetMaxTextLength(9);
//Controls.edit2->SetPasswordChar('*'); // --> stack fault
Controls.edit2->SetPasswordChar(0x25CF); // --> stack fault
// Control-Code
BOOL CMyEdit::__SubClassControl(CMyDialog *pParent, int nID)
{
// subclassing per SubclassDlgItem is okay, SubclassWindow does not work
m_pParent = pParent;
m_nID = nID;
BOOL ret = SubclassDlgItem(m_nID, m_pParent);
// changing FontSize to parentfont and the heigth of the Control
LOGFONT lf;
if(m_pParent->GetFont()->GetLogFont(&lf) != 0){
// m_Font is a derived class from CFont and an Item of my CMyEdit for better Fonthandling
// if i use CFont the same Problem exist with SetPasswordChar (isn't the reason)
m_Font->_SetLogFont(lf);
if(m_bFontResizing == TRUE){
CRect rect;
GetClientRect(&rect);
rect.DeflateRect(2,2,2,2);
m_Font->_SetHeight(rect.Height());
}
SetFont((CFont*)m_Font, FALSE);
}
return ret;
}
void CMyEdit::__SetMaxTextLength(int maxLen)
{
m_maxLen = maxLen;
// del mask
m_Mask = _T("");
}
modified on Friday, January 23, 2009 7:14 AM
|
|
|
|
|
No real idea here but what happens if you change the order, use SetPasswordChar first and then use SetFont? Any change?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
i try it, but it has no effect...
the message EM_SETPASSWORDCHAR raises the error, mfc is not the reason...
|
|
|
|
|
So let me get this clear:
edit.SetFont(somefont);
edit.SetPasswordChar(whatever); <- it crashes here
crashes and
edit.SetPasswordChar(whatever); <- it crashes here again, so SetFont never gets called
edit.SetFont(somefont);
crashes too but
edit.SetPasswordChar(whatever); <- works ok, no SetFont around
works. That is very strange, i don't really see the great difference between the later two cases since SetFont never gets called at all in either case.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|