[good]visual studio 2022 创建空的win32程序

05_MFC窗口的创建_哔哩哔哩_bilibili

参考这个

VS创建空的Win32程序 - fenggwsx - 博客园 (cnblogs.com)

修改subsystem项

 

 

 编译运行

 

 

测试代码

#include <windows.h>

int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {
    messagebox(null, text("hello world"), text("this a new window"), mb_ok);
    return 0;
}

 

上面是全空的,下面可以使用mfc

Visual Studio 2022如何创建Win32项目_vs2022怎么创建win32项目-CSDN博客

 

在项目属性里面修改以下两项

 

测试代码

#include <afxwin.h>
class CMyFrameWnd : public CFrameWnd
{
};

class CMyWinApp : public CWinApp
{
public:
    CMyWinApp()
    {
    }
    virtual BOOL InitInstance()
    {
        CMyFrameWnd *pFrame = new CMyFrameWnd;
        pFrame->Create(NULL, "My Application");
        m_pMainWnd = pFrame;
        // pFrame->ShowWindow(m_nCmdShow);
        pFrame->ShowWindow(SW_SHOW);
        pFrame->UpdateWindow();
        return TRUE;
    }
};

CMyWinApp theApp;

 

 

 

前面是用了mfc,这是用的win32

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_LBUTTONDOWN:
    {
        int xPos = LOWORD(lParam);
        int yPos = HIWORD(lParam);

        char buf[1024];
        wsprintf(buf, TEXT("x=%d,y=%d"), xPos, yPos);

        MessageBox(hwnd, buf, TEXT("Pressed"), MB_OK);

        break;
    }
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    WNDCLASS wc = { 0 };
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszClassName = TEXT("Win");

    RegisterClass(&wc);

    HWND hwnd = CreateWindow(
        wc.lpszClassName,
        TEXT("Windows"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (hwnd == NULL) {
        MessageBox(NULL, TEXT("窗口创建失败!"), TEXT("错误"), MB_ICONERROR);
        return 0;
    }

    ShowWindow(hwnd, nShowCmd);
    UpdateWindow(hwnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

 

posted on 2023-12-14 09:50  风中狂笑  阅读(118)  评论(0编辑  收藏  举报

导航