【发布时间】:2016-07-18 23:28:55
【问题描述】:
我开始学习如何使用 Win32 Api 来构建一个 un c++ 程序,并且我已经完成了一个带有 CheckBox 的表单,当你按下它时,程序(应该)执行另一个用 other 编写的代码。 H 问题是,在另一个 cpp 中编写的这段代码有字符串、浮点数、unsigned int 和 char,当我将它们包含在编写表单并尝试构建程序的主 cpp 中时,它显示了超过 17 个错误(一个由变量)我一直在寻找答案,我在这个论坛上看到一个人告诉我添加这个
#define WIN32_LEAN_AND_MEAN
应该可以...我已经尝试过了,但一直在发生。
这里有代码:
#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);
}
这里有一张我的错误的照片:
(对不起语言,我一直在尝试将 VS 翻译成英文,但我不知道如何._。但如果需要,我可以翻译所有错误。)
大多数错误都说“已经定义...”,除了第一个错误,它说“找到一个或多个同时定义的符号”
__已编辑
.cpp 主要包含以下内容:
float CharToInt(char* value) {
stringstream str;
str << value;
float x;
str >> x;
return x;
}
在这里我测试了我的代码。我把我的函数放在主 cpp 中,仍然给我这个错误。
代码:
#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);
}
错误: IMG
1-“发现一个或多个同时定义的符号” 2-“已经定义……”
【问题讨论】:
-
一般不会
#include*.cpp 文件。 -
#include 一个 .cpp 文件是非常奇怪的。如果你坚持这样做,你能告诉我们它的内容吗?
-
我已经包含了一部分
标签: c++ api winapi compiler-errors