How to open a New window

closed account (ozUkoG1T)
Hi C++ Forums,

I am making a simple program which opens a New Window when a correct Password is Entered.
But i do not know how i can do that : The source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<WinSock2.h>
#include<Windows.h>
#include<tchar.h>
#pragma comment(lib,"ws2_32.lib")
const char Cybernetwork[] ="Cyber Network";
HWND HWND_EDIT_Number;
HWND HWND_Button_Enter;
HWND HWND_Edit_Login;
char Number[100];
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg , WPARAM wParam , LPARAM lParam)
{
	switch (msg)
	{
	case WM_CREATE:
		HWND_EDIT_Number= CreateWindow(_T("Edit"),_T(""),WS_BORDER|WS_CHILD|WS_VISIBLE,0,20,100,20,hwnd,NULL,NULL,NULL);
		HWND_Button_Enter = CreateWindow(_T("Button"),_T("Login"),BS_PUSHBUTTON|WS_VISIBLE|WS_BORDER|WS_CHILD,0,40,50,20,hwnd,(HMENU)1001,NULL,NULL);
		HWND_Edit_Login= CreateWindow(_T("Edit"),_T("Welcome, to Cyber Network."),WS_BORDER|WS_DISABLED|WS_VISIBLE|WS_CHILD,0,0,500,20,hwnd,NULL,NULL,NULL);
		break;
	case WM_COMMAND:
		if(LOWORD(wParam)==1001)
		{
			GetWindowText(HWND_EDIT_Number,Number,100);
			if(strcmp(Number,"Cyberwarfare")==0)
			{
			MessageBox(NULL,"Correct Password Successfully Logged in ","Password Authentication",MB_OK);
			}
			else
			{
			MessageBox(NULL,"Password Incorrect","Password Authentication",MB_OK);
			}
		}
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(1);
		break;
	default:
		return DefWindowProc(hwnd,msg,wParam , lParam);
	}
	return 0;
}
int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevhInstance , LPSTR CmdLine,int CmdShow)
{
	WNDCLASSEX ws;
	HWND hwnd ;
	MSG msg;
	ws.cbClsExtra = 0;
	ws.cbSize = sizeof(WNDCLASSEX);
	ws.cbWndExtra = 0;
	ws.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
	ws.hCursor = LoadCursor(NULL,IDC_ARROW);
	ws.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	ws.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
	ws.hInstance= hInstance;
	ws.lpfnWndProc= WndProc;
	ws.lpszClassName= Cybernetwork;
	ws.lpszMenuName = 0;
	ws.style = 0;
	if(!RegisterClassEx(&ws))
	{
	MessageBoxA(NULL,"Error During Registering Class","Error",MB_OK | MB_ICONERROR);
	}
	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE ,Cybernetwork,Cybernetwork,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT ,500,500,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,CmdShow);
	UpdateWindow(hwnd);
	while(GetMessage(&msg,0,0,0)>0)
	{
		TranslateMessage(&msg);
		DispatchMessageA(&msg);
	}
	return msg.wParam; 
}
Last edited on
Here are some hints ...

In WinMain() you were able to create a main window of class "Cyber Network" by registering a custom class through a WNDCLASSEX type.

In your WM_CREATE handler you were able to create child window controls (which are windows in their own right) by specifying such window classes as "edit", "button", etc.

What's the similiarity?

You were able to create these windows either by registering your own custom window class, i.e., "Cyber Network", or using pre-existing window classes provided by Windows.

Time for you to pick up the ball ...

closed account (ozUkoG1T)
So do i make a new Window Procudure and a WinMain()
You just need another CreateWindows(Ex)

BTW - most people would use a dialog for logon purposes, rather than the main window.

Option #1

If you use a modal dialog (ie use DialogBox), then it can be used before the app's message loop starts (as they use their own message loop). So it would be called from WinMain, before the main window is registered, etc. Note that without a parent, you would need to position the dialog wrt the desktop yourself.

Option #2

Display the logon dialog from the main window while it's hidden.

You don't have to call ShowWindow immediately after creating it, so the dialog can appear before the main window becomes visible.

Andy
Glad to see you are putting the pieces together!

My general technique is to create a seperate Window Class for each window or screen my program presents. So, for each window class you register, you are going to need a Window Procedure (its one of the most important fields of the WNDLASSEX struct). This makes sense, and routes messages for each specific type (read class) to the Window Procedure for that specific kind of object. It is the code within a Window Procedure which gives the entire character to a Window Object.

I consider the WM_CREATE message to be a C based object constructor call for a window. It is my style to register all Window Classes that my app will use within the WM_CREATE handler of my main start up form/window/dialog. Having done that, the string class name will be recognizable to windows in any CreateWindow() /CreateWindowEx() call you wish to make at any point in your program. Further note that it isn't necessary to maintain any global variables in a Windows GUI program at all.

Topic archived. No new replies allowed.