'int WinMain' 重新声明为不同种类的符号
'int WinMain' redeclared as different kind of symbol
我在使用 CODEBLOCKS (WinApi) 和 WINDOWS SDK 在 CPP 中执行代码时遇到问题。
我的代码:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <string>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <math.h>
#include <windowsx.h>
#define IDT_TIMER1 151L
#define NUM 1000
#define TWOPI (2*3.14159)
static TCHAR szWindowClass[] = _T("DesktopApp");
static TCHAR szTitle[] = _T("Curbe");
POINT apt[4];
POINT apt1[4];// definim variabilele globale
RECT r;
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void InitBezierParams(); // Initializam punctele pentru Bezier
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex)) {
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
hInst = hInstance;
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW, 500, 500, 700, 500,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd) {
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
InitBezierParams(); // Chemam functia care initializeaza pentru prima data punctele curbei Bezier
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
void DrawBezier(HDC hdc, POINT apt[]) {
// Draw Bessel function call system
PolyBezier(hdc, apt, 4);
MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
LineTo(hdc, apt[1].x, apt[1].y);
MoveToEx(hdc, apt[2].x, apt[2].y, NULL);
LineTo(hdc, apt[3].x, apt[3].y);
}
// functia care initializeaza punctele
void InitBezierParams() {
apt1[0].x = 220; apt1[0].y = 210;
apt1[1].x = 260; apt1[1].y = 20;
apt1[2].x = 360; apt1[2].y = 290;
apt1[3].x = 420; apt1[3].y = 210;
}
POINT rotate_point(float cx, float cy, float angle, POINT p) {
float s = sin(angle);
float c = cos(angle);
// translate point back to origin:
p.x -= cx;
p.y -= cy;
// rotate point
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;
// translate point back:
p.x = xnew + cx;
p.y = ynew + cy;
return p;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
int i, j;
static int cxClient, cyClient;
static POINT apt[4];
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Curba Besier prin functii GDI");
TCHAR greeting1[] = _T("Curba Besier prin functii matematice");
switch (message) {
case WM_SIZE: {
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
//re the starting points, a first control point, the second control point.End
apt[0].x = cxClient / 4;
apt[0].y = cyClient / 2;
apt[1].x = cxClient / 2;
apt[1].y = cyClient / 4;
apt[2].x = cxClient / 2;
apt[2].y = 3 * cyClient / 4;
apt[3].x = 3 * cxClient / 4;
apt[3].y = cyClient / 2;
return 0;
}
case WM_LBUTTONDOWN: {
case WM_RBUTTONUP: {
case WM_MOUSEMOVE: {
// Left or right can draw
if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
{
hdc = GetDC(hWnd);
// draw from Bayesian curve defined function
SelectObject(hdc, GetStockObject(WHITE_PEN));
// Desenarea curbei prin funtie GDI
DrawBezier(hdc, apt);
// Left first control point
if (wParam & MK_LBUTTON){
apt[1].x = LOWORD(lParam);
apt[1].y = HIWORD(lParam);
}
// Right control of the second control point
if (wParam & MK_RBUTTON){
apt[2].x = LOWORD(lParam);
apt[2].y = HIWORD(lParam);
}
if (wParam & MK_RBUTTON)
{
apt[2].x = LOWORD(lParam);
apt[2].y = HIWORD(lParam);
}
// replaced with custom brushes
SelectObject(hdc, CreatePen(PS_DOT, 0, RGB(255, 0, 0)));
// draw the curve
DrawBezier(hdc, apt);
ReleaseDC(hWnd, hdc);
}
return 0;
}}}
case WM_KEYDOWN: {
//deplasarea punctului de control
if (GetAsyncKeyState(VK_CONTROL))
{
switch (wParam)
{
case VK_LEFT:
apt1[2].x -= 10;
break;
case VK_RIGHT:
apt1[2].x += 6;
break;
case VK_UP:
apt1[2].y -= 10;
break;
case VK_DOWN:
apt1[2].y += 6;
break;
}
}
else
{
if (GetAsyncKeyState(VK_SPACE))
{
switch (wParam)
{
case VK_LEFT:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].x--;
}
}
break;
case VK_RIGHT:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].x++;
}
}
break;
case VK_UP:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].y--;
}
}
break;
case VK_DOWN:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].y++;
}
}
break;
}
}
else
{
if (GetAsyncKeyState(VK_SHIFT)) {
switch (wParam)
{
case VK_LEFT:
apt1[0] = rotate_point(300, 300, -100, apt1[0]);
apt1[1] = rotate_point(300, 300, -100, apt1[1]);
apt1[2] = rotate_point(300, 300, -100, apt1[2]);
apt1[3] = rotate_point(300, 300, -100, apt1[3]);
break;
case VK_RIGHT:
apt1[0] = rotate_point(300, 300, 100, apt1[0]);
apt1[1] = rotate_point(300, 300, 100, apt1[1]);
apt1[2] = rotate_point(300, 300, 100, apt1[2]);
apt1[3] = rotate_point(300, 300, 100, apt1[3]);
break;
}
}
else {
switch (wParam)
{
case VK_LEFT:
apt1[1].x -= 10;
break;
case VK_RIGHT:
apt1[1].x += 6;
break;
case VK_UP:
apt1[1].y -= 10;
break;
case VK_DOWN:
apt1[1].y += 6;
break;
case VK_TAB:
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
apt1[1].x = p.x;
apt1[1].y = p.y;
}
break;
}
}
}
}
InvalidateRect(hWnd, NULL, TRUE);
break;
}
case WM_PAINT: {
hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, RGB(50, 70, 70));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 10, 10, greeting1, _tcslen(greeting1));
PolyBezier(hdc, apt1, 4);
SetTextColor(hdc, RGB(50, 70, 70));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 300, 10, greeting, _tcslen(greeting));
{//desenare curba bezier dupa formula
int x[4] = { 20, 60, 160, 220 };
int y[4] = { 110, 20, 190, 110 };
double xu, yu;
for (double u = 0.0; u <= 1.0; u += 0.0001)
{
xu = (pow(1 - u, 3) * x[0] + 3 * u * pow(1 - u, 2) * x[1] + 3 * pow(u, 2) * (1 - u) * x[2] + pow(u, 3) * x[3]);
yu = pow(1 - u, 3) * y[0] + 3 * u * pow(1 - u, 2) * y[1] + 3 * pow(u, 2) * (1 - u) * y[2] + pow(u, 3) * y[3];
SetPixel(hdc, (int)xu, (int)yu, RGB(125, 120, 235));
}
}
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default: {
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
}
我得到的错误:
||=== Build: Debug in Lab3 (compiler: GNU GCC Compiler) ===|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|warning: '__stdcall__' attribute only applies to function types [-Wattributes]|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: 'int WinMain' redeclared as different kind of symbol|
c:\mingw\include\winbase.h|1263|note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)'|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_opt_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|23|warning: 'szTitle' defined but not used [-Wunused-variable]|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|22|warning: 'szWindowClass' defined but not used [-Wunused-variable]|
||=== Build failed: 5 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
我试图在 Whosebug 上查找任何信息和修复程序,但我什至没有得到任何东西。
请帮忙...
代码含义:
执行后:https://prnt.sc/10v4p74
Exp: 我想制作 3 个 bezeir curbes,其中一个应该用键盘操作
- 启动代码块。
- 文件 -> 新建 -> 项目
- Win32 GUI 项目。 不要 选择控制台应用程序!
- 基于选择框(可能无关紧要)
- 选择名称和路径。选择 gcc 作为编译器。
- 将您的代码按原样复制到 main.cpp。
- 构建、编译 & 运行.
出现神秘错误消息的原因是,如果您选择了错误类型的项目,例如一个不是明确 Win32 API 的项目,那么编译器库可能在其他地方有一个 WinMain内部启动代码。所以你得到一个重复的函数冲突 - WinMain 重新声明。
我在使用 CODEBLOCKS (WinApi) 和 WINDOWS SDK 在 CPP 中执行代码时遇到问题。 我的代码:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <string>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <math.h>
#include <windowsx.h>
#define IDT_TIMER1 151L
#define NUM 1000
#define TWOPI (2*3.14159)
static TCHAR szWindowClass[] = _T("DesktopApp");
static TCHAR szTitle[] = _T("Curbe");
POINT apt[4];
POINT apt1[4];// definim variabilele globale
RECT r;
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void InitBezierParams(); // Initializam punctele pentru Bezier
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex)) {
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
hInst = hInstance;
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW, 500, 500, 700, 500,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd) {
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
InitBezierParams(); // Chemam functia care initializeaza pentru prima data punctele curbei Bezier
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
void DrawBezier(HDC hdc, POINT apt[]) {
// Draw Bessel function call system
PolyBezier(hdc, apt, 4);
MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
LineTo(hdc, apt[1].x, apt[1].y);
MoveToEx(hdc, apt[2].x, apt[2].y, NULL);
LineTo(hdc, apt[3].x, apt[3].y);
}
// functia care initializeaza punctele
void InitBezierParams() {
apt1[0].x = 220; apt1[0].y = 210;
apt1[1].x = 260; apt1[1].y = 20;
apt1[2].x = 360; apt1[2].y = 290;
apt1[3].x = 420; apt1[3].y = 210;
}
POINT rotate_point(float cx, float cy, float angle, POINT p) {
float s = sin(angle);
float c = cos(angle);
// translate point back to origin:
p.x -= cx;
p.y -= cy;
// rotate point
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;
// translate point back:
p.x = xnew + cx;
p.y = ynew + cy;
return p;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
int i, j;
static int cxClient, cyClient;
static POINT apt[4];
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Curba Besier prin functii GDI");
TCHAR greeting1[] = _T("Curba Besier prin functii matematice");
switch (message) {
case WM_SIZE: {
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
//re the starting points, a first control point, the second control point.End
apt[0].x = cxClient / 4;
apt[0].y = cyClient / 2;
apt[1].x = cxClient / 2;
apt[1].y = cyClient / 4;
apt[2].x = cxClient / 2;
apt[2].y = 3 * cyClient / 4;
apt[3].x = 3 * cxClient / 4;
apt[3].y = cyClient / 2;
return 0;
}
case WM_LBUTTONDOWN: {
case WM_RBUTTONUP: {
case WM_MOUSEMOVE: {
// Left or right can draw
if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
{
hdc = GetDC(hWnd);
// draw from Bayesian curve defined function
SelectObject(hdc, GetStockObject(WHITE_PEN));
// Desenarea curbei prin funtie GDI
DrawBezier(hdc, apt);
// Left first control point
if (wParam & MK_LBUTTON){
apt[1].x = LOWORD(lParam);
apt[1].y = HIWORD(lParam);
}
// Right control of the second control point
if (wParam & MK_RBUTTON){
apt[2].x = LOWORD(lParam);
apt[2].y = HIWORD(lParam);
}
if (wParam & MK_RBUTTON)
{
apt[2].x = LOWORD(lParam);
apt[2].y = HIWORD(lParam);
}
// replaced with custom brushes
SelectObject(hdc, CreatePen(PS_DOT, 0, RGB(255, 0, 0)));
// draw the curve
DrawBezier(hdc, apt);
ReleaseDC(hWnd, hdc);
}
return 0;
}}}
case WM_KEYDOWN: {
//deplasarea punctului de control
if (GetAsyncKeyState(VK_CONTROL))
{
switch (wParam)
{
case VK_LEFT:
apt1[2].x -= 10;
break;
case VK_RIGHT:
apt1[2].x += 6;
break;
case VK_UP:
apt1[2].y -= 10;
break;
case VK_DOWN:
apt1[2].y += 6;
break;
}
}
else
{
if (GetAsyncKeyState(VK_SPACE))
{
switch (wParam)
{
case VK_LEFT:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].x--;
}
}
break;
case VK_RIGHT:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].x++;
}
}
break;
case VK_UP:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].y--;
}
}
break;
case VK_DOWN:
{
for (size_t i = 0; i < 4; i++)
{
apt1[i].y++;
}
}
break;
}
}
else
{
if (GetAsyncKeyState(VK_SHIFT)) {
switch (wParam)
{
case VK_LEFT:
apt1[0] = rotate_point(300, 300, -100, apt1[0]);
apt1[1] = rotate_point(300, 300, -100, apt1[1]);
apt1[2] = rotate_point(300, 300, -100, apt1[2]);
apt1[3] = rotate_point(300, 300, -100, apt1[3]);
break;
case VK_RIGHT:
apt1[0] = rotate_point(300, 300, 100, apt1[0]);
apt1[1] = rotate_point(300, 300, 100, apt1[1]);
apt1[2] = rotate_point(300, 300, 100, apt1[2]);
apt1[3] = rotate_point(300, 300, 100, apt1[3]);
break;
}
}
else {
switch (wParam)
{
case VK_LEFT:
apt1[1].x -= 10;
break;
case VK_RIGHT:
apt1[1].x += 6;
break;
case VK_UP:
apt1[1].y -= 10;
break;
case VK_DOWN:
apt1[1].y += 6;
break;
case VK_TAB:
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
apt1[1].x = p.x;
apt1[1].y = p.y;
}
break;
}
}
}
}
InvalidateRect(hWnd, NULL, TRUE);
break;
}
case WM_PAINT: {
hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, RGB(50, 70, 70));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 10, 10, greeting1, _tcslen(greeting1));
PolyBezier(hdc, apt1, 4);
SetTextColor(hdc, RGB(50, 70, 70));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 300, 10, greeting, _tcslen(greeting));
{//desenare curba bezier dupa formula
int x[4] = { 20, 60, 160, 220 };
int y[4] = { 110, 20, 190, 110 };
double xu, yu;
for (double u = 0.0; u <= 1.0; u += 0.0001)
{
xu = (pow(1 - u, 3) * x[0] + 3 * u * pow(1 - u, 2) * x[1] + 3 * pow(u, 2) * (1 - u) * x[2] + pow(u, 3) * x[3]);
yu = pow(1 - u, 3) * y[0] + 3 * u * pow(1 - u, 2) * y[1] + 3 * pow(u, 2) * (1 - u) * y[2] + pow(u, 3) * y[3];
SetPixel(hdc, (int)xu, (int)yu, RGB(125, 120, 235));
}
}
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default: {
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
}
我得到的错误:
||=== Build: Debug in Lab3 (compiler: GNU GCC Compiler) ===|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|warning: '__stdcall__' attribute only applies to function types [-Wattributes]|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: 'int WinMain' redeclared as different kind of symbol|
c:\mingw\include\winbase.h|1263|note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)'|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_opt_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|32|error: '_In_' was not declared in this scope|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|23|warning: 'szTitle' defined but not used [-Wunused-variable]|
C:\Users\Lil-Dredd\Desktop\Lab_PW\Lab3\main.cpp|22|warning: 'szWindowClass' defined but not used [-Wunused-variable]|
||=== Build failed: 5 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
我试图在 Whosebug 上查找任何信息和修复程序,但我什至没有得到任何东西。 请帮忙...
代码含义: 执行后:https://prnt.sc/10v4p74 Exp: 我想制作 3 个 bezeir curbes,其中一个应该用键盘操作
- 启动代码块。
- 文件 -> 新建 -> 项目
- Win32 GUI 项目。 不要 选择控制台应用程序!
- 基于选择框(可能无关紧要)
- 选择名称和路径。选择 gcc 作为编译器。
- 将您的代码按原样复制到 main.cpp。
- 构建、编译 & 运行.
出现神秘错误消息的原因是,如果您选择了错误类型的项目,例如一个不是明确 Win32 API 的项目,那么编译器库可能在其他地方有一个 WinMain内部启动代码。所以你得到一个重复的函数冲突 - WinMain 重新声明。