// JP opened flex table

Click to See Complete Forum and Search --> : recreate child window fails


sublime99
March 6th, 2008, 12:32 PM
I modified an application to be a component in my main application. The user can create and destroy a window.

After the first time the window is destroyed I am unable to recreate the window. I am unable to register the class in windows. Is there something else which I must nullify?




//=============================================================================
int treeRun()
{

HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);

ustring classname=_T("SIMPLEWND");
WNDCLASSEX wcx={0}; //used for storing information about the wnd 'class'

wcx.cbSize = sizeof(WNDCLASSEX);
wcx.lpfnWndProc = rlWndProc; //wnd Procedure pointer
wcx.hInstance = hInstance; //app instance
//use 'LoadImage' to load wnd class icon and cursor as it supersedes the
//obsolete functions 'LoadIcon' and 'LoadCursor', although these functions will
//still work. Because the icon and cursor are loaded from system resources ie
//they are shared, it is not necessary to free the image resources with either
//'DestroyIcon' or 'DestroyCursor'.
wcx.hIcon = reinterpret_cast<HICON>(LoadImage(0,IDI_APPLICATION,
IMAGE_ICON,0,0,LR_SHARED));
wcx.hCursor = reinterpret_cast<HCURSOR>(LoadImage(0,IDC_ARROW,
IMAGE_CURSOR,0,0,LR_SHARED));
wcx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);
wcx.lpszClassName = classname.c_str();
//the window 'class' (not c++ class) has to be registered with the system
//before windows of that 'class' can be created
if (!RegisterClassEx(&wcx))
{
rlErrMsg(_T("Failed to register wnd class")); // fails here on recreate
return -1;
}

int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
int desktopheight=GetSystemMetrics(SM_CYSCREEN);

HWND hwnd=CreateWindowEx(WS_EX_ACCEPTFILES , //extended styles
classname.c_str(), //name: wnd 'class'
_T("Tray Agent"), //wnd title
WS_OVERLAPPEDWINDOW, //wnd style
desktopwidth/4, //position:left
desktopheight/4, //position: top
desktopwidth/2, //width
desktopheight/2, //height
0, //parent wnd handle
0, //menu handle/wnd id
hInstance, //app instance
0); //user defined info

hTreeWin=hwnd;
if (!hwnd)
{
rlErrMsg(_T("Failed to create wnd")); // fails here in recreate
return -1;
}

int nCmd=1;
ShowWindow(hwnd,nCmd);
UpdateWindow(hwnd);
refreshData(hwnd);

return 0;
}




//=============================================================================
void OnDestroy(const HWND hwnd)
{
//get the treeview control handle which has been previously stored in the user
//data associated with the parent window.
HWND hTreeview=reinterpret_cast<HWND>(static_cast<LONG_PTR>
(GetWindowLongPtr(hwnd,GWLP_USERDATA)));

//and free resources used by the treeview's image list
HIMAGELIST hImages=reinterpret_cast<HIMAGELIST>(SendMessage(hTreeview,
TVM_GETIMAGELIST,0,0));

ImageList_Destroy(hImages);

// Set the global handle to the window to null
hTreeWin=NULL;


}

Martin O
March 6th, 2008, 01:59 PM
Both RegisterClassEx and CreateWindowEx return a certain value when they fail & you can use GetLastError to get detailed info about why they failed. I didn't see any calls to GetLastError in your code.

heteroy
March 12th, 2008, 05:02 PM
A window class only needs to be registered once.

//JP added flex table