0
所以我目前卡住試圖讓窗口API顯示(語言是C++),當我嘗試運行該程序時,我得到以下錯誤。 (雖然我現在得到錯誤顯示在紅色下劃線)。我目前使用Visual Studio社區作爲我的IDE。卡住讓Windows.h工作,直到編譯纔出錯。
「解析的外部符號_main函數參考‘INT _cdel調用(主要(無效))’
‘1周解決的外部’
我已經在網上查,並試圖它既是一個雙贏32控制檯程序和一個win 32項目(其中一些已列爲解決該程序的方法)。但是沒有結果。我不知道該錯誤是什麼(也可以參考我將以下教程作爲開始基礎https://www.youtube.com/watch?v=012pFrYE5_k,但我沒有使用開放的GL庫,因爲我正在嘗試完成的是製作一個非常簡化的win32窗口模板)任何想法?
代碼:main.h
#pragma once
#pragma once
#include<Windows.h>
#include<tchar.h>
#include<iostream>
#define WINDOW_WIDTH = 800 //remember to leave off the ; at the end of define sets(currently not being used
#define WINDOW_HEIGHT = 600
HWND hWnd;
代碼:main.cpp中
#include "main.h" //remember <> means library, "" means within your project.
// long results, CALL BACK, windows procedure, basically the basics of what to do. UINT (unsigned INT)
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
break;
case WM_DESTROY:
case WM_QUIT:
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
/*
remember that this is a run time instance, and is in essence the same as Main (char[args]) {}
returning 0 ends the operation of the win32 library.
*/
int WINAPI winMain(HINSTANCE hInstance, HINSTANCE hPrevious, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc; //default instance for the scope of the window (new window named WC for WINDOW CLASS)
MSG msg;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //default background color, set to stock light gray
wc.hCursor = LoadCursor(hInstance, IDC_ARROW); //default cursor
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = _T("NAME");
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW | CS_HREDRAW; //windows style in bit form, 0X00 hex if you were to print them
if (!RegisterClass(&wc)) //pointer the to the window, if it doesn't exist call this simple error handle
{
MessageBox(NULL, _T("error: cannot reg window class"), _T("error"), MB_OK);
return 0; // kills
}
hWnd = CreateWindow(L"NAME", //reference of the object already defined as wc
L"Window Title", //title
WS_OVERLAPPEDWINDOW, //basic window style
0, //x start,
0, //y start,
800,
600, //set all the dimensions to default value
NULL, //no parent window
NULL, //no menu
hInstance,
NULL);
if (!hWnd)
{
MessageBox(NULL, L"ERROR: cannot create window", L"ERROR!", MB_OK);
return 0;
}
while (1)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
if (GetMessage(&msg, NULL, 0, 0))
{
break;
}
}
DispatchMessage(&msg);
TranslateMessage(&msg);
}
return(int)msg.wParam;
}
你的消息循環是相當奇怪的。使用標準循環。爲什麼要兩次寫'#pragma once'?在頭文件中聲明'hWnd'將會捲土重來。不要這樣做。你會有多個不是你想要的實例。這幾天你不想使用'tchar'。您不支持Windows 98. –
TranslateMessage()在DispatchMessage()之前,不在之後。 – andlabs
這是非常糟糕的代碼,我不會指出所有的錯誤。要解決您的直接問題,請執行以下操作:在項目設置中,轉至*常規*並選擇*「使用Unicode字符集」*。在鏈接器設置選項卡上,選擇*系統*並將子系統更改爲*「/ SUBSYSTEM:WINDOWS」*。用'wWinMain'替換你的函數名'winMain'。這將鏈接您的可執行文件。儘管如此,它在運行時仍會非常糟糕。 – IInspectable