Strange Problem with SetWindowPos

Started by
3 comments, last by Metzler 21 years, 8 months ago
Hey, I am using SetWindowPos, to resize two child windows in an running Win32 MDI Application. The Problem is the strange behaviour, when i resize the mainwindow : I call the function SetWindowPos two times, but it just works one time, always for the same child window, no matter in which sequence i try to resize the windows. And the strangest thing is that both calls give me an Invalid Window Handle return (testet by GetLastError). So here is, what i do : case WM_SIZE: { RECT client; Palette_Window(client); SetWindowPos(hwndPalette,HWND_BOTTOM, client.left, client.top, client.right, client.bottom, SWP_NOZORDER); Map_Window(client); SetWindowPos(hwndMap,HWND_BOTTOM, client.left, client.top, client.right, client.bottom, SWP_NOZORDER); int fail = GetLastError(); char buf[50]; sprintf(buf, "Fehler : %d", fail); MessageBox(hwndFrame, buf, "Scheiss", MB_OK); }break; WM_SIZE is called in the Callback Function of the MainWindow ! Palette_Window and Map_Window just calculate the coordinates for these two childwindows. They (the functions) work perfectly (i use them at the beginning, when the application starts).
Advertisement
How do you resize multiple child windows at once ? The same way ?
#include <windows.h>#include <commdlg.h>#include <stdlib.h>#include <stdio.h>#include <ddraw.h>#include "structs.h"#include "dxstructs.h"#include "dxinit.h"#include <assert.h>#include "resource.h"#include "filedlg.h"#include "fileopsmap.h"#include "dxfunctions.h"#include "childwnd.h"LRESULT CALLBACK MapProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);LRESULT CALLBACK PaletteProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);int WINAPI WinMain (HINSTANCE hinst, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow){	WNDCLASSEX	winclass;	MSG			message;	hInstance = hinst;		HACCEL   hAccel ;		//Main Window Class				winclass.cbSize				= sizeof(WNDCLASSEX);	winclass.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);	winclass.style				= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	winclass.lpfnWndProc		= WindowProc;	winclass.cbClsExtra			= 0;	winclass.cbWndExtra			= 0;	winclass.hInstance			= hinst;	winclass.hIcon				= LoadIcon(NULL, IDI_APPLICATION);	winclass.hCursor			= LoadCursor(NULL, IDC_ARROW);	winclass.hbrBackground		= (HBRUSH) (COLOR_APPWORKSPACE + 1);	winclass.lpszMenuName		= MAKEINTRESOURCE(IDR_MENU1);	winclass.lpszClassName		= g_szAppClassName;	if (!RegisterClassEx(&winclass)) MessageBox(NULL, "Window Init Failure", "Failure", MB_OK);		winclass.style         = CS_HREDRAW | CS_VREDRAW;    winclass.lpfnWndProc   = PaletteProc;    winclass.cbClsExtra    = 0;    winclass.cbWndExtra    = 0;    winclass.hInstance     = hinst;    winclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);    winclass.hCursor       = LoadCursor(NULL, IDC_ARROW);    winclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);    winclass.lpszMenuName  = NULL;    winclass.lpszClassName = g_szChildClassName;	RegisterClassEx(&winclass);	winclass.style         = CS_HREDRAW | CS_VREDRAW;    winclass.lpfnWndProc   = MapProc;    winclass.cbClsExtra    = 0;    winclass.cbWndExtra    = 0;    winclass.hInstance     = hinst;    winclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);    winclass.hCursor       = LoadCursor(NULL, IDC_ARROW);    winclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);    winclass.lpszMenuName  = NULL;    winclass.lpszClassName = g_szMapClassName;	RegisterClassEx(&winclass);	hwndFrame = CreateWindowEx(WS_EX_APPWINDOW, g_szAppClassName, g_szAppClassName,                          WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE,                          CW_USEDEFAULT, CW_USEDEFAULT,                          CW_USEDEFAULT, CW_USEDEFAULT,                          NULL, NULL, hinst, NULL);			//ShowWindow( hwndFrame, SW_SHOW );		//UpdateWindow( hwndFrame);		hAccel = LoadAccelerators (hInstance, g_szAppClassName) ;	//hdc = GetDC(hwndFrame);	//CHILD VARIABLES 	RECT coord;	CLIENTCREATESTRUCT ccs;	    MDICREATESTRUCT	mdicreate ;	HWND hwndChild;		//MAP WINDOW CREATE	Map_Window(coord);	ccs.hWindowMenu		= NULL;			ccs.idFirstChild	= IDM_MAPCHILD;		hwndMap = CreateWindowEx(NULL,g_szMapClassName, 		NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, coord.left,coord.top,coord.right,coord.bottom, hwndFrame, NULL, hInstance, (PSTR)&ccs);		//MAP WINDOW SHOW	mdicreate.szClass = g_szMapClassName;	mdicreate.szTitle = g_szMapClassName;	mdicreate.hOwner  = hInstance ;	mdicreate.x       = coord.left;	mdicreate.y       = coord.top ;	mdicreate.cx      = coord.right ;	mdicreate.cy      = coord.bottom ;	mdicreate.style   = 0 ;	mdicreate.lParam  = 0 ;	hwndChild = (HWND)SendMessage (hwndMap, WM_MDICREATE, NULL, (LPARAM) (LPMDICREATESTRUCT) &mdicreate);	//PALETTE WINDOW CREATE	Palette_Window(coord);	ccs.hWindowMenu		= NULL;			ccs.idFirstChild	= IDM_PALETTECHILD;		hwndPalette = CreateWindowEx(NULL,g_szChildClassName, 		NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, coord.left,coord.top,coord.right,coord.bottom, hwndFrame, NULL, hInstance, (PSTR)&ccs);			//PALETTE WINDOW SHOW	mdicreate.szClass = g_szChildClassName;	mdicreate.szTitle = g_szChildClassName;	mdicreate.hOwner  = hInstance ;	mdicreate.x       = coord.left;	mdicreate.y       = coord.top ;	mdicreate.cx      = coord.right ;	mdicreate.cy      = coord.bottom ;	mdicreate.style   = 0 ;	mdicreate.lParam  = 0 ;	hwndChild = (HWND)SendMessage (hwndPalette, WM_MDICREATE, NULL, (LPARAM) (LPMDICREATESTRUCT) &mdicreate);//	DX_Init_Display(hwnd);//	Init_Map();//	load_map("karte.map");	while (GetMessage (&message, NULL, 0, 0))    {	/*	RECT rct;		AdjustClient(hwnd, rct);		  		int right = (rct.right-rct.left) / 40 + view_x;		int bottom = (rct.bottom-rct.top) / 40 + view_y;				render_map(view_x,view_y,right,bottom, rct);*/		/*char buffer[150];		long length = sprintf(buffer, "Mouse %d"); 		TextOut(hdc,0,0,buffer,length);*/		if (!TranslateMDISysAccel(hwndPalette, &message)			&& !TranslateAccelerator (hwndFrame, hAccel, &message))		{			TranslateMessage (&message) ;			DispatchMessage (&message) ;		}	}    return message.wParam ;}LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){	static TCHAR		 szFileName[MAX_PATH], szTitleName[MAX_PATH];	static HWND			 hwndPalette ;	HMENU				 hMenu; 	HWND				 hwndChild;	MDICREATESTRUCT		 mdicreate ;	switch(message)	{	case WM_CREATE:		{			/*RECT Scroll_Window;			AdjustClient(hwnd, Scroll_Window);			OffsetRect(&Scroll_Window, -1*Window_Cords.x, -1*Window_Cords.y);			scroll_x = Scroll_Window.right / 40;			scroll_y = Scroll_Window.bottom / 40;			scroll_x = feldx - scroll_x;			scroll_y = feldy - scroll_y;			SetScrollRange (hwnd, SB_VERT, 0, scroll_y, FALSE) ;			SetScrollRange (hwnd, SB_HORZ, 0, scroll_x, FALSE) ;*/		}break;	case WM_SIZE:        { 			/*RECT Scroll_Window;			AdjustClient(hwnd, Scroll_Window);			OffsetRect(&Scroll_Window, -1*Window_Cords.x, -1*Window_Cords.y);			scroll_x = Scroll_Window.right / 40;			scroll_y = Scroll_Window.bottom / 40;			scroll_x = feldx - scroll_x;			scroll_y = feldy - scroll_y;			SetScrollRange (hwnd, SB_VERT, 0, scroll_y, FALSE) ;			SetScrollRange (hwnd, SB_HORZ, 0, scroll_x, FALSE) ;*/			RECT client;			Palette_Window(client);			SetWindowPos(hwndPalette,NULL, client.left, client.top, client.right, client.bottom, NULL);						int fail = GetLastError();			char buf[50];			sprintf(buf, "Fehler Palette Child: %d", fail);			MessageBox(NULL, buf, "Scheiss", MB_OK);			Map_Window(client);			SetWindowPos(hwndMap,NULL, client.left, client.top, client.right, client.bottom, NULL);			sprintf(buf, "Fehler Palette Child: %d", fail);			MessageBox(NULL, buf, "Scheiss", MB_OK);		}break;	case WM_COMMAND:        {  			hMenu = GetMenu (hwnd);			switch (LOWORD (wparam))	        {			case ID_DATEI_BEENDEN :				{					PostQuitMessage(0);				}break;			case ID_MENU_ABOUT :				{					MessageBox(hwnd, "Created by Flunserl for Abinator, 2000 - 200X", "About : ", MB_OK);				}break;			case ID_ANSICHT_PALETTE :				{					mdicreate.szClass = g_szChildClassName;					mdicreate.szTitle = TEXT ("Palette") ;					mdicreate.hOwner  = hInstance ;					mdicreate.x       = CW_USEDEFAULT ;					mdicreate.y       = CW_USEDEFAULT ;					mdicreate.cx      = CW_USEDEFAULT ;					mdicreate.cy      = CW_USEDEFAULT ;					mdicreate.style   = 0 ;					mdicreate.lParam  = 0 ;		               					hwndChild = (HWND)SendMessage (hwndPalette, WM_MDICREATE, 0,										(LPARAM) (LPMDICREATESTRUCT) &mdicreate);				}break;			case ID_DATEI_OEFFNEN :				{					dialog_open_file(hwnd);				}break;			default :				{             					hwndChild = (HWND) SendMessage (hwndPalette, WM_MDIGETACTIVE, 0, 0) ;		               					if (IsWindow (hwndChild))							SendMessage (hwndChild, WM_COMMAND, wparam, lparam) ;				}break ;        // ...und danach an DefFrameProc			}		} break;	case WM_VSCROLL:		{			switch (LOWORD (wparam))			{				case SB_LINEUP:					view_y--;					break; 			    case SB_LINEDOWN:					view_y++;					break;			    case SB_PAGEUP:				    view_y = view_y - scroll_per_page;					if (view_y < 0) view_y = 0;					break;			    case SB_PAGEDOWN:					view_y = view_y + scroll_per_page;					if (view_y > feldy) view_y = feldy;					break;			    case SB_THUMBPOSITION:					view_y = HIWORD (wparam);					break;			    default : break ;			}			if (view_y != GetScrollPos (hwnd, SB_VERT))			{				if (view_y < 0) view_y = 0;				if (view_y > scroll_y) view_y = scroll_y;                SetScrollPos (hwnd, SB_VERT, view_y, TRUE) ;                InvalidateRect (hwnd, NULL, TRUE) ;			    UpdateWindow (hwnd) ;			}        return 0 ;		}break;	case WM_HSCROLL:		{			switch(LOWORD(wparam))			{				case SB_LINELEFT:					view_x--;					break;				case SB_LINERIGHT:					view_x++;					break;				case SB_PAGELEFT:					view_x = view_x - scroll_per_page;					if (view_x < 0) view_x = 0;					break;				case SB_PAGERIGHT:					view_x = view_x + scroll_per_page;					if (view_x > feldx) view_x = feldx;					break;				case SB_THUMBPOSITION:					view_x = HIWORD(wparam);					break;				default:break;			}			if (view_x != GetScrollPos (hwnd, SB_HORZ))			{				if (view_x < 0) view_x = 0;				if (view_x > scroll_x) view_x = scroll_x;                SetScrollPos (hwnd, SB_HORZ, view_x, TRUE) ;                InvalidateRect (hwnd, NULL, TRUE) ;			    UpdateWindow (hwnd) ;			}		}break;	case WM_DESTROY:		{			CleanUp();			PostQuitMessage(0);			return(0);		} break;	case WM_TIMER:        { 			MessageBeep (0) ;            return 0 ;		}break;	case WM_MOUSEMOVE:		{			Mouse_Move.x = LOWORD(lparam);			Mouse_Move.y = HIWORD(lparam);		}break;		default:return DefFrameProc(hwnd, hwndPalette, message, wparam, lparam);	}	}LRESULT CALLBACK MapProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){	switch (LOWORD(wparam))	{	case WM_CREATE :		{		}break;	case WM_COMMAND :		{		}break;	case WM_MDIACTIVATE :		{		}break;	default:break;	}	return DefMDIChildProc(hwnd, message, wparam, lparam);}LRESULT CALLBACK PaletteProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){	switch (LOWORD(wparam))	{	case WM_CREATE :		{		}break;	case WM_COMMAND :		{		}break;	case WM_MDIACTIVATE :		{		}break;	default:break;	}	return DefMDIChildProc(hwnd, message, wparam, lparam);}   

Please help me, cant find the problem...

[edited by - Metzler on July 31, 2002 5:10:20 PM]
I see where you are using the createwindowex and receiving the HWND into hwndMap, but not where you are declaring it, is it static? If it isn''t then it will lose scope when you exit the function and it will revert to undefined behavior.
Found the Problem : I redeclared the HWND hwndPalette in the callback function of the framewindow. I ask myself, why Visual Studio didnt give me a warning...

This topic is closed to new replies.

Advertisement