试图在visual studio上用C++创建一个桌面应用程序

Trying to create a desktop application in C++ on visual studio

本文关键字:一个 应用程序 桌面 创建 visual studio C++ 上用      更新时间:2023-10-16

我正在尝试编程一个桌面应用程序,该应用程序最终将与arduino(尚未推出(通信,以控制我房间和电脑周围的LED灯。然而,由于两个问题,我甚至无法运行该程序。

  1. 当我尝试使用WndProc时,它说"找不到WndProc的函数定义
LRESULT CALLBACK WndProc(
_In_ HWND   hWnd,
_In_ UINT   message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
  1. 当我尝试使用WinMain时,它会说"WinMain的注释不一致:此实例没有注释">
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)

以下是完整的源代码:


#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <string.h>

static TCHAR szWindowClass[] = _T("Colour Control");
static TCHAR szTitle[] = _T("Colour Control 1.0");
HINSTANCE hInst;
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR     lpCmdLine,
_In_ int       nCmdShow
);
LRESULT CALLBACK WndProc(
_In_ HWND   hWnd,
_In_ UINT   message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
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.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
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;
}

// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application dows not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 100,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
// Store instance handle in our global variable
hInst = hInstance;
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}

为什么要在.cpp文件中定义WinMain和WndProc。您不应该这样做,相反,您必须只提供这些函数的定义。类似于WindowsProc

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ 
switch(msg)
{
case WM_LBUTTONDOWN:
//Code 
break;                  
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
} 
return 0;  

}

您必须删除WndProc和WinMain的减速。

相关文章: