试图在visual studio上用C++创建一个桌面应用程序
Trying to create a desktop application in C++ on visual studio
我正在尝试编程一个桌面应用程序,该应用程序最终将与arduino(尚未推出(通信,以控制我房间和电脑周围的LED灯。然而,由于两个问题,我甚至无法运行该程序。
- 当我尝试使用WndProc时,它说"找不到WndProc的函数定义
LRESULT CALLBACK WndProc(
_In_ HWND hWnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
- 当我尝试使用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的减速。
相关文章:
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何将一个窗口保留在另一个应用程序窗口的前面
- 将库链接到另一个应用程序时,CMAKE 找不到库
- 调试控制台 ChessEngine.exe "used"在另一个应用程序(国际象棋 GUI)中
- 在 Windows 10 中将一个应用程序放在另一个应用程序前面
- 将数据从一个应用程序单向发送到另一个应用程序的最佳做法
- 当电子的窗口隐藏时如何激活另一个应用程序窗口
- 与 Python 中子进程中的另一个应用程序的交互式会话
- 如何将Qt嵌入到另一个应用程序中(非阻塞方式)
- 调试从 Linux 中的另一个C++应用程序启动的 python 脚本
- 在 Linux 上编译一个 c++ 应用程序,并在 TI TIVA TM4C123GH6PM上刷新它
- 另一个应用程序用 c++ 在 Windows 上的线程中破坏 QTimer
- 为什么我需要运行一个应用程序作为Papi库工作的根源
- 发送消息WM_SETTEXT不适用于 MFC 应用程序的另一个应用程序
- 当从一个应用程序调用时,在DLL方法中创建COM接口指针是有效的,但当从另一个应用软件调用时则无效
- 在Qt和C++中从另一个应用程序启动一个应用软件会产生问题
- 在另一个应用程序中嵌入python时,如何在子模块(即scipy.optimize.nnls)中导入或调用函数
- 如何从Windows上的Windows API上的另一个应用程序获得应用程序宽度和高度
- QT应用程序在OSX中启动另一个应用程序
- mfc c++通过编辑控件设置另一个应用程序的HWND地址