作为一个Jetbrains迷的我,下载了Jetbrains全家桶,我就想用CLion 编写 Windows 项目
前提:必须安装 Visual Studio 2022
-
New Project
-
选择 C++ Executable,取好项目名, 点击 Create
-
在 CMakeList.txt 中添加以下内容,目的是使CLion支持Windows编程和MFC 编程
if (WIN32)add_definitions(-D_WIN32_WINNT=0x0601)
endif ()if (WIN32)set(CMAKE_MFC_FLAG 2)add_definitions(-D_AFXDLL)set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -mwindows")
ENDIF ()
- 在 main.cpp 编写以下内容
#include <windows.h>
// 窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wparam, LPARAM lparam){return DefWindowProc(hWnd, msgID, wparam, lparam);
}
// 入口函数
int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow){// 注册窗口类WNDCLASS wndclass = {0};wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);wndclass.hCursor = nullptr;wndclass.hIcon = nullptr;wndclass.hInstance = hIns;wndclass.lpfnWndProc = WndProc;wndclass.lpszClassName = "Main";wndclass.lpszMenuName = nullptr;wndclass.style = CS_HREDRAW | CS_VREDRAW;// 将以上所有赋值全部写入操作系统RegisterClass(&wndclass);// 在内存中创建窗口 HWND hwnd = CreateWindow("Main","window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, nullptr, nullptr, hIns, nullptr);// 显示窗口ShowWindow(hwnd, SW_SHOW);// 消息循环MSG msg = {nullptr};while (GetMessage(&msg, nullptr, 0, 0)){TranslateMessage(&msg);// 将消息交给窗口处理函数来处理 DispatchMessage(&msg);}return 0;
}
- 右键点击 Reload CMake Project
- 运行 main 函数
大功告成