Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. The code:
C++
// Window.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam);
 
HINSTANCE hInst;
 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hWnd;
    MSG msg;
    
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "Base";
    wc.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
    
    if (!RegisterClassEx(&wc))
        return 0;
    if (!(hWnd = CreateWindowEx (NULL, "Base", "Програма керування локальними групами користувачів", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, 425, 300, NULL, NULL, hInst, NULL)))
        return (0);
    
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);
    
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hButton, hListBox, hComboBox, hWindow1;
    
    switch(message)
    {
    case WM_CREATE:
        {
            hButton = CreateWindow ("BUTTON", "Створення локальної групи", WS_CHILD | WS_VISIBLE,
                                    33, 0, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Визначення інформації про локальну групу", WS_CHILD | WS_VISIBLE,
                                    33, 50, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Перерахування створених локальних груп", WS_CHILD | WS_VISIBLE,
                                    33, 100, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Зміна інформації про групу", WS_CHILD | WS_VISIBLE,
                                    33, 150, 350, 20, hWnd, (HMENU)100, hInst, 0);
			hButton = CreateWindow ("BUTTON", "Видалення локальної групи", WS_CHILD | WS_VISIBLE,
                                    33, 200, 350, 20, hWnd, (HMENU)100, hInst, 0);
        }
        break;
    
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case 100:
            {
                //MessageBox (hWnd, "Button", "Другая форма", 0);
            }
        }
        break;
        
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return (0);
            }
        break;
    }
    return (DefWindowProc(hWnd, message, wParam, lParam));
}


So, how to create new window with special stuff like buttons and editbox? And how to take the information from that editbox, for some calculation. I need that after clicking the button, new window appears.


P.S.: I get a new window, but:
1. It's similar to main(or parent).
2. If I close it, all program is closing.

C++
hWindow1 = CreateWindowEx (NULL, "Base", "Програма керування локальними групами користувачів", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    CW_USEDEFAULT, CW_USEDEFAULT, 425, 300, GetParent(hWnd), NULL, hInst, NULL);

There are no other changes to the code, only this.
Posted
Updated 18-Sep-12 9:09am
v3
Comments
Je7 18-Sep-12 13:55pm    
I know that some code about child window must be in case 100: but which type of code, maybe it should create a new window class, with all stuff needed? Really i don't know. I'am a newbie in WinAPI. Looking for your's help.
Sergey Alexandrovich Kryukov 18-Sep-12 14:29pm    
As soon as you write "case 100" you already go nowhere...
--SA
Je7 18-Sep-12 14:50pm    
Yes, I know. I commented this becouse i've tried some code fragments.

You can use new objects in a callback function which catches events. Also a resources for dialogs can be used.
Please see this tutorial:
http://www.winprog.org/tutorial/modeless_dialogs.html[^]

The other way is to use MFC.
http://msdn.microsoft.com/en-us/library/d06h2x6e.aspx[^]
 
Share this answer
 
Comments
Je7 18-Sep-12 14:47pm    
Thank you for tutorials.
Now, that answer is given by Charles Petzold in his classic Programming Windows, 5th Edition[^]

Another nice source of information is Windows Application UI Development[^], and it looks like Jani Mäkinen wrote a nice article on how to create your first Windows application in C: Win32 Window Minimal[^]

Seriously, though, get yourself a copy of Petzolds' book - it's not terribly exciting, but he is thorough - providing you with a solid grounding on the basics of programming directly against the Windows API.

Windows via C/C++[^] by Jeffrey Richter and Christophe Nasarre is also well worth reading.

[Update]
2. If I close it, all program is closing.
You need a different WndProc for the second window that don't call PostQuitMessage on WM_DESTROY as this causes the message loop to terminate.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Je7 18-Sep-12 14:48pm    
Thank you for information too.
pasztorpisti 18-Sep-12 16:38pm    
Get the book and go with that if you are learning winapi in general. If you are learning and need gui then don't use MFC before you are familiar with winapi otherwise you will be confused since MFC uses a mixture of winapi and object oriented design that isn't ordinary sometimes. MFC tutorials will help you deepen your confusion about how things really work at low level winapi.
pasztorpisti 18-Sep-12 16:35pm    
+5 because of the book that is really a nice start for winapi.
Espen Harlinn 18-Sep-12 16:40pm    
Thank you :-D
Mohibur Rashid 18-Sep-12 20:52pm    
To close the child window op will have to use DestroyWindow function. It will destroy the specific Window.

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