Discussion:
Child Window and SetFocus problem.
(too old to reply)
Pankaj
2004-10-26 19:39:01 UTC
Permalink
Hi,
I have a Dialog based GUI application. I later on create some child
windows(CWnd based) on the surface of my dialog (which look like 3d circles)
and allow the user to drag them around. My problem is that when a user click
on one of the circles, I do an explicit SetFocus() for that window and then
handle all its input etc....
But then if i now leave this window in a selected state and now Alt tab to
some other application, and try to come back to my app, it hangs and doesnt
paint itself.

The same problem can happen if i use the Menu and choose an option which
results in another dialog box opening....and my child window jsut doesnt
reluinquish control.

I know its something do to with differnt message queues and that SetFocus on
child window is causing the problem but i dont know the solution...

Any help will be appreciated.

thanks
P
Doug Harrison [MVP]
2004-10-26 19:55:11 UTC
Permalink
Post by Pankaj
I have a Dialog based GUI application. I later on create some child
windows(CWnd based) on the surface of my dialog (which look like 3d circles)
and allow the user to drag them around. My problem is that when a user click
on one of the circles, I do an explicit SetFocus() for that window and then
handle all its input etc....
To implement a dragging operation, you need to use SetCapture on the
button-down event and ReleaseCapture on button-up and WM_CANCELMODE. When
you get the button-down message, you should set a flag m_dragging in the
dialog object; you could of course save other state such as which object is
being dragged. Then you can return from the button-down handler, and "handle
all its input" without doing anything special, such as running your own
message loop. Instead, you can rely on MFC's internal message loop.
Post by Pankaj
But then if i now leave this window in a selected state and now Alt tab to
some other application, and try to come back to my app, it hangs and doesnt
paint itself.
The same problem can happen if i use the Menu and choose an option which
results in another dialog box opening....and my child window jsut doesnt
reluinquish control.
I know its something do to with differnt message queues and that SetFocus on
child window is causing the problem but i dont know the solution...
I suspect the "hanging" problem lies in how you "handle all its input". It
sounds almost like you aren't processing messages during this time.
--
Doug Harrison
Microsoft MVP - Visual C++
AliR
2004-10-26 20:04:33 UTC
Permalink
It all depends on how you are doing all of this. There are many ways to
skin the cat.

Post some sample code or something!

AliR.
Post by Pankaj
Hi,
I have a Dialog based GUI application. I later on create some child
windows(CWnd based) on the surface of my dialog (which look like 3d circles)
and allow the user to drag them around. My problem is that when a user click
on one of the circles, I do an explicit SetFocus() for that window and then
handle all its input etc....
But then if i now leave this window in a selected state and now Alt tab to
some other application, and try to come back to my app, it hangs and doesnt
paint itself.
The same problem can happen if i use the Menu and choose an option which
results in another dialog box opening....and my child window jsut doesnt
reluinquish control.
I know its something do to with differnt message queues and that SetFocus on
child window is causing the problem but i dont know the solution...
Any help will be appreciated.
thanks
P
Pankaj
2004-10-26 20:21:03 UTC
Permalink
My requirement is to not only allow dragging of the child (3d circle) windows
but also i want them to respond to keyboard input. I want them to move in all
directions on arrow keys etc. I also want right click menus on them . And i
am doing all of this already....albeit in wrong manner.

Right now i create the window and then in LButtonUP(also in rbuttonUP) i do
a SetFocus .Then in OnSetFocus handler i set a flag of selcted and paint
myself like a selected window by calling invalidate() which checks for flag
and if selected paints myself in a selected color.

The problem is then if i deliberatley do not take the focus away, i get
stuck in this mode and hang if i go a alt+tb or any action that has to bring
a dialog box up.

-P
Post by Pankaj
Hi,
I have a Dialog based GUI application. I later on create some child
windows(CWnd based) on the surface of my dialog (which look like 3d circles)
and allow the user to drag them around. My problem is that when a user click
on one of the circles, I do an explicit SetFocus() for that window and then
handle all its input etc....
But then if i now leave this window in a selected state and now Alt tab to
some other application, and try to come back to my app, it hangs and doesnt
paint itself.
The same problem can happen if i use the Menu and choose an option which
results in another dialog box opening....and my child window jsut doesnt
reluinquish control.
I know its something do to with differnt message queues and that SetFocus on
child window is causing the problem but i dont know the solution...
Any help will be appreciated.
thanks
P
AliR
2004-10-26 20:33:50 UTC
Permalink
Try using this class as your child window and see if there is any
improvment. It doesn't do the arrow keys or the menu but try this class to
see if things are a little better.


class CMyWnd : public CWnd
{
DECLARE_DYNAMIC(CMyWnd)

public:
CMyWnd();
virtual ~CMyWnd();

BOOL Create(CWnd *pParent,const RECT &rect,UINT ID);
protected:
DECLARE_MESSAGE_MAP()
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnPaint();

private:
CPoint m_StartPoint;
char m_Char;
};


// CMyWnd

IMPLEMENT_DYNAMIC(CMyWnd, CWnd)
CMyWnd::CMyWnd()
: m_Char('A')
{
}

CMyWnd::~CMyWnd()
{
}


BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
END_MESSAGE_MAP()



// CMyWnd message handlers

BOOL CMyWnd::Create(CWnd *pParent,const RECT &rect,UINT ID)
{
return
CWnd::Create(NULL,NULL,WS_CLIPSIBLINGS|WS_CHILD|WS_VISIBLE|WS_BORDER,rect,pP
arent,ID);
}

void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture();

m_StartPoint = point;
ClientToScreen(&m_StartPoint);

SetFocus();

CWnd::OnLButtonDown(nFlags, point);
}

void CMyWnd::OnMouseMove(UINT nFlags, CPoint point)
{
if (nFlags & MK_LBUTTON)
{
CPoint Pt = point;
ClientToScreen(&Pt);
CPoint Diff(Pt.x-m_StartPoint.x,Pt.y-m_StartPoint.y);
m_StartPoint = Pt;

CRect Rect;
GetWindowRect(&Rect);
Rect.OffsetRect(Diff);
GetParent()->ScreenToClient(&Rect);
SetWindowPos(NULL,Rect.left,Rect.top,0,0,SWP_SHOWWINDOW|SWP_NOSIZE);
}

CWnd::OnMouseMove(nFlags, point);

}

void CMyWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
ReleaseCapture();
CWnd::OnLButtonUp(nFlags, point);
}


void CMyWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting

CRect Rect(0,0,20,20);
CBrush Brush(RGB(255,255,255));

dc.FillRect(&Rect,&Brush);

dc.DrawText(CString(m_Char),CRect(0,0,20,20),DT_CENTER);
}

BOOL CMyWnd::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_CHAR)
{
if (isalpha(pMsg->wParam))
{
m_Char = pMsg->wParam;
Invalidate();
UpdateWindow();
return TRUE;
}
}
return CWnd::PreTranslateMessage(pMsg);
}




AliR.
Post by Pankaj
My requirement is to not only allow dragging of the child (3d circle) windows
but also i want them to respond to keyboard input. I want them to move in all
directions on arrow keys etc. I also want right click menus on them . And i
am doing all of this already....albeit in wrong manner.
Right now i create the window and then in LButtonUP(also in rbuttonUP) i do
a SetFocus .Then in OnSetFocus handler i set a flag of selcted and paint
myself like a selected window by calling invalidate() which checks for flag
and if selected paints myself in a selected color.
The problem is then if i deliberatley do not take the focus away, i get
stuck in this mode and hang if i go a alt+tb or any action that has to bring
a dialog box up.
-P
Post by Pankaj
Hi,
I have a Dialog based GUI application. I later on create some child
windows(CWnd based) on the surface of my dialog (which look like 3d circles)
and allow the user to drag them around. My problem is that when a user click
on one of the circles, I do an explicit SetFocus() for that window and then
handle all its input etc....
But then if i now leave this window in a selected state and now Alt tab to
some other application, and try to come back to my app, it hangs and doesnt
paint itself.
The same problem can happen if i use the Menu and choose an option which
results in another dialog box opening....and my child window jsut doesnt
reluinquish control.
I know its something do to with differnt message queues and that SetFocus on
child window is causing the problem but i dont know the solution...
Any help will be appreciated.
thanks
P
Loading...