C++ GUI button stuck to the bottom - c++

I'm making a C++ GUI form in windows.h for my mother and I can't figure out how to make the "adaugare" , "iesire", "refresh" etc. buttons to stick to the bottom side of windows form. I want to make them to auto-move when I resize the form, so they can always be at bottom-right side.
Please help me guys because I am a beginner in C++ Gui !
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
ofstream fout;
ifstream fin;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
HWND hwnd; /* This is the handle for our window */
HWND button[5];
HWND banda[100];
HWND tip[100];
HWND lungime[100];
HWND latime[100];
HWND data1[100];
HWND button_valideaza[100];
int button_index[100];
int i = 0, counter = 1;
char buffer[10];
char textEP[20];
char textTip[20];
char textLungime[20];
char textLatime[20];
char textData[20];
char textAll[101];
/* Make the class name into a global variable */
TCHAR szClassName[] = _T("CodeBlocksWindowsApp");
int getIndex(){
fin.open("record.txt", std::ios_base::in);
string line1;
string line;
while(getline(fin, line1)) {
line = line1;
}
char save[line.size()];
strcpy(save, line.c_str());
char *p = strtok(save, " ");
fin.close();
return atoi(p);
}
void refresh() {
}
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof(WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx(&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Flux Artego"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
700, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow(hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage(&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
void scrie() {
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WORD cmd = LOWORD(wParam);
switch (message) /* handle the messages */
{
case WM_CREATE: // fac butoane, label etc
counter = getIndex();
counter++;
fout.open("record.txt", std::ios_base::app);
std::fill_n(button_index, 100, -1);
button[0] = CreateWindow("BUTTON",
"Iesire",
WS_VISIBLE | WS_CHILD | WS_BORDER,
560, 220, 100, 20,
hwnd, (HMENU)1, NULL, NULL); // (HMENU) 1 reprezinta care case din switch se executa
button[1] = CreateWindow("BUTTON",
"Adauga",
WS_VISIBLE | WS_CHILD | WS_BORDER,
450, 220, 100, 20,
hwnd, (HMENU)2, NULL, NULL);
button[2] = CreateWindow("BUTTON",
"Refresh",
WS_VISIBLE | WS_CHILD | WS_BORDER,
340, 220, 100, 20,
hwnd, (HMENU)3, NULL, NULL);
break;
case WM_COMMAND: // fac instructiuni butoane
switch (cmd)
{
case 1:
//::MessageBeep(MB_ICONERROR);
//::MessageBox(hwnd, "Ai salvat ?", "atentie", MB_OKCANCEL);
cout << "GoodBye!";
PostQuitMessage(0);
break;
case 2: // Adaug nou record
banda[i] = CreateWindow("EDIT",
"EP",
WS_BORDER | WS_CHILD | WS_VISIBLE,
20, 30 * i, 30, 25,
hwnd, NULL, NULL, NULL);
tip[i] = CreateWindow("EDIT",
"100",
WS_BORDER | WS_CHILD | WS_VISIBLE,
55, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
lungime[i] = CreateWindow("EDIT",
"Lungime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
160, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
latime[i] = CreateWindow("EDIT",
"Latime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
265, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
data1[i] = CreateWindow("EDIT",
"Data",
WS_BORDER | WS_CHILD | WS_VISIBLE,
370, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
button_valideaza[i] = CreateWindow("BUTTON",
"Scrie",
WS_VISIBLE | WS_CHILD | WS_BORDER,
475, 30 * i, 80, 20,
hwnd, (HMENU)(i+5), NULL, NULL);
i++;
break;
case 3: // Refresh
refresh();
break;
case 4: // Compute
break;
default:
if (cmd > 4 && cmd < 103)
{
int index;
if (button_index[cmd-3] == -1){ // daca buton index nu a fost setata
button_index[cmd-3] = counter;
index = counter;
//cout << "Prima apasare a butinului " << cmd-3 << " si primeste index "<<counter<<endl;
counter++;
} else { // a fost setat
index = button_index[cmd-3];
//cout << "Deja apasat butonul "<< cmd-3 << " si are vechiul index " << button_index[cmd-3] << endl;
::MessageBox(hwnd, "Pentru a rescrie apasa pe REFRESH","Suprascriere", MB_OK);
}
int gwtstat = 0;
gwtstat = GetWindowText(banda[cmd-5], &textEP[0], 20);
gwtstat = 0;
gwtstat = GetWindowText(tip[cmd-5], &textTip[0], 20);
gwtstat = 0;
gwtstat = GetWindowText(lungime[cmd-5], &textLungime[0], 20);
gwtstat = 0;
gwtstat = GetWindowText(latime[cmd-5], &textLatime[0], 20);
gwtstat = 0;
gwtstat = GetWindowText(data1[cmd-5], &textData[0], 20);
itoa(index, buffer, 10);
strcpy(textAll, buffer);
strcat(textAll," ");
strcat(textAll,textEP);
strcat(textAll, " ");
strcat(textAll,textTip);
strcat(textAll, " ");
strcat(textAll, textLungime);
strcat(textAll, " ");
strcat(textAll, textLatime);
strcat(textAll, " ");
strcat(textAll, textData);
fout << textAll << "\n";
::MessageBox(hwnd, textAll,"text", MB_OKCANCEL);
}
break;
}
break;
case WM_DESTROY:
fout.close();
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}

Unfortunately, there is no mechanism for doing this automatically. You have to write code that listens for sizing events, calculates the appropriate size and position of each control, and moves it accordingly. It is relatively straightforward, once you understand the process, but a fair amount of drudgery, especially when you have a large number of controls to adjust.
Start by adding a handler for the WM_SIZE message to your main window's window procedure. This message gets sent when the window is being resized.
Inside of that handler, you will call either the MoveWindow or SetWindowPos function to change the position of your child windows (i.e., your controls; i.e., your buttons).
Here is some sample code:
case WM_SIZE:
{
const int cxPadding = 12;
const int cyPadding = 12;
// Get client rectangle for parent window.
RECT rcParent;
GetClientRect(hwnd, &rcParent);
RECT rcButton[5];
// Adjust the position of the first button.
// It will be the furthest one to the right.
GetClientRect(button[0], &rcButton[0]);
SetWindowPos(button[0],
NULL,
(rcParent.right - cxPadding - (rcButton[0].right - rcButton[0].left)),
(rcParent.bottom - cyPadding - (rcButton[0].bottom - rcButton[0].top)),
0,
0,
SWP_NOACTIVATE | SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOZORDER);
// Adjust the position of the second button.
// It will be the next to the first one.
GetClientRect(button[1], &rcButton[1]);
SetWindowPos(button[1],
NULL,
(rcParent.right - cxPadding - (rcButton[0].right - rcButton[0].left)
- cxPadding - (rcButton[1].right - rcButton[1].left)),
(rcParent.bottom - cyPadding - (rcButton[1].bottom - rcButton[1].top)),
0,
0,
SWP_NOACTIVATE | SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOZORDER);
// etc.
}
To reduce flicker when you are repositioning a large number of child controls, you can use the DeferWindowPos function. This requires that you call BeginDeferWindowPos at the beginning, then call DeferWindowPos (just like SetWindowPos) for each control, and then finally call EndDeferWindowPos when you are finished adjusting all controls. See What's the point of DeferWindowPos? on Raymond Chen's blog.
Also, if you are trying to learn Windows API programming, do yourself a huge favor and pick up a copy of Charles Petzold's classic book, Programming Windows (5th edition). Yes, you need the 5th edition. The newer editions cover a different programming language/framework. You can find used copies on Amazon.

Related

Window.h c++ how to get index of a button from an array inside switch

I have one question for you. I just started to learn C++ GUI (windows.h) and I got a problem. I have to do for my mother a program that record some information so I designed a GuI that add a new set of empty field every time I click on "Adaugare" button . Near every set of fields, on right, there is a button named "Scrie" that should get the information from textBoxes and store it into a string. The problem is when I press the button to get the info, I don t know how to make my switch to figure out what field I want to get . Example : I writed 5 records, but I want to modify the first one, so how do I get the index of it even thought I have another 5 set of fields after ?
PLEASE HELP ME GUYS !
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <iostream>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND hwnd; /* This is the handle for our window */
HWND button[5];
HWND banda[100];
HWND tip[100];
HWND lungime[100];
HWND latime[100];
HWND data[100];
HWND button_valideaza[100];
int i = 1;
char textSaved[20];
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Flux Artego"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
700, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
void scrie() {
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE: // fac butoane, label etc
button[0] = CreateWindow("BUTTON",
"Iesire",
WS_VISIBLE | WS_CHILD | WS_BORDER,
560, 220, 100, 20,
hwnd, (HMENU) 1, NULL, NULL); // (HMENU) 1 reprezinta care case din switch se executa
button[1] = CreateWindow("BUTTON",
"Adauga",
WS_VISIBLE | WS_CHILD | WS_BORDER,
450, 220, 100, 20,
hwnd, (HMENU) 2, NULL, NULL);
break;
case WM_COMMAND: // fac instructiuni butoane
switch(LOWORD(wParam) )
{
case 1:
//::MessageBeep(MB_ICONERROR);
//::MessageBox(hwnd, "Ai salvat ?", "atentie", MB_OKCANCEL);
cout << "GoodBye!";
PostQuitMessage (0);
break;
case 2: // Adaug nou record
banda[i] = CreateWindow("EDIT",
"EP",
WS_BORDER | WS_CHILD | WS_VISIBLE,
20, 30 * i, 30, 25,
hwnd, NULL, NULL, NULL);
tip[i] = CreateWindow("EDIT",
"100",
WS_BORDER | WS_CHILD | WS_VISIBLE,
55, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
lungime[i] = CreateWindow("EDIT",
"Lungime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
160, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
latime[i] = CreateWindow("EDIT",
"Latime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
265, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
data[i] = CreateWindow("EDIT",
"Data",
WS_BORDER | WS_CHILD | WS_VISIBLE,
370, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
button_valideaza[i] = CreateWindow("BUTTON",
"Scrie",
WS_VISIBLE | WS_CHILD | WS_BORDER,
475, 30 * i, 80, 20,
hwnd, NULL, NULL, NULL);
i++;
break;
case 3: // scriu record
int gwtstat = 0;
gwtstat = GetWindowText(banda[i], &textSaved[0], 20);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Use the different command for each "Scrie" Button , set its Menu like that:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <iostream>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
HWND hwnd; /* This is the handle for our window */
HWND button[5];
HWND banda[100];
HWND tip[100];
HWND lungime[100];
HWND latime[100];
HWND data1[100];
HWND button_valideaza[100];
int i = 0;
char textSaved[20];
/* Make the class name into a global variable */
TCHAR szClassName[] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof(WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx(&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Flux Artego"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
700, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow(hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage(&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
void scrie() {
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WORD cmd = LOWORD(wParam);
switch (message) /* handle the messages */
{
case WM_CREATE: // fac butoane, label etc
button[0] = CreateWindow("BUTTON",
"Iesire",
WS_VISIBLE | WS_CHILD | WS_BORDER,
560, 220, 100, 20,
hwnd, (HMENU)1, NULL, NULL); // (HMENU) 1 reprezinta care case din switch se executa
button[1] = CreateWindow("BUTTON",
"Adauga",
WS_VISIBLE | WS_CHILD | WS_BORDER,
450, 220, 100, 20,
hwnd, (HMENU)2, NULL, NULL);
break;
case WM_COMMAND: // fac instructiuni butoane
switch (cmd)
{
case 1:
//::MessageBeep(MB_ICONERROR);
//::MessageBox(hwnd, "Ai salvat ?", "atentie", MB_OKCANCEL);
cout << "GoodBye!";
PostQuitMessage(0);
break;
case 2: // Adaug nou record
banda[i] = CreateWindow("EDIT",
"EP",
WS_BORDER | WS_CHILD | WS_VISIBLE,
20, 30 * i, 30, 25,
hwnd, NULL, NULL, NULL);
tip[i] = CreateWindow("EDIT",
"100",
WS_BORDER | WS_CHILD | WS_VISIBLE,
55, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
lungime[i] = CreateWindow("EDIT",
"Lungime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
160, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
latime[i] = CreateWindow("EDIT",
"Latime",
WS_BORDER | WS_CHILD | WS_VISIBLE,
265, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
data1[i] = CreateWindow("EDIT",
"Data",
WS_BORDER | WS_CHILD | WS_VISIBLE,
370, 30 * i, 100, 25,
hwnd, NULL, NULL, NULL);
button_valideaza[i] = CreateWindow("BUTTON",
"Scrie",
WS_VISIBLE | WS_CHILD | WS_BORDER,
475, 30 * i, 80, 20,
hwnd, (HMENU)(i+3), NULL, NULL);
i++;
break;
default:
if (cmd > 2 && cmd < 103)
{
int gwtstat = 0;
gwtstat = GetWindowText(banda[cmd-3], &textSaved[0], 20);
::MessageBox(hwnd, textSaved,"text", MB_OKCANCEL);
}
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}

Windows.h input [duplicate]

This question already has answers here:
How to parse a string to an int in C++?
(17 answers)
Closed 7 years ago.
For the last 2 weeks i`ve been trying to make a program using windows.h library (for the simple use of GUI ). I managed to make a textbox that you can write in , but my problem is that you can only introduce data as char* . The programm is ment for my workers to be able to calculate some formulas easier & faster. How can i transform the char to an int , without having trouble with ASCII codes?
*I have also tried using strings. This is my actual code :
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <string>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND textstatic,texteditz1,texteditz2,textedit2,buttoncalculate,button2;
/* Make the class name into a global variable */
string inputZ1,inputZ2;
char *cinputZ1 = &inputZ1[0u];
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
500, /* The programs width */
500, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
texteditz1 = CreateWindow ("EDIT","",
WS_VISIBLE | WS_CHILD,
60,50,45,14,
hwnd, (HMENU) 4,NULL,NULL);
texteditz2 = CreateWindow ("EDIT","",
WS_VISIBLE | WS_CHILD,
60,30,45,14,
hwnd, (HMENU) 5 ,NULL , NULL);
textstatic = CreateWindow ("STATIC",
"Z2=",
WS_VISIBLE | WS_CHILD,
30,50,30,15,
hwnd, NULL, NULL, NULL);
textstatic = CreateWindow ("STATIC",
"Z1=",
WS_VISIBLE | WS_CHILD,
30,30,30,15,
hwnd, NULL, NULL, NULL);
textstatic = CreateWindow ("STATIC",
"x1min=",
WS_VISIBLE | WS_CHILD,
30,70,50,15,
hwnd, NULL, NULL, NULL);
buttoncalculate = CreateWindow ("BUTTON",
"Calculate",
WS_VISIBLE | WS_CHILD,
150, 300, 70, 15,
hwnd, (HMENU) 1, NULL, NULL);
button2 = CreateWindow ("BUTTON","apasa",
WS_VISIBLE | WS_CHILD,
300, 300, 70, 15,
hwnd, (HMENU) 2, NULL, NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 1:
{
int text=0 ,text2=0 ;
text=GetWindowText(texteditz1,inputZ1,5);
text2=GetWindowText(texteditz2,inputZ2,5);
::MessageBox(hwnd, inputZ1, "Button",MB_OK);
::MessageBox(hwnd, inputZ2, "Text2",MB_OK);
}
break;
case 2:
::MessageBox (hwnd, "251", "button",NULL);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
you can use atoi() get int from a char*,
and _wtoi() get int from a wchar_t*.
so uauslly use _ttoi(),it will replacesed atoi or _wtoi.
int a = _ttoi("123");
and you need add stdlib.h

Opening a file and writing to Text box c++

Okay, so I need this button that, when clicked, opens a user-specified file. After the file is opened, it needs to be read/copied into the text box below that button. Eventually other things need to happen but I'm having trouble just doing that. The following is the code I have so far for creating the GUI, then I'm completely lost on how to proceed.
#include <iostream>
#include <fstream>
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Software Engineering II", /* Title Text */
WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
900, /* The programs width */
500, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
#define ID_LABEL
static HWND hwndLbl1;
static HWND hwndLbl2;
static HWND hwndLbl3;
static HWND hwndLbl4;
bool buttonPressed = false;
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
hwndLbl1 = CreateWindow(TEXT("STATIC"),TEXT("Building File Selection"),
WS_VISIBLE | WS_CHILD,
10,10,150,25,
hwnd, (HMENU) 0, NULL, NULL
);
hwndLbl2 = CreateWindow(TEXT("STATIC"),TEXT("Class File Selection"),
WS_VISIBLE | WS_CHILD,
500,10,150,25,
hwnd, (HMENU) 0, NULL, NULL
);
hwndLbl3 = CreateWindow(TEXT("STATIC"),TEXT("Building No."),
WS_VISIBLE | WS_CHILD,
10, 75, 150, 25,
hwnd, (HMENU) 0, NULL, NULL
);
hwndLbl4 = CreateWindow(TEXT("STATIC"),TEXT("Room No."),
WS_VISIBLE | WS_CHILD,
500,75,150,25,
hwnd, (HMENU) 0, NULL, NULL
);
// Building Room open
CreateWindow(TEXT("button"),TEXT("Open"),
WS_VISIBLE | WS_CHILD,
10,40,80,25,
hwnd, (HMENU) 1, NULL, NULL
);
// Classroom open
CreateWindow(TEXT("button"),TEXT("Open"),
WS_VISIBLE | WS_CHILD,
500, 40, 80, 25,
hwnd, (HMENU) 2, NULL, NULL
);
CreateWindow(TEXT("button"),TEXT("Search"),
WS_VISIBLE | WS_CHILD,
800,100,80,25,
hwnd, (HMENU) 3, NULL, NULL
);
// The information should be read from here.
CreateWindow(TEXT("edit"), TEXT("Text goes here"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
10, 100, 300, 20,
hwnd, (HMENU) 5, NULL, NULL
);
CreateWindow(TEXT("edit"),TEXT("Text goes here"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
500,100,300,20,
hwnd, (HMENU) 5, NULL, NULL
);
CreateWindow(TEXT("edit"),TEXT("Output from Building"),
WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
10, 150, 400, 250,
hwnd, (HMENU) 6, NULL, NULL
);
CreateWindow(TEXT("edit"),TEXT("Output from Room file"),
WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
500, 150, 380, 250,
hwnd, (HMENU) 7, NULL, NULL
);
CreateWindow(TEXT("button"),TEXT("Continue"),
WS_VISIBLE | WS_CHILD,
500,410,80,25,
hwnd, (HMENU) 8, NULL, NULL
);
CreateWindow(TEXT("button"),TEXT("Show .PRN"),
WS_VISIBLE | WS_CHILD,
600,410,80,25,
hwnd, (HMENU) 9, NULL, NULL
);
CreateWindow(TEXT("button"),TEXT("Show .LOG"),
WS_VISIBLE | WS_CHILD,
700,410,80,25,
hwnd, (HMENU) 10, NULL, NULL
);
CreateWindow(TEXT("button"),TEXT("Show .CSV"),
WS_VISIBLE | WS_CHILD,
800,410,80,25,
hwnd, (HMENU) 11, NULL, NULL
);
break;
// This is where you issue commands for buttons
// To read from files and output them into those 2 text boxes that are ID'd (HMENU) 6 and (HMENU 7.
case WM_COMMAND:
if(LOWORD(wParam) == 1){
FILE * BuildingFile;
buttonPressed = true;
/* ifstream infile;
infile.open ("Bldgs-Rms-Txt-File-2012-08082012.txt");
if(infile.is_open()){
while(infile.good())
print to buildingtextbox (char) infile.get();
//BuildingFile = fopen("Bldgs-Rms-Txt-File-2012-08082012.txt","r"); // In the argument of MessageBox, this is the PARENT WINDOW, which should be the same for all
*/
}
if (LOWORD (wParam) == 2){
FILE * RoomFile;
RoomFile = fopen("cbm_005_003639_201310_08082012.DAT","r");
}
if (LOWORD (wParam) == 6){
std::cout << "chuck";
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
1) You need to preserve the window handles (hWnd)s from the CreateWindow calls.
// Up top
#include <string>
// Class member:
HWND building1text;
// Saving the hWnd on the CreateWindow call
building1text = CreateWindow(TEXT("edit"),TEXT("Output from Building"),
WS_CHILD | WS_VISIBLE | ES_READONLY | WS_BORDER,
10, 150, 400, 250,
hwnd, (HMENU) 6, NULL, NULL
);
2) Then you can use SetWindowText(hWnd, LPCTSTR) to put the text into the text box.
string lineString, totalString;
// open file here
while(infile.good()) {
// concatenate next line from file (works if file contains
// null-terminated lines of stuff
getline(infile, lineString, '\n');
totalString += lineString;
}
SetWindowText(building1text, totalString);
I don't have a compiler set up, but hopefully that works for you. Of course, I don't know how your data file is formatted, either.

Adding a progress bar and/or internal window to program. (How To?) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to take this code and add a progress bar to show the progress (BUUUUH), and also add a window inside the main one showing the names of the files being currently moved. I am totally lost right now on how to do this. I would love help if anyone can offer some.
enter code here
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "Back Up";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Back It Up", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
545, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Pictures"),
WS_VISIBLE | WS_CHILD ,
20, 50, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Music"),
WS_VISIBLE | WS_CHILD ,
120, 50, 80, 25,
hwnd, (HMENU) 2, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Videos"),
WS_VISIBLE | WS_CHILD ,
20, 100, 80, 25,
hwnd, (HMENU) 3, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Fun Times"),
WS_VISIBLE | WS_CHILD ,
120, 100, 80, 25,
hwnd, (HMENU) 4, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Programs"),
WS_VISIBLE | WS_CHILD ,
20, 150, 80, 25,
hwnd, (HMENU) 5, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("(WiP)"),
WS_VISIBLE | WS_CHILD ,
120, 150, 80, 25,
hwnd, (HMENU) 6, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Quit"),
WS_VISIBLE | WS_CHILD ,
20, 200, 180, 25,
hwnd, (HMENU) 7, NULL, NULL);
CreateWindow(TEXT("Static"), TEXT("Choose what you would like to back up from the options on the left."
" Remember this program is still in developmentso bugs and UI issues are to be expected."
" Future addons include Progress bar, activity terminal, and customizable locations."
" Also please do not disturb the program while it is running, it will not respond."),
WS_VISIBLE | WS_CHILD,
220, 50, 280, 150,
hwnd, (HMENU) NULL, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Back Up All"),
WS_VISIBLE | WS_CHILD,
20, 14, 180, 25,
hwnd, (HMENU) 8, NULL, NULL);
break;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == 1) {
system("robocopy C:/users/jacob/pictures E:/pictures /e /r:3 /w:0");
}
if (LOWORD(wParam) == 2) {
system("robocopy C:/users/jacob/music E:/music /e /r:3 /w:0");
}
if (LOWORD(wParam) == 3) {
system("robocopy C:/users/jacob/videos E:/videos /e /r:3 /w:0");
}
if (LOWORD(wParam) == 4) {
system("robocopy C:/Users/Jacob/Desktop/fun times e:/fun times /e /r:3 /w:0");
}
if (LOWORD(wParam) == 5) {
system("robocopy C:/Users/Jacob/Desktop/programs e:/programs /e /r:3 /w:0");
} /*
if (LOWORD(wParam) == 6) {
system("robocopy C:/Users/jacob/documents e:/documents /e /r:3 /w:0");
} */
if (LOWORD(wParam) == 7) {
PostQuitMessage(0);
}
if (LOWORD(wParam) == 8 ) {
system("robocopy C:/users/jacob/pictures E:/pictures /e /r:3 /w:0");
system("robocopy C:/users/jacob/music E:/music /e /r:3 /w:0");
system("robocopy C:/users/jacob/videos E:/videos /e /r:3 /w:0");
system("robocopy C:/Users/Jacob/Desktop/fun times e:/fun times /e /r:3 /w:0");
system("robocopy C:/Users/Jacob/Desktop/programs e:/programs /e /r:3 /w:0");
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
enter code here
add a progress bar to show the progress
Read up about the Progress Bar control.
You already know how to create a window and subwindows. Creating a ProcessBar is no different than creating a Button, or any other control, using CreateWindow/Ex().
add a window inside the main one showing the names of the files being currently moved.
That is more difficult. You are executing an external command to move the files, so you don't have direct access to its status information. You would have to replace system() with CreateProcess() so you can redirect its output:
Creating a Child Process with Redirected Input and Output
Once you have access to the output, you can parse it as needed and display it however you want, such as in a multi-line Edit control, a ListBox control, a ListView control, etc.
Alternatively, use FindFirstFile() and FindNextFile() in a recursive loop to iterate the source folder yourself, using CreateDirectory() and CopyFileEx() as needed. Then you will have direct access to each filename and progress information for each file. Then you do not need to use a redirected robocopy process anymore, and have more control over the iterating and copying.

C++ many errors when defining string

Hello i am learning C++ and i have errors while making VISUAL APP NOT CONSOLE
My problem is at the string Num1 = "0";
Please explain to me why this is happening?
The string is 0 because i want to make calculator so i need to make Num1 + "TheClicked number"
<script>
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "Andrey App";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Andrey App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
string Num1 = "0";
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Beep"),
WS_VISIBLE | WS_CHILD ,
20, 50, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("2"),
WS_VISIBLE | WS_CHILD ,
120, 50, 30, 30,
hwnd, (HMENU) 2, NULL, NULL);
CreateWindow(L"STATIC", Num1,
WS_CHILD | WS_VISIBLE | SS_LEFT,
20, 20, 300, 230,
hwnd, (HMENU) 1, NULL, NULL);
break;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == 1) {
Beep(40, 50);
}
if (LOWORD(wParam) == 2) {
if(Num1 == 0){
Num1 = 2;
}
else{
Num1 + "2";
}
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Here's how it should look:
//At the top of your .cpp file
#include<string>
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static std::string Num1 = "0"; /* static string created once and keeps it contents for each function call */
switch (message) /* handle the messages */
{
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Beep"),
WS_VISIBLE | WS_CHILD ,
20, 50, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("2"),
WS_VISIBLE | WS_CHILD ,
120, 50, 30, 30,
hwnd, (HMENU) 2, NULL, NULL);
///CreateWindow takes a LPCSTR as second arg, which is wchar_t under the covers, so need to convert
std::wstring wstr(Num1.begin(), Num1.end());
CreateWindow(L"STATIC", wstr.c_str(), //.c_str() returns a const char*
WS_CHILD | WS_VISIBLE | SS_LEFT,
20, 20, 300, 230,
hwnd, (HMENU) 1, NULL, NULL);
break;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == 1) {
Beep(40, 50);
}
if (LOWORD(wParam) == 2) {
if(Num1 == "0"){ //Num1 is a string, so you should check a string
Num1 = "2";
}
else{
Num1 += "2";
}
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}