Need Help With Buttons

IM NEW HERE AND im trying to make buttons for just a simple game im making to get back into coding. Basically its a little mmo im trying to make( not to play or to spread, just to practice)... Heres the sample of code im trying to get to use the buttons on...

________________________________________________________________________________

for(int i = 0; i<10; i++){

cout<< a << "- ";

CreateWindow(TEXT("button"), TEXT(RACE[i]), WS_VISIBLE | WS_CHILD,
10, 10, 80, 25, NULL, (HMENU) 1, NULL, NULL); << endl;

a++;
}

________________________________________________________________________________


i watched a youtube video on how to make buttons as Dev-C++ as my IDE... something im not sure how to do is figure out the name of my parent window, i tried hwnd but it didnt work, the name for the window goes where the first NULL value is.... im getting a few errors with what im tryign to compile now, one of which is because im trying to have the text on the button being displayed by the array and the for loop where it says TEXT(RACE[i])....


Errors:

1.In function `int main()':

2. cannot convert `std::string' to `const CHAR*' for argument `3' to `HWND__* CreateWindowExA(DWORD, const CHAR*, const CHAR*, DWORD, int, int, int, int, HWND__*, HMENU__*, HINSTANCE__*, void*)'

3.expected primary-expression before '<<' token

4.[Warning] multi-character character constant
I can only assume you are trying to make 10 buttons, but you are using the same coords for all of them. Also the <<endl; in the loop makes no sense, for that matter the cout doesn't either.

im getting a few errors with what im tryign to compile now, one of which is because im trying to have the text on the button being displayed by the array and the for loop where it says TEXT(RACE[i])
If race[] is an array of strings, use race[i].c_str().

something im not sure how to do is figure out the name of my parent window
You get it when you create the main window HWND hWnd = CreateWindow(...);
Last edited on
yeah im trying to make 10 buttons, the cout is there for just numbering the buttons. i have endl; so it doesnt put all the buttons out on the same line, and i am not sure what to make the coords if im having the buttons display after the cout with the number, could i just make the coords NULL values and just put it after the numbers? and should i be adding
(HWND hWnd =) before my CreateWindow(...);?
Also, i see how i dont get an error for the Array now, but what does .c_str() actually do?
The cout is NOT going to number the buttons, cout is the standard output stream (console). The coords are where you want to place the buttons on the main window (they are relative to the main window). You need the main windows handle to attach the child windows (buttons).
Try something like this (UNTESTED)
1
2
3
4
5
HWND hWnd = CreateWindow("className","title",flags,x,y,width,height,NULL,NULL,hInstance,NULL);
HWND handles[10];
for(int i = 0; i < 10; ++i){
    handles[i] = CreateWindow("button",race[i].c_str(),flags,(80*i+10),10,80,25,hWnd,NULL,hInstance,NULL);
}


Also, i see how i dont get an error for the Array now, but what does .c_str() actually do?
It returns the char array (cstring) of the string object.
ah i see.. so i put in that code, and i put the first 2 lines you had there, and i get a bunch of errors for undeclared variables... is there a specific type i should put them as, int maybe?
Sorry, I didn't specify, you need to change x,y, width, and height to your own values. x and y are the start location (top left corner) of window. hInstance is the HINSTANCE passed to WinMain INT WINAPI WinMain(HINSTANCE Instance, HINSTANCE, LPSTR, INT nCmdShow) so you will probably need to change that as well.
Last edited on
but thats the thing.. i dont know what to put there.. by the time it comes to that point in the code, its not in the top left, its like mid screen maybe... so dont know what coord sto start with
its hard to understand what your talking about, i am very amateur when it comes to c++, i took only one intro level class, but i enjoy it a lot, and i was one of the better people in the class.. its just ive been out of practice since then and i forgot a lot of stuff like classes and inheritance and stuff.. like i said, im sure i can input all of this into this little game im making, so i thought it would be goo dpractice .. do you want me to post the code i have up to that part so you can show me where and what i actually need to change?
Yes, please post the code in code tags.
Here you go, sorry it took so long, had to get to bed, and ill be gone for work for a while now

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM,LPARAM);
using namespace std;
    
    //FOR RACE CHOICE
int a = 1,
    b = 0,
    
    //FOR WEAPON CHOICE
    c = 1,
    d = 0;

string name,
       gender2;
       
       
       //ARRAYS
string RACE[10] = {"Breton", "Imperial", "Nord", "Redguard", "Altmer", "Bosmer", "Dunmer", "Orc", "Argonian", "Khajiit" },
       WEAPON[6] = {"Dagger", "Sword", "Spear", "Axe", "Club", "Bow"};

char gender,
     choice;


int main(void){
    
    
    cout<<"Welcome to Corey and Rachel's Test Game :]" << endl;
    cout<<endl;    
    
    //BEGINNING OF PLAYERS NAME CHOICE
    cout<<"Please input your characters name..." << endl;
    cout<<endl;
    
    cin>>name;
    cout<<endl;
    
    cout<<"Is " << name << " the correct name you wanted?\n Enter y (for yes) or n (for no)..." << endl;
    cout<<endl;
    
    cin>>choice;
    cout<<endl;
    
   while(choice !='n' && choice !='y'){
                
                cout<<"Sorry, please use lower case y or n" <<endl;
                cout<<endl;
                
                cin>>choice;
                cout<<endl;
   }
                
                
                
   while(choice=='n' ){
                    
                    cout<<"Please input your characters name..." << endl;
                    cout<<endl;
                   
                    cin>>name;
                    cout<<endl;
                    
                    cout<<"Is " << name << " the correct name you wanted? Please enter y or n" << endl;
                    cout<<endl;
                    
                    cin>>choice;
    }
    
    
    
    //BEGINNING OF PLAYER'S CHARACTER'S GENDER CHOICE
    cout<<"What is your gender? m (for male) f (for female)\n\n";
    cin>>gender;
    
    while(gender !='m' && gender !='f'){
              cout<<"Sorry, please use lower case letters\n\n";
              cin>>gender;
    }
    
    cout<<endl;
    
    if(gender=='m'){
                    gender2="male";
    }
    
    if(gender=='f'){
                    gender2="female";
    }
    
    
    //BEGINNING OF PLAYERS CHARACTERS RACE CHOICE   
    cout<<"Please choose your race by entering the correspondoing number\n\n";
    
   
   //FOR loop to display classes with buttons to choose which one you want         
   for(int i = 0; i < 10; ++i){ 
           
           
           HWND hWnd = CreateWindow("className","title",WS_VISIBLE | WS_CHILD, (80*i+10), 10, 80, 25,NULL,NULL,HINSTANCE,NULL);
           HWND handles[10];
           
           handles[i] = CreateWindow("button",RACE[i].c_str(),WS_VISIBLE | WS_CHILD,(80*i+10),10,80,25,hWnd,NULL,hInstance,NULL);
    
           a++;
    }
            
            

    
    cout<<endl;
    cin>>b;
    
    while( b<1 || b>10 ){
              
              cout<<"\nSorry, you need to enter 1 - 10 for the corresponding class\n\n";
              cin>>b;
              
    }



and excuse the classes in the RACE array, i used the ones of Skyrim just as a quick option, like i said just practice, not to distribute or anything
Please read a good book / online tutorial about win32 graphical interface programming and stop watching youtube video crap.

Also, do not use dev-cpp IDE these days, it is a piece of crap, use Code::Blocks or Visual Studio Express (both are free of charge). Create a new win32 project with either of them and you get a fully working program which you can then customize further to your needs.
what about Netbeans as an IDE? one of my good friends who is great with Java uses netbeans and supposedly you can do c++ and other languages in it also? and the only reason i used dev-c++ is because thats the program i first learned c++ in and didnt know about/how to use other programs. Thanks for the info, and do you maybe have a link for a good tutorial for c++? maybe even java also, because that friend i mentioned earlier along with one other are making a game that's in java , and hes the only programmer and he cant work on the game all the time so i would like to be able to help with that but as of now me and the other guy are doing everything else. We even have a website, but the webserver is down atm because we moved the server and havent set it back up yet...
Topic archived. No new replies allowed.