Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows VisualStudio
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:
// 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 23-Sep-12 3:44am
Je7505
Comments
pasztorpisti at 23-Sep-12 9:49am
   
And what is your problem? What do you mean on "same style"?
ProgramFOX at 23-Sep-12 9:49am
   
Do you think someone want to read all your code?
pasztorpisti at 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 at 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 at 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.
pasztorpisti at 23-Sep-12 11:18am
   
Then we could simply write the code instead of you because that's faster than dictating you what to write and where. If you finish 1-2 short beginner win32 gui tutorials this questions becomes no question.
Je7 at 23-Sep-12 11:25am
   
Ok. I understand you. I already read the tutorial about Dialogs windows. And there are two little questions:
 
1. I created the dialog window (as a resource) with edit boxes. So how i can take the Text from editboxes?
2. Maybe there is a way to create the same window but without resource file? I mean only WinAPI, without any MFC or something.
pasztorpisti at 23-Sep-12 11:37am
   
First use google to find the answer for example with the following keywords: "get text from dialog editbox win32 api". If you don't find the answer then come back and see the solution. (()txeTmetIglDteG).
Je7 at 23-Sep-12 12:02pm
   
GetDlgItemText - i used this function. So, and what about second question? There is a way to make dialog without .rc help?
pasztorpisti at 23-Sep-12 12:16pm
   
The dialog resource is pure WinaAPI. The sad fact that MFC builds on top of that is a thing. If you want to create a window that has lots of controls in it like buttons, editboxes, labels then the best and most comfortable way is to go with dialog resources and the DialogBox() api call. Maybe the tutorial doesn't mention but there are graphical designers that allow you to design dialogs easily and they save the textual resource file for you. Another big advantage of using dialog resources is that your windows will be independent of screen resolution unlike the windows you create with CreateWindowEx without special handling. Because of the visual dialog resource editors its impractical to go with CreateWindowEx. In most applications both CreatewindowEx and dialog resources are used, CreateWindowEx is used only for windows that contain 1-or two controls that cover the whole client area of the window (like in a paintbrush) but even in that case option dialogs and other kind of windows are usually dialogs. Again: the dialog resource stuff is native winapi, get a resource editor from somewhere or use the one that is built into Visual Studio if you use that IDE.
Je7 at 23-Sep-12 12:35pm
   
Ok, i got it. I haven't any question about dialogs, they are pretty good, and i am coding the project now with their help. If is not very hard for you, can you give me information about "low-level" API? I mean how to create all that but without dialogs.
pasztorpisti at 23-Sep-12 12:58pm
   
Sorry but then you have to go on your own because this whole thing points too far. Anyway what DialogBox, CreateDialog and its friends do is also just making a series of calls to CreateWindowEx(), they do data driven creation of a HWND hierarchy. If you have to finish your project quickly then go with dialogs, if you are just interested in the whole stuff then search for tutorials on the net or buy a good book like: http://shop.oreilly.com/product/0790145369079.do
I'm here to answer specific questions but sorry, I have no time to teach my own padavans...
Je7 at 23-Sep-12 13:06pm
   
Thank you for patience and great help. You give me almost priceless information for me. I wish success you in your work.
pasztorpisti at 23-Sep-12 13:11pm
   
Thank you, and good luck! Call again if you have specific questions/obstacles while learning!
pasztorpisti at 23-Sep-12 11:15am
   
Agree. Redundancy also makes things harder to understand sometimes. The biggest problem here with the auto generated code is that the OP doesn't understand what its about.
Sergey Alexandrovich Kryukov at 23-Sep-12 11:27am
   
I'm saying so because the the bare skeleton code OP described in words should be translated in much shorter Win32 API based code.
--S
pasztorpisti at 23-Sep-12 11:31am
   
That's absolutely true.
Je7 at 23-Sep-12 10:31am
   
The same style, means that child as a main window, has a menu. If you look into the code, you'll see that in class reg, child does not have menu support. Yes, this code is auto-generated with some modification, and yes, i think that someone look on this code, becouse 99% of it is auto-generated, and there are only a little bit of code, typed by me.
Je7 at 23-Sep-12 10:34am
   
p.s.: I'm reading about dialog windows now, i hope, it's help me. I'll write here the result. Nevertheless thank both of you for answers.
Andrewpeter at 23-Sep-12 10:57am
   
What do you use WndProcC() function to do?
Je7 at 23-Sep-12 11:03am
   
WndProcC the same to WndProc. Same in what to do. But WndProcC for child. But this function is almost empty, becouse it does not have any practically work.
Andrewpeter at 23-Sep-12 11:53am
   
You can see my answer below.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Comments
Richard MacCutchan at 23-Sep-12 12:48pm
   
CreateWindow() is perfectly OK to create a windo that does not use extended styles.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I decided to use dialog windows. And i received exactly information about this.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 500
1 Prakriti Goyal 292
2 Yogesh Kumar Tyagi 224
3 Maciej Los 195
4 CPallini 170
0 OriginalGriff 6,652
1 Sergey Alexandrovich Kryukov 5,449
2 Maciej Los 3,474
3 Peter Leow 3,303
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100