Varible
Varible
2020-02-21 17:04
采纳率: 61.3%
浏览 576

求“C++ WinMain()”代码!!!!!!!!!

  • 求“ C++ WinMain() ”代码!!!!!!!!!
  • 有窗口
  • 最好 简单的

求助各位大佬啊!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • caozhy
    已采纳

    在VC++里新建一个win32项目

    // Q1055430.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "Q1055430.h"
    
    #define MAX_LOADSTRING 100
    
    // Global Variables:
    HINSTANCE hInst;                                // current instance
    TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
    
    // Forward declarations of functions included in this code module:
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);
    
        // TODO: Place code here.
        MSG msg;
        HACCEL hAccelTable;
    
        // Initialize global strings
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
        LoadString(hInstance, IDC_Q1055430, szWindowClass, MAX_LOADSTRING);
        MyRegisterClass(hInstance);
    
        // Perform application initialization:
        if (!InitInstance (hInstance, nCmdShow))
        {
            return FALSE;
        }
    
        hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_Q1055430));
    
        // Main message loop:
        while (GetMessage(&msg, NULL, 0, 0))
        {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    
        return (int) msg.wParam;
    }
    
    
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage are only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
        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, MAKEINTRESOURCE(IDI_Q1055430));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_Q1055430);
        wcex.lpszClassName  = szWindowClass;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    
        return RegisterClassEx(&wcex);
    }
    
    //
    //   FUNCTION: InitInstance(HINSTANCE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
    
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    
    //
    //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND  - process the application menu
    //  WM_PAINT    - Paint the main window
    //  WM_DESTROY  - post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
    
        switch (message)
        {
        case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
    
    // Message handler for about box.
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        UNREFERENCED_PARAMETER(lParam);
        switch (message)
        {
        case WM_INITDIALOG:
            return (INT_PTR)TRUE;
    
        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return (INT_PTR)TRUE;
            }
            break;
        }
        return (INT_PTR)FALSE;
    }
    
    

    这个程序需要一个资源:

    //Microsoft Visual C++ generated resource script.
    //
    #include "resource.h"
    
    #define APSTUDIO_READONLY_SYMBOLS
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 2 resource.
    //
    #ifndef APSTUDIO_INVOKED
    #include "targetver.h"
    #endif
    #define APSTUDIO_HIDDEN_SYMBOLS
    #include "windows.h"
    #undef APSTUDIO_HIDDEN_SYMBOLS
    /////////////////////////////////////////////////////////////////////////////
    #undef APSTUDIO_READONLY_SYMBOLS
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
    LANGUAGE 9, 1
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Icon
    //
    
    // Icon with lowest ID value placed first to ensure application icon
    // remains consistent on all systems.
    
    IDI_Q1055430       ICON         "Q1055430.ico"
    IDI_SMALL               ICON         "small.ico"
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Menu
    //
    
    IDC_Q1055430 MENU
    BEGIN
        POPUP "&File"
        BEGIN
            MENUITEM "E&xit",                IDM_EXIT
        END
        POPUP "&Help"
        BEGIN
            MENUITEM "&About ...",           IDM_ABOUT
        END
    END
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Accelerator
    //
    
    IDC_Q1055430 ACCELERATORS
    BEGIN
        "?",            IDM_ABOUT,              ASCII,  ALT
        "/",            IDM_ABOUT,              ASCII,  ALT
    END
    
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // Dialog
    //
    
    IDD_ABOUTBOX DIALOGEX 0, 0, 170, 62
    STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "About Q1055430"
    FONT 8, "MS Shell Dlg"
    BEGIN
        ICON            IDR_MAINFRAME,IDC_STATIC,14,14,21,20
        LTEXT           "Q1055430, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
        LTEXT           "Copyright (C) 2020",IDC_STATIC,42,26,114,8
        DEFPUSHBUTTON   "OK",IDOK,113,41,50,14,WS_GROUP
    END
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // DESIGNINFO
    //
    
    #ifdef APSTUDIO_INVOKED
    GUIDELINES DESIGNINFO
    BEGIN
        IDD_ABOUTBOX, DIALOG
        BEGIN
            LEFTMARGIN, 7
            RIGHTMARGIN, 163
            TOPMARGIN, 7
            BOTTOMMARGIN, 55
        END
    END
    #endif    // APSTUDIO_INVOKED
    
    #ifdef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // TEXTINCLUDE
    //
    1 TEXTINCLUDE
    BEGIN
        "resource.h\0"
    END
    
    2 TEXTINCLUDE
    BEGIN
        "#ifndef APSTUDIO_INVOKED\r\n"
        "#include ""targetver.h""\r\n"
        "#endif\r\n"
        "#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
        "#include ""windows.h""\r\n"
        "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
        "\0"
    END
    
    3 TEXTINCLUDE
    BEGIN
        "\r\n"
        "\0"
    END
    
    #endif    // APSTUDIO_INVOKED
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // String Table
    //
    
    STRINGTABLE
    BEGIN
       IDC_Q1055430   "Q1055430"
       IDS_APP_TITLE       "Q1055430"
    END
    
    #endif
    /////////////////////////////////////////////////////////////////////////////
    
    
    
    #ifndef APSTUDIO_INVOKED
    /////////////////////////////////////////////////////////////////////////////
    //
    // Generated from the TEXTINCLUDE 3 resource.
    //
    
    /////////////////////////////////////////////////////////////////////////////
    #endif    // not APSTUDIO_INVOKED
    
    

    对应的resurce.h

    //{{NO_DEPENDENCIES}}
    // Microsoft Visual C++ generated include file.
    // Used by Q1055430.rc
    //
    
    #define IDS_APP_TITLE           103
    
    #define IDR_MAINFRAME           128
    #define IDD_Q1055430_DIALOG 102
    #define IDD_ABOUTBOX            103
    #define IDM_ABOUT               104
    #define IDM_EXIT                105
    #define IDI_Q1055430            107
    #define IDI_SMALL               108
    #define IDC_Q1055430            109
    #define IDC_MYICON              2
    #ifndef IDC_STATIC
    #define IDC_STATIC              -1
    #endif
    // Next default values for new objects
    //
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    
    #define _APS_NO_MFC                 130
    #define _APS_NEXT_RESOURCE_VALUE    129
    #define _APS_NEXT_COMMAND_VALUE     32771
    #define _APS_NEXT_CONTROL_VALUE     1000
    #define _APS_NEXT_SYMED_VALUE       110
    #endif
    #endif
    
    
    点赞 评论
  • BostonRayAlen
    bostonAlen 2020-02-21 18:16

    这是最简单的了

    #include <Windows.h>        // windows程序开发必须使用头文件
    
    #pragma comment(lib,"winmm.lib")   // DLL
    
    int WINAPI WinMain(HINSTANCE hInstance,     // 应用程序当前实例的句柄
        HINSTANCE hPrevInstance,                // 应用程序先前实例的句柄 
        LPSTR lpCmdLine,                        // 指向应用程序命令选择字符串的指针,不包括执行文件名
        int nCmdShow)                           // 指明窗口如何显示,比如:隐藏窗口SW_HIDE
    {
        MessageBox(NULL, 
            L"欢迎你.", 
            L"提示",
            MB_ABORTRETRYIGNORE| MB_ICONQUESTION);
    
        return 0;
    }
    
    
    点赞 评论
  • Varible
    Varible 2020-02-21 21:12

    @bostonAlen
    不是那个消息框……

    点赞 评论

相关推荐