I'm starting to learn how to use the Win32 Api to build a program un c++ and I've done a form that have a CheckBox that when you press it, the program(should) execute another code written in other .h The problem is that this code written in another cpp have strings, floats, unsigned int and char and when I include them in the main cpp where the form is written and try to build the program, it show me more than 17 errors(one by variable) I have been looking for answers and I've seen in this forum a guy how told that adding this
#define WIN32_LEAN_AND_MEAN
should work... I've tried it but keep happening.
Here you have the code:
#include <windows.h>
//Here I add the include, #include "newcpp.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static char *title = TEXT("Check Box");
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wc = { 0 };
wc.lpszClassName = TEXT("Check Box");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName, title,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
150, 150, 230, 150, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Show Title"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
20, 20, 185, 35,
hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
CheckDlgButton(hwnd, 1, BST_CHECKED);
break;
}
case WM_COMMAND:
{
BOOL checked = IsDlgButtonChecked(hwnd, 1);
if (checked) {
CheckDlgButton(hwnd, 1, BST_UNCHECKED);
SetWindowText(hwnd, TEXT(""));
//Code Should be here like action();
}
else {
CheckDlgButton(hwnd, 1, BST_CHECKED);
SetWindowText(hwnd, title);
//and here like anotheraction();
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
And here you have a photo of my errors:
(Sorry about the language, I've been trying to translate VS to english but I dont know how ._. But I can translate all the errors if needed.)
Most of the errors say "already defined...." except the first one, that says "found one or more symbols defined simultaneously"
__Edited
The .cpp mostly have things like:
float CharToInt(char* value) {
stringstream str;
str << value;
float x;
str >> x;
return x;
}
Here I've a test of my code. I put my function inside the main cpp and still give me this error.
Code:
#include <windows.h>
#include <iostream>
#include <string>
#include <psapi.h> //yes, so much libs, but I'll need them
#include <sstream>
#pragma comment(lib, "psapi")
using namespace std;
float CharToInt(char* value) {
stringstream str;
str << value;
float x;
str >> x;
return x;
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static char *title = TEXT("Check Box");
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wc = { 0 };
wc.lpszClassName = TEXT("Check Box");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName, title,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
150, 150, 230, 150, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
CreateWindow(TEXT("button"), TEXT("Show Title"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
20, 20, 185, 35,
hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
CheckDlgButton(hwnd, 1, BST_CHECKED);
break;
}
case WM_COMMAND:
{
BOOL checked = IsDlgButtonChecked(hwnd, 1);
if (checked) {
CheckDlgButton(hwnd, 1, BST_UNCHECKED);
SetWindowText(hwnd, TEXT(""));
//Code Should be here like action();
}
else {
CheckDlgButton(hwnd, 1, BST_CHECKED);
SetWindowText(hwnd, title);
//and here like anotheraction();
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Error: IMG
1-"found one or more symbols defined simultaneously" 2-"already defined...."