DebugLw 2015-01-13 04:55 采纳率: 100%
浏览 2366
已采纳

为什么不能创建出对话框

#include

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CreateWindowEx(0,TEXT("#32770"),TEXT(""),WS_OVERLAPPEDWINDOW,
50,50,100,100,NULL,NULL,hInstance,NULL);
return 0;
}

  • 写回答

4条回答 默认 最新

  • 91program 博客专家认证 2015-01-13 04:58
    关注

    你需要按 Win32 程序的框架来,不可能在 console 来显示其它窗体吧。

     // Win32MultiPlatformApp.cpp : 定义应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "Win32MultiPlatformApp.h"
    
    
    #define MAX_LOADSTRING 100
    
    // 全局变量:
    HINSTANCE           g_hInst;            // 当前实例
    #ifdef SHELL_AYGSHELL
    HWND                g_hWndMenuBar;      // 菜单栏句柄
    #else // SHELL_AYGSHELL
    HWND                g_hWndCommandBar;   // 命令栏句柄
    #endif // SHELL_AYGSHELL
    
    // 此代码模块中包含的函数的前向声明:
    ATOM            MyRegisterClass(HINSTANCE, LPTSTR);
    BOOL            InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    #ifndef WIN32_PLATFORM_WFSP
    INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
    #endif // !WIN32_PLATFORM_WFSP
    
    #ifdef _UNICODE
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                           HINSTANCE hPrevInstance,
                           LPTSTR    lpCmdLine,
                           int       nCmdShow)
    #else
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
    #endif
    {
        MSG msg;
    
        // 执行应用程序初始化:
        if (!InitInstance(hInstance, nCmdShow)) 
        {
            return FALSE;
        }
    
    #ifndef WIN32_PLATFORM_WFSP
        HACCEL hAccelTable;
        hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32MULTIPLATFORMAPP));
    #endif // !WIN32_PLATFORM_WFSP
    
        // 主消息循环:
        while (GetMessage(&msg, NULL, 0, 0)) 
        {
    #ifndef WIN32_PLATFORM_WFSP
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    #endif // !WIN32_PLATFORM_WFSP
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    
        return (int) msg.wParam;
    }
    
    //
    //  函数: MyRegisterClass()
    //
    //  目的: 注册窗口类。
    //
    //  注释:
    //
    ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
    {
        WNDCLASS wc;
    
        wc.style         = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32MULTIPLATFORMAPP));
        wc.hCursor       = 0;
        wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName  = 0;
        wc.lpszClassName = szWindowClass;
    
        return RegisterClass(&wc);
    }
    
    //
    //   函数: InitInstance(HINSTANCE, int)
    //
    //   目的: 保存实例句柄并创建主窗口
    //
    //   注释:
    //
    //        在此函数中,我们在全局变量中保存实例句柄并
    //        创建和显示主程序窗口。
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
        HWND hWnd;
        TCHAR szTitle[MAX_LOADSTRING];      // 标题栏文本
        TCHAR szWindowClass[MAX_LOADSTRING];    // 主窗口类名
    
        g_hInst = hInstance; // 将实例句柄存储在全局变量中
    
    #if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
        // 在应用程序初始化期间,应调用一次 SHInitExtraControls 以初始化
        // 所有设备特定控件,例如,CAPEDIT 和 SIPPREF。
        // SHInitExtraControls();
    #endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP
    
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
        LoadString(hInstance, IDC_WIN32MULTIPLATFORMAPP, szWindowClass, MAX_LOADSTRING);
    
    #if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
        //如果它已经在运行,则将焦点置于窗口上,然后退出
        hWnd = FindWindow(szWindowClass, szTitle);  
        if (hWnd) 
        {
            // 将焦点置于最前面的子窗口
            // “| 0x00000001”用于将所有附属窗口置于前台并
            // 激活这些窗口。
            SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
            return 0;
        } 
    #endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP
    
        if (!MyRegisterClass(hInstance, TEXT("szWindowClass")))
        {
            return FALSE;
        }
    
        hWnd = CreateWindow(szWindowClass, _T("szTitle"), WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    
        if (!hWnd)
        {
            return FALSE;
        }
    
    #ifdef WIN32_PLATFORM_PSPC
        // 使用 CW_USEDEFAULT 创建主窗口时,将不会考虑菜单栏的高度(如果创建了一个
        // 菜单栏)。因此,我们要在创建窗口后调整其大小
        // 如果菜单栏存在
        if (g_hWndMenuBar)
        {
            RECT rc;
            RECT rcMenuBar;
    
            GetWindowRect(hWnd, &rc);
            GetWindowRect(g_hWndMenuBar, &rcMenuBar);
            rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
    
            MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
        }
    #endif // WIN32_PLATFORM_PSPC
    
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    
    #ifndef SHELL_AYGSHELL
        if (g_hWndCommandBar)
        {
            // CommandBar_Show(g_hWndCommandBar, TRUE);
        }
    #endif // !SHELL_AYGSHELL
    
        return TRUE;
    }
    
    //
    //  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  目的: 处理主窗口的消息。
    //
    //  WM_COMMAND  - 处理应用程序菜单
    //  WM_PAINT    - 绘制主窗口
    //  WM_DESTROY  - 发送退出消息并返回
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
    
    #if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
        static SHACTIVATEINFO s_sai;
    #endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
    
        switch (message) 
        {
            case WM_COMMAND:
                wmId    = LOWORD(wParam); 
                wmEvent = HIWORD(wParam); 
                // 分析菜单选择:
                switch (wmId)
                {
    #ifndef WIN32_PLATFORM_WFSP
                    case IDM_HELP_ABOUT:
                        DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
                        break;
    #endif // !WIN32_PLATFORM_WFSP
    #ifndef SHELL_AYGSHELL
                    case IDM_FILE_EXIT:
                        DestroyWindow(hWnd);
                        break;
    #endif // !SHELL_AYGSHELL
    #ifdef WIN32_PLATFORM_WFSP
                    case IDM_OK:
                        DestroyWindow(hWnd);
                        break;
    #endif // WIN32_PLATFORM_WFSP
    #ifdef WIN32_PLATFORM_PSPC
                    case IDM_OK:
                        SendMessage (hWnd, WM_CLOSE, 0, 0);             
                        break;
    #endif // WIN32_PLATFORM_PSPC
                    default:
                        return DefWindowProc(hWnd, message, wParam, lParam);
                }
                break;
            case WM_CREATE:
    #ifndef SHELL_AYGSHELL
    
    #endif // !SHELL_AYGSHELL
    #ifdef SHELL_AYGSHELL
                SHMENUBARINFO mbi;
    
                memset(&mbi, 0, sizeof(SHMENUBARINFO));
                mbi.cbSize     = sizeof(SHMENUBARINFO);
                mbi.hwndParent = hWnd;
                mbi.nToolBarId = IDR_MENU;
                mbi.hInstRes   = g_hInst;
    
                if (!SHCreateMenuBar(&mbi)) 
                {
                    g_hWndMenuBar = NULL;
                }
                else
                {
                    g_hWndMenuBar = mbi.hwndMB;
                }
    
    #ifndef WIN32_PLATFORM_WFSP
                // 初始化外壳程序激活信息结构
                memset(&s_sai, 0, sizeof (s_sai));
                s_sai.cbSize = sizeof (s_sai);
    #endif // !WIN32_PLATFORM_WFSP
    #endif // SHELL_AYGSHELL
                break;
            case WM_PAINT:
                hdc = BeginPaint(hWnd, &ps);
    
                // TODO: 在此添加任意绘图代码...
    
                EndPaint(hWnd, &ps);
                break;
            case WM_DESTROY:
    #ifndef SHELL_AYGSHELL
    #endif // !SHELL_AYGSHELL
    #ifdef SHELL_AYGSHELL
                CommandBar_Destroy(g_hWndMenuBar);
    #endif // SHELL_AYGSHELL
                PostQuitMessage(0);
                break;
    
    #if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
            case WM_ACTIVATE:
                // 向外壳程序通知我们的激活消息
                SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
                break;
            case WM_SETTINGCHANGE:
                SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
                break;
    #endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
    
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
    
    #ifndef WIN32_PLATFORM_WFSP
    // “关于”框的消息处理程序。
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_INITDIALOG:
    #ifndef SHELL_AYGSHELL
                RECT rectChild, rectParent;
                int DlgWidth, DlgHeight;    // 以像素为单位的对话框宽度和高度
                int NewPosX, NewPosY;
    
                // 设法使“关于”对话框居中显示
                if (GetWindowRect(hDlg, &rectChild)) 
                {
                    GetClientRect(GetParent(hDlg), &rectParent);
                    DlgWidth    = rectChild.right - rectChild.left;
                    DlgHeight   = rectChild.bottom - rectChild.top ;
                    NewPosX     = (rectParent.right - rectParent.left - DlgWidth) / 2;
                    NewPosY     = (rectParent.bottom - rectParent.top - DlgHeight) / 2;
    
                    // 如果“关于”框比实际屏幕大
                    if (NewPosX < 0) NewPosX = 0;
                    if (NewPosY < 0) NewPosY = 0;
                    SetWindowPos(hDlg, 0, NewPosX, NewPosY,
                        0, 0, SWP_NOZORDER | SWP_NOSIZE);
                }
    #endif // !SHELL_AYGSHELL
    #ifdef SHELL_AYGSHELL
                {
                    // 创建一个“完成”按钮并调整其大小。
                    SHINITDLGINFO shidi;
                    shidi.dwMask = SHIDIM_FLAGS;
                    shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
                    shidi.hDlg = hDlg;
                    SHInitDialog(&shidi);
                }
    #endif // SHELL_AYGSHELL
    
                return (INT_PTR)TRUE;
    
            case WM_COMMAND:
    #ifndef SHELL_AYGSHELL
                if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
    #endif // !SHELL_AYGSHELL
    #ifdef SHELL_AYGSHELL
                if (LOWORD(wParam) == IDOK)
    #endif
                {
                    EndDialog(hDlg, LOWORD(wParam));
                    return (INT_PTR)TRUE;
                }
                break;
    
            case WM_CLOSE:
                EndDialog(hDlg, message);
                return (INT_PTR)TRUE;
    
        }
        return (INT_PTR)FALSE;
    }
    #endif // !WIN32_PLATFORM_WFSP
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号