[comp.windows.ms.programmer] How to open two windows from one application

koziol@yoyodyne.ncsa.uiuc.edu (Quincey Koziol) (04/13/91)

Ok, I'm stumped.  How does one open two windows from one Windows App?  I have
tried so many combinations I'm really getting sick of this problem.  Attached
below is the relevant section of my current iteration of the code:

===============================================================================

char far szAppName[] = "SciVis";       /* App. name           */
char far szPalName[] = "SciVis_Pal";   /* Name of the palette window */

typedef struct info_node {  /* structure for information about the window */
    WORD WindowWidth,       /* the width and height of the data int the window in pixels */
        WindowHeight;
  } wind_info;

typedef struct wind_node {  /* a structure which contains all the information needed to display a window on the screen */
    HPALETTE hWindPal;      /* handle to the window's palette */
    HANDLE hdibWindow;      /* handle to the window's DIB */
    HBITMAP hbmWindow;      /* handle to the window's BM */
    HANDLE hbiWindow;       /* handle to the window's BitmapInfo */
  } window;

HPALETTE hpalCurrent = NULL;/* Handle to current palette           */
HANDLE hdibCurrent = NULL;	/* Handle to current memory DIB	       */
HBITMAP hbmCurrent = NULL;	/* Handle to current memory BITMAP      */
HANDLE hbiCurrent = NULL;	/* Handle to current bitmap info struct */

WORD wBitmapWidth,wBitmapHeight,    /* width and height of the bitmap loaded currently */
    wDisplayWidth,wDisplayHeight;   /* width and height of the bitmap displayed currently */
HWND hWndApp;               /* Handle to app. window           */
HWND hDebugWnd;             /* Handle to the debugging window */
HWND hPaletteWnd;           /* Handle to the palette window */

short xScreen, yScreen;     /* size of the screen */
short xFrame, yFrame;       /* frame around the sizeable window */
short MenuHeight;           /* height of the menu bar */
short CaptionHeight;        /* height of caption */

/* Styles of app. window */
DWORD dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME;

DWORD dwPalStyle = DS_MODALFRAME | WS_OVERLAPPED | DS_NOIDLEMSG | WS_CAPTION | WS_BORDER ;

/****************************************************************************
 *									    *
 *  FUNCTION   : WinMain(HANDLE, HANDLE, LPSTR, int)			    *
 *									    *
 *  PURPOSE    : Creates the app. window and enters the message loop.	    *
 *									    *
 ****************************************************************************/
int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
HANDLE hInstance, hPrevInstance;
LPSTR lpszCmdLine;
int nCmdShow;
{
	HWND hWnd;
    WNDCLASS wndclass;
    WNDCLASS palclass;
    FARPROC lpProc;
	MSG msg;
	char ach[40];

	hInst = hInstance;

	/* default to MEMORY DIB's if XWindows */
	bMemoryDIB = WinFlags & WF_PMODE;

	/* Initialize clip rectangle */
	SetRectEmpty(&rcClip);

    /* get a procedure instance for the palette window */
    lpProc=MakeProcInstance((FARPROC)PalWndProc,hInst);

	if (!hPrevInstance) {
		wndclass.style = CS_DBLCLKS;
		wndclass.lpfnWndProc = WndProc;
		wndclass.cbClsExtra = 0;
		wndclass.cbWndExtra = 0;
		wndclass.hInstance = hInstance;
		wndclass.hIcon = LoadIcon(hInst, "SHOWICON");
		wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
		wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
		wndclass.lpszMenuName = szAppName;
		wndclass.lpszClassName = szAppName;

		if (!RegisterClass(&wndclass))
			return FALSE;

        palclass.style = CS_DBLCLKS;
        palclass.lpfnWndProc = lpProc;
        palclass.cbClsExtra = 0;
        palclass.cbWndExtra = 0;
        palclass.hInstance = hInstance;
        palclass.hIcon = LoadIcon(hInst, "SHOWICON");
        palclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        palclass.hbrBackground = GetStockObject(BLACK_BRUSH);
        palclass.lpszMenuName = szPalName;
        palclass.lpszClassName = szPalName;

        if (!RegisterClass(&palclass))
			return FALSE;
    }

	/* Save the pointer to the command line */
	lstrcpy(achFileName, lpszCmdLine);

	xScreen = GetSystemMetrics(SM_CXSCREEN);
	yScreen = GetSystemMetrics(SM_CYSCREEN);
    xFrame = GetSystemMetrics(SM_CXFRAME);
    yFrame = GetSystemMetrics(SM_CYFRAME);
    MenuHeight = GetSystemMetrics(SM_CYMENU);
    CaptionHeight = GetSystemMetrics(SM_CYCAPTION);

	/* Create the app. window */
    hWndApp = hWnd =CreateWindow(szAppName,szAppName,dwStyle,CW_USEDEFAULT,
                0,xScreen / 2,yScreen / 2,NULL,NULL,hInstance,NULL);

    ShowWindow(hWndApp , nCmdShow);

    hDebugWnd=CreateDebugWindow(hWndApp);      /* Open the debugging window */

    hPaletteWnd = CreateWindow(szPalName,szPalName,dwPalStyle,0,
                yScreen-(24+CaptionHeight+10),256+2,24+CaptionHeight+8,NULL,NULL,hInstance,NULL);

ErrMsg("Palette Window Handle:%d",(int)hPaletteWnd);
ErrMsg("szPalName=%ld",(long int)szPalName);

    ShowWindow(hPaletteWnd , SW_SHOWNORMAL);


	/* Enter message loop */
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

    FreeProcInstance(lpProc);   /* free the procedure instance for the palette */

	return msg.wParam;
}

===============================================================================

Somebody please help me, this is really driving me mad!

		Quincey Koziol
		Programmer
		NCSA

koziol@ncsa.uiuc.edu

bcw@rti.rti.org (Bruce Wright) (04/14/91)

In article <1991Apr12.181046.14531@ux1.cso.uiuc.edu>, koziol@yoyodyne.ncsa.uiuc.edu (Quincey Koziol) writes:
> 
> Ok, I'm stumped.  How does one open two windows from one Windows App?  I have
> tried so many combinations I'm really getting sick of this problem.  Attached
> below is the relevant section of my current iteration of the code:

I've had no trouble creating multiple windows for a single app - you
must be doing something wrong.  A few possible problems:

>     /* get a procedure instance for the palette window */
>     lpProc=MakeProcInstance((FARPROC)PalWndProc,hInst);

1) You don't need to do a MakeProcInstance on a window function - 
   only on a dialog box function, a window subclass function, or
   some type of callback function.

>         palclass.style = CS_DBLCLKS;
>         palclass.lpfnWndProc = lpProc;

2) Set palclass.lpfnWndProc to PalWndProc (just like the class 
   structure for the main window sets lpfnWndProc to WndProc).

3) Make SURE you export the PalWndProc function in your .DEF file.

4) You may want to force a WM_PAINT of one or both windows after 
   you create them by adding an UpdateWindow (hPaletteWnd) or
   UpdateWindow (hWndApp) call after the ShowWindow call for the
   appropriate window.

That's all I see or can think of offhand.  There may be something 
else, either in code that you didn't post (like in the window
function for the palette window) or that just escaped me when I
read over the article.

Good luck -

						Bruce C. Wright

bonneau@hyper.hyper.com (Paul Bonneau) (04/15/91)

In article <1991Apr12.181046.14531@ux1.cso.uiuc.edu> koziol@yoyodyne.ncsa.uiuc.edu (Quincey Koziol) writes:
>
>Ok, I'm stumped.  How does one open two windows from one Windows App?  I have
>tried so many combinations I'm really getting sick of this problem.  Attached
>below is the relevant section of my current iteration of the code:
>
>DWORD dwPalStyle = DS_MODALFRAME | WS_OVERLAPPED | DS_NOIDLEMSG | WS_CAPTION | WS_BORDER ;
>
.
.
.
>
>    hPaletteWnd = CreateWindow(szPalName,szPalName,dwPalStyle,0,
>                yScreen-(24+CaptionHeight+10),256+2,24+CaptionHeight+8,NULL,NULL,hInstance,NULL);
>
The problem is you have not used the WS_VISIBLE style with
the palette window.  Either call ShowWindow() after creating
it, or use the WS_VISIBLE style when creating it.

Also, you may prefer to use the WS_POPUP style, since this
type of window will "follow" its parent around, instead of
appearing as if two different applications are present.

cheers - Paul Bonneau.