CLion手把手教你创建Windows项目

本文介绍了如何在JetBrainsCLion中设置Windows开发环境,包括安装VisualStudio2022,配置CMakeLists.txt以支持MFC,并在main.cpp中实现基本窗口程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

作为一个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 函数
    在这里插入图片描述

大功告成

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasonakeke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值