[c++] Multiple windows

Pagina: 1
Acties:
  • 321 views

Onderwerpen


Acties:
  • 0 Henk 'm!

  • lauwsa
  • Registratie: Juli 2010
  • Laatst online: 10-09 20:43
Ik wil 3 Windows maken in een Windows Application,
ik krijg 3 schermen na voren alleen het probleem is dat het 3x het zelfde scherm is.

edit:
Dit heb ik voor elkaar gekregen door 3x het zelfde lpszClassName te gebruiken, ik probeerde om deze 3x verschillend te maken alleen dan krijg ik een error.


Ik heb zo een WNDCLASS gemaakt,

code:
1
2
3
4
5
6
7
8
9
10
11
    WNDCLASS wc3;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc3;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = ghAppInst3;
    wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
    wc.hCursor = ::LoadCursor(0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = 0;
    wc.lpszClassName = "base3";


Zelf denk ik dat hier het probleem licht:
wc.lpszClassName = "base3";

Als ik voor allen 3 de schermen een anderen naam daar neer zet, dan lukt het window maken niet. Als ik dus base1 - 2 - 3 gebruik dan failt het maken van de windows.

Zo ziet een Create window er uit:

code:
1
2
3
4
5
    ghMainWnd = ::CreateWindow("base", "window base", ( WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_CAPTION | WS_VSCROLL ), 0, 0, 500, 500, 0, 0, ghAppInst, 0);
    if(ghMainWnd == 0)  {
        ::MessageBox(0, "CreateWindow - Failed", 0, 0);
        return false;
    }


code:
1
2
3
4
5
    ghMainWnd2 = ::CreateWindow("base2", "window base", ( WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_CAPTION | WS_VSCROLL ), 0, 0, 500, 500, 0, 0, ghAppInst2, 0);
    if(ghMainWnd2 == 0)     {
        ::MessageBox(0, "CreateWindow - Failed", 0, 0);
        return false;
    }


Je kan hier zien dat ik 3x verschilende namen gebruik, want zo denk ik dat ik het moet doen, maar ik krijg een error hier door. Zelfde class namen gebruiken krijg ik 3x het zelfde scherm.

Dus:
ghMainWnd geeft 0 terug als je 2 of meer verschilende lpszClassName hebt.

Ik heb geprobeerd om allen dingen van allen Windows apart te groeperen onder elkaar, alleen dit wil niet helpen

Dit is nog een WinProc:

code:
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
LRESULT CALLBACK
    WndProc2(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch( msg )   {
        // Handle left mouse button click message.
        case WM_LBUTTONDOWN:
        MessageBox(0, "Window 2", "Msg", MB_OK);
        return 0;

        // Handle key down message.
        case WM_KEYDOWN:
        if( wParam == VK_ESCAPE )
            if(MessageBox(0,"Weet u zeker dat u wilt stoppen?","Quite",MB_YESNO) == IDYES) {
                DestroyWindow(ghMainWnd2);
            }
        if ( wParam == VK_NUMPAD1)
            MessageBox(0,"1","Numpad", MB_OK);
        return 0;

        // Handle destroy window message.
        case WM_DESTROY:
            PostQuitMessage(0);
        return 0;
    }

    // Forward any other messages we didn't handle to the
    // default window procedure.
    return DefWindowProc(hWnd, msg, wParam, lParam);
}


Zou iemand me hier mee kunnen helpen? Ik heb nog geen oplossing op google kunnen vinden.

[ Voor 3% gewijzigd door lauwsa op 09-12-2010 14:08 ]


Acties:
  • 0 Henk 'm!

  • CodeCaster
  • Registratie: Juni 2003
  • Niet online

CodeCaster

Can I get uhm...

Dan kijk je dus in MSDN wat lpszClassName doet:
If lpszClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.
Je moet dus, denk ik, maar ik heb niet veel met C++ en de Windows API gewerkt, per scherm een window-klasse maken en die met RegisterClass registreren. Daarna kun je ze pas aan de lpszClassName meegeven.

https://oneerlijkewoz.nl
Op papier is hij aan het tekenen, maar in de praktijk...


Acties:
  • 0 Henk 'm!

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 03:42

.oisyn

Moderator Devschuur®

Demotivational Speaker

Daar is z'n WNDCLASS structure dus voor. Maar ik gok eerlijk gezegd dat je maar 1x zo'n class registreert. Je moet die registratie dus ook 3x doen, met voor elk een andere class name.

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Acties:
  • 0 Henk 'm!

  • lauwsa
  • Registratie: Juli 2010
  • Laatst online: 10-09 20:43
Dit is een werkend voorbeeld:

code:
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
75
76
77
78
79
80
81
82
83
84
#include "StdAfx.h"
#include <windows.h>

//Vorialbels voor de windows
HWND ghMainWnd = 0; //de HWND na de window
HINSTANCE ghAppInst = 0; //Hinstance na de window

// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
    WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch( msg )   {
        // Handle left mouse button click message.
        case WM_LBUTTONDOWN:
        MessageBox(0, "WM_LBUTTONDOWN message.", "Msg", MB_OK);
        return 0;

        // Handle key down message.
        case WM_KEYDOWN:
        if( wParam == VK_ESCAPE )
            if(MessageBox(0,"Weet u zeker dat u wilt stoppen?","Quite",MB_YESNO) == IDYES) {
                DestroyWindow(ghMainWnd);
            }
        if ( wParam == VK_NUMPAD1)
            MessageBox(0,"1","Numpad", MB_OK);
        return 0;

        // Handle destroy window message.
        case WM_DESTROY:
            PostQuitMessage(0);
        return 0;
    }

    // Forward any other messages we didn't handle to the
    // default window procedure.
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

// WinMain: Entry point for a Windows application.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd) {

    // Save handle to application instance.
    ghAppInst = hInstance;

    // Step 2: Fill out a WNDCLASS instance.
    WNDCLASS wc;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = ghAppInst;
    wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
    wc.hCursor = ::LoadCursor(0, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = 0;
    wc.lpszClassName = "base";

    // Step 3: Register the WNDCLASS instance with Windows.
    RegisterClass( &wc );

    // Step 4: Create the window, and save handle in globla
    // window handle variable ghMainWnd.
    ghMainWnd = ::CreateWindow("base", "window base", ( WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_CAPTION | WS_VSCROLL ), 0, 0, 500, 500, 0, 0, ghAppInst, 0);
    if(ghMainWnd == 0)  {
        ::MessageBox(0, "CreateWindow - Failed", 0, 0);
        return false;
    }

    // Step 5: Show and update the window.
    ShowWindow(ghMainWnd, showCmd);
    UpdateWindow(ghMainWnd);

    // Step 6: Enter the message loop and don't quit until
    // a WM_QUIT message is received.
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));

    while( GetMessage(&msg, 0, 0, 0) )  {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Return exit code back to operating system.
    return (int)msg.wParam;
}


Ik heb alles via deze volgorde gedaan, ben weer begonnen met uitproberen wat er mis kan zijn.

edit:
RegisterClass heb ik voor allen WNDCLASS's gedaan


edit:
Moet ik anders even nog eens maken hoe ik dit wil doen, en dan de bron code posten? Want ik heb:

3x HWND
3x HINSTANCE
3x "WndProc"
3x WNDCLASS
3x RegisterClass
3x CreateWindow
3x ShowWindow
3x UpdateWindow
1x msg loop



edit:
Dat voorbeeld is een single window die ik eerder "gemaakt heb", en die wil ik dus verandere in 3 windows.


edit:
#include "StdAfx.h"
#include <windows.h>

//Vorialbels voor de windows
HWND ghMainWnd = 0; //de HWND na de window
HWND ghMainWnd2 = 0; //de HWND na de window
HWND ghMainWnd3 = 0; //de HWND na de window

HINSTANCE ghAppInst = 0; //Hinstance na de window
HINSTANCE ghAppInst2 = 0; //Hinstance na de window
HINSTANCE ghAppInst3 = 0; //Hinstance na de window

// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch( msg ) {
// Handle left mouse button click message.
case WM_LBUTTONDOWN:
MessageBox(0, "WM_LBUTTONDOWN message.", "Msg", MB_OK);
return 0;

// Handle key down message.
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
if(MessageBox(0,"Weet u zeker dat u wilt stoppen?","Quite",MB_YESNO) == IDYES) {
DestroyWindow(ghMainWnd);
}
if ( wParam == VK_NUMPAD1)
MessageBox(0,"1","Numpad", MB_OK);
return 0;

// Handle destroy window message.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

// Forward any other messages we didn't handle to the
// default window procedure.
return DefWindowProc(hWnd, msg, wParam, lParam);
}

LRESULT CALLBACK
WndProc2(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch( msg ) {
// Handle left mouse button click message.
case WM_LBUTTONDOWN:
MessageBox(0, "WM_LBUTTONDOWN message.", "Msg", MB_OK);
return 0;

// Handle key down message.
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
if(MessageBox(0,"Weet u zeker dat u wilt stoppen?","Quite",MB_YESNO) == IDYES) {
DestroyWindow(ghMainWnd2);
}
if ( wParam == VK_NUMPAD1)
MessageBox(0,"1","Numpad", MB_OK);
return 0;

// Handle destroy window message.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

// Forward any other messages we didn't handle to the
// default window procedure.
return DefWindowProc(hWnd, msg, wParam, lParam);
}

LRESULT CALLBACK
WndProc3(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch( msg ) {
// Handle left mouse button click message.
case WM_LBUTTONDOWN:
MessageBox(0, "WM_LBUTTONDOWN message.", "Msg", MB_OK);
return 0;

// Handle key down message.
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
if(MessageBox(0,"Weet u zeker dat u wilt stoppen?","Quite",MB_YESNO) == IDYES) {
DestroyWindow(ghMainWnd3);
}
if ( wParam == VK_NUMPAD1)
MessageBox(0,"1","Numpad", MB_OK);
return 0;

// Handle destroy window message.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

// Forward any other messages we didn't handle to the
// default window procedure.
return DefWindowProc(hWnd, msg, wParam, lParam);
}

// WinMain: Entry point for a Windows application.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd) {

// Save handle to application instance.
ghAppInst = hInstance;
ghAppInst2 = hInstance;
ghAppInst3 = hInstance;

// Step 2: Fill out a WNDCLASS instance.
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ghAppInst;
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "base";

WNDCLASS wc2;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc2;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ghAppInst2;
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "base2";

WNDCLASS wc3;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc3;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ghAppInst3;
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "base3";

// Step 3: Register the WNDCLASS instance with Windows.
RegisterClass( &wc );
RegisterClass( &wc2 );
RegisterClass( &wc3 );

// Step 4: Create the window, and save handle in globla
// window handle variable ghMainWnd.
ghMainWnd = ::CreateWindow("base", "window base", ( WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_CAPTION | WS_VSCROLL ), 0, 0, 500, 500, 0, 0, ghAppInst, 0);
if(ghMainWnd == 0) {
::MessageBox(0, "CreateWindow - Failed", 0, 0);
return false;
}

ghMainWnd2 = ::CreateWindow("base2", "window base", ( WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_CAPTION | WS_VSCROLL ), 0, 0, 500, 500, 0, 0, ghAppInst2, 0);
if(ghMainWnd2 == 0) {
::MessageBox(0, "CreateWindow - Failed", 0, 0);
return false;
}

ghMainWnd3 = ::CreateWindow("base3", "window base", ( WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_CAPTION | WS_VSCROLL ), 0, 0, 500, 500, 0, 0, ghAppInst3, 0);
if(ghMainWnd3 == 0) {
::MessageBox(0, "CreateWindow - Failed", 0, 0);
return false;
}

// Step 5: Show and update the window.
ShowWindow(ghMainWnd, showCmd);
UpdateWindow(ghMainWnd);

ShowWindow(ghMainWnd2, showCmd);
UpdateWindow(ghMainWnd2);

ShowWindow(ghMainWnd3, showCmd);
UpdateWindow(ghMainWnd3);

// Step 6: Enter the message loop and don't quit until
// a WM_QUIT message is received.
MSG msg;
ZeroMemory(&msg, sizeof(MSG));

while( GetMessage(&msg, 0, 0, 0) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Return exit code back to operating system.
return (int)msg.wParam;
}

Zo heb ik het nu gedaan, zo ver ik zie heb ik alles gedaan hoe het ook met 1 Windows moet, in me boek stond als opdracht dat ik in 1 applicatie 3 Windows moet maken. Er word dus niet uit gelecht hoe het moet :(

[ Voor 58% gewijzigd door lauwsa op 09-12-2010 15:13 ]


Acties:
  • 0 Henk 'm!

  • farlane
  • Registratie: Maart 2000
  • Laatst online: 15:26
Een HINSTANCE is een handle voor je applicatie, die heb je maar 1x nodig.

Verder zou je eens naar de documentatie van GetLastError() kunnen kijken, veel functies vertellen wat er fout gaat.

Somniferous whisperings of scarlet fields. Sleep calling me and in my dreams i wander. My reality is abandoned (I traverse afar). Not a care if I never everwake.


Acties:
  • 0 Henk 'm!

  • CodeCaster
  • Registratie: Juni 2003
  • Niet online

CodeCaster

Can I get uhm...

Die wc2 en wc3 vul je nooit. Ga eens debuggen, breakpoints zetten en zo.

https://oneerlijkewoz.nl
Op papier is hij aan het tekenen, maar in de praktijk...


Acties:
  • 0 Henk 'm!

  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
Inderdaad, ga eens debuggen. Je zou ondertussen moeten weten dat we het hier niet op prijs stellen als je zulke lappen code post.

Dus probeer eens wat meer te debuggen en als je meer informatie hebt kun je een nieuw topic openen. Maar lees dan PRG Beleid en De QuickStart nog eens door, zodat het volgende topic niet ook op slot gaat.

“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”

Pagina: 1

Dit topic is gesloten.