Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi there. I'm trying to configure my windows application.
What i want:
Click button->open the new window.
What i have:
i have close enough what i want, but the child window has the same style with main window. Help, please. Here the code:
C++
// normal.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "normal.h"

#define MAX_LOADSTRING 100
#define ID_BUTTON 3000

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
TCHAR szWindowChildClass[MAX_LOADSTRING];

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
ATOM				RegisterChildClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	WndProcC(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_NORMAL, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	LoadString(hInstance, IDC_NORMAL, szWindowChildClass, MAX_LOADSTRING);
    RegisterChildClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_NORMAL));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_NORMAL));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_NORMAL);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

ATOM RegisterChildClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcexChild;

	wcexChild.cbSize = sizeof(WNDCLASSEX);

	wcexChild.style			= CS_HREDRAW | CS_VREDRAW;
	wcexChild.lpfnWndProc	= WndProcC;
	wcexChild.cbClsExtra		= 0;
	wcexChild.cbWndExtra		= 0;
	wcexChild.hInstance		= hInstance;
	wcexChild.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_NORMAL));
	wcexChild.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcexChild.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcexChild.lpszMenuName	= 0;
	wcexChild.lpszClassName	= szWindowClass;
	wcexChild.hIconSm		= LoadIcon(wcexChild.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcexChild);
}
//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	HWND button1 = NULL, wind;

	switch (message)
	{
	case WM_CREATE:
		{
			button1 = CreateWindow("button","Button",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 10,30,40,20,hWnd,(HMENU)ID_BUTTON,hInst, NULL);
			break;
		}
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		case ID_BUTTON:
			{
			wind = CreateWindow(szWindowChildClass, "Child", WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, hWnd, NULL, hInst, NULL);
			ShowWindow(wind,SW_SHOWNORMAL);
			UpdateWindow(wind);
			break;
			}
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

LRESULT CALLBACK WndProcC(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
Posted
Comments
pasztorpisti 23-Sep-12 9:49am    
And what is your problem? What do you mean on "same style"?
Thomas Daniels 23-Sep-12 9:49am    
Do you think someone want to read all your code?
pasztorpisti 23-Sep-12 9:53am    
This seems to be an auto-generated code with some functions copy pasted and slightly modified... I think you should start with the basics and read a win32 gui tutorial before asking SPECIFIC questions on this forum. This is a good place to start at: http://www.winprog.org/tutorial/
I guess the problem is that the secondary window should be a MODAL DIALOG, and thats your problem, search for modal dialog examples in the tutorial I linked.
Here is the modal dialog part: http://www.winprog.org/tutorial/dialogs.html
Sergey Alexandrovich Kryukov 23-Sep-12 11:03am    
Using auto-generated code for such purpose is often bad. It usually contains a lot of redundancies and is poorly supportable, because it is designed to make auto-generation simpler, not to meet criteria of good code. I would really recommend writing the code from scratch using the documentation.
--SA
Je7 23-Sep-12 11:09am    
Ok,I'll do this without auto-generated code. But, just tell me, how to open another window via button. I only need an answer: what and where to type.

The first, you have to create a button with CreateWindowEx() function, not CreateWindow().
The second, you should make the same code in About(). Good luck!
 
Share this answer
 
Comments
Richard MacCutchan 23-Sep-12 12:48pm    
CreateWindow() is perfectly OK to create a windo that does not use extended styles.
You are creating your child window using the same style as your main window so it will look the same. You need to use a different class and make sure it uses the WS_CHILD style to create a non-main window.
 
Share this answer
 
I decided to use dialog windows. And i received exactly information about this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900