作為一個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 函數
大功告成