Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WinXP C++ VisualStudio
Hi. The code:
// 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.
 
                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 18-Sep-12 7:52am
Je7506
Edited 18-Sep-12 9:09am
v3
Comments
Je7 at 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 at 18-Sep-12 14:29pm
   
As soon as you write "case 100" you already go nowhere...
--SA
Je7 at 18-Sep-12 14:50pm
   
Yes, I know. I commented this becouse i've tried some code fragments.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Je7 at 18-Sep-12 14:48pm
   
Thank you for information too.
pasztorpisti at 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 at 18-Sep-12 16:35pm
   
+5 because of the book that is really a nice start for winapi.
Espen Harlinn at 18-Sep-12 16:40pm
   
Thank you :-D
Mohibur Rashid at 18-Sep-12 20:52pm
   
To close the child window op will have to use DestroyWindow function. It will destroy the specific Window.
Espen Harlinn at 20-Sep-12 4:59am
   
Note what OP writes:"If I close it, all program is closing"
So he is able to close his window, his problem is that this exits the program
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Je7 at 18-Sep-12 14:47pm
   
Thank you for tutorials.

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

  Print Answers RSS
0 OriginalGriff 478
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Uma Shankar Patel 80
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 18 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