Re: Swap handles of attached windows ... assert in wincore.cpp .... please explain

Tech-Archive recommends: Fix windows errors by optimizing your registry



On 7 Okt., 02:30, Joseph M. Newcomer <newco...@xxxxxxxxxxxx> wrote:
You are doing this the hard way. Again, I have no idea what you're trying to do, but
swapping handles as you have done is not going to work. Doing complex attach operations
like this is going to result in problems, because now you have potential multiple
mappings.



On Fri, 05 Oct 2007 11:51:17 -0700, ".rhavin grobert" <cl...@xxxxxxxx> wrote:
I'd realy like it if someone could please answer something;)

If i get rid of all the...
AFX_MANAGE_STATE(AfxGetStaticModuleState());
...i can create the new control and use it, but i get an assert when i
try to destroy the old one after swapping hWnds.

If i keep them, i can correctly (?) create the new window, destroy the
old one, attach the new hwnd to the old controls CWnd but then all
SendMessages to my control seem to vanish into void. Perhaps someone
finds something i didnt find in the whole lot of code that comes here?

_______________________________________________________

// class CQListBox is base public CListbox, public CQObject....
// QGetCWnd(), QCreateNew(), QDelete() and QGetAllocation()
// are CQObject's virtual functions overloaded by CQListBox.

bool CQObject::QRecreateWindow(DWORD dwStyle, DWORD dwStyleEx) {
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CWnd* pWnd = QGetCWnd();
if (pWnd == NULL) return false;
if (pWnd->GetSafeHwnd() == 0) return false;

CWnd* pParent = pWnd->GetParent();
HWND hParent = 0;
UINT nID = pWnd->GetDlgCtrlID();
CFont* pFont = pWnd->GetFont();
CWnd* pWndAfter = pWnd->GetNextWindow(GW_HWNDPREV);
LPVOID lParam = (LPVOID) GetWindowLong(pWnd->GetSafeHwnd(),
GWL_USERDATA);
LONG lWndProc = GetWindowLong(pWnd->GetSafeHwnd(), GWL_WNDPROC);

if (pWndAfter == NULL ) pWndAfter = (CWnd*) &CWnd::wndBottom;
if (pWnd->GetParent()) hParent = pWnd->GetParent()->GetSafeHwnd();

****
Code this complex is dangerous. You are playing with fire here. What are you really
trying to do here?

CQObject is the baseclass of CQListBox, CQComboBox, CQYouNameIt, ...
those classes all have two baseclasses: their window-control-class
(eg. CListBox, CCombobox, ...) and CQObject.
I get a pointer to the CWnd-baseclass [QGetCWnd()] and get the
settings for the following creation-command to
create a similar window.

****> CRect rc;
pWnd->GetWindowRect(&rc);
rc.bottom += 150; // this is just to see both windows
rc.top += 150;
pParent->ScreenToClient(&rc);

CString scCaption;
pWnd->GetWindowText(scCaption);

char szClass[32];
GetClassName(pWnd->GetSafeHwnd(), szClass, 32);

****
Why do you think 32 is going to make sense?

'twas enough for testing. later i wanted to see if i find some
MAX_CLASSNAME_LENGHT anywher.

****

CQObject* pObjNew = QCreateNew(dwStyleEx, szClass, scCaption,
dwStyle, rc, pParent, nID, lParam);
CWnd* pNew = pObjNew->QGetCWnd();

pWnd->DestroyWindow();
HWND hWnd = pNew->Detach();
pWnd->Attach(hWnd);
pWnd->SetWindowPos(pWndAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
pWnd->SetFont(pFont);

****
I can't begin to understand the purpose of the above code. You are asking detailed
questions of how to do something without explaining your purpose in doing it.

QCreateNew() creates a new window-control of the derived class. After
that, i get its CWnd
[QGetCWnd()] and modify it's settings according to the original
control.

Now i have two similar controls, one with the style-flags originally
set by the resources, one
with the flags i want. I thought i can now somehow swap the windows of
the two CWnd and
destroy the newly created object, that way destroying the old window.
I didnt know that a
CWnd needs more than a hwnd to point to a window. Seems like it really
doesn't _need_ more,
the rest seems to be moe like some error-management to me, because the
actual communication
is still handled via hwnd. please correct me if im wrong.


****



QDelete(pObjNew->QGetAllocation());
return true;
}

// ________________________________

CQObject* CQListBox::QCreateNew(
DWORD dwExStyle, LPCTSTR szClassName,
LPCTSTR szWindowName, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd,
UINT nID, LPVOID lpParam)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CQListBox* pObj = new CQListBox;
pObj->CreateEx(dwExStyle, szClassName, szWindowName,
dwStyle, rect, pParentWnd, nID, lpParam);
return pObj;
}

// ________________________________

void CQListBox::QDelete(void* pAlloc) {
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CQListBox* p_C = (CQListBox*) pAlloc;
p_C->DestroyWindow();
delete p_C;
}

// ________________________________

CWnd* CQListBox::QGetCWnd() {
return this;
}

// ________________________________

void* CQListBox::QGetAllocation() {
return this;
}

Joseph M. Newcomer [MVP]
email: newco...@xxxxxxxxxxxx
Web:http://www.flounder.com
MVP Tips:http://www.flounder.com/mvp_tips.htm


.



Relevant Pages

  • Re: Simulating CTR+INS keystroke problem
    ... I am afraid GetFocus() is not for me. ... It does work if the control belongs to my app but I am interested in the ... I want to copy to clipboard the highlighted text in any window when user ... GetForegroundWindowto get the hWnd, but this gives the main window. ...
    (microsoft.public.vb.general.discussion)
  • Re: How much oop is too much oop?
    ... It sounds like your behavior class can hold a reference to a handle object ... that has a NULL HWND, or it doesn't hold a reference at all. ... >> MFC arranges for CWnd to have a NULL HWND. ... The handle class doesn't care while the window class will ...
    (microsoft.public.vc.language)
  • Re: Given Window hWnd and Control ID, how to find Controls Parent hWnd?
    ... Sign the petition to Microsoft. ... Given the hWnd of an application Window and the control ID of a control ...
    (microsoft.public.vb.winapi)
  • Given Window hWnd and Control ID, how to find Controls Parent hWnd?
    ... Given the hWnd of an application Window and the control ID of a control ... with all controls placed directly onto the main parent window. ...
    (microsoft.public.vb.winapi)
  • Re: MFC and Worker Thread issue
    ... THERE IS NO HWND ASSOCIATED WITH THE CWnd! ... However, note that code does not show any use of HWND, in the original ... the absence of the window is a neutral condition ... have a facility to inform UI about the progress (and termination). ...
    (microsoft.public.vc.mfc)