简体   繁体   English

Win32 / Win API C ++对话框未显示

[英]Win32/Win API C++ Dialog Box is not showing

I just started to learn how to use win api(But i do know basics of c++ and c#) 我刚刚开始学习如何使用Win API(但是我确实知道C ++和C#的基础知识)
I had this problem with and without using window (I'm not using DialogBar) 无论是否使用窗口,我都遇到了这个问题(我不使用DialogBar)

#include "stdafx.h"
#include <iostream>
#include "resource.h"
#include <windows.h>

INT_PTR CALLBACK Dialog(HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
{
    UNREFERENCED_PARAMETER(lparam);
    switch (msg)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;
    case IDH_OK:
        EndDialog(dlg, LOWORD(wparam));

    case WM_COMMAND:
        if (LOWORD(wparam) == IDOK || LOWORD(wparam) == IDCANCEL)
        {
            EndDialog(dlg, LOWORD(wparam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
int main()
{
    DialogBoxW(nullptr, MAKEINTRESOURCE(IDD_DIALOG1), nullptr, Dialog);
    return 0;
}

Update: 1. This is console application(I forgot to say about that) 2. Visible is true for dialog box I didn't know what to show from .rc file 更新:1.这是控制台应用程序(我忘记说了)2.对话框可见,我不知道从.rc文件显示什么

////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DIALOG1 DIALOGEX 0, 0, 309, 176
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,170,144,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,252,155,50,14
    CONTROL         "",IDC_STATIC,"Static",SS_BLACKFRAME,59,41,20,20
    CONTROL         "Radio1",IDC_RADIO1,"Button",BS_AUTORADIOBUTTON,37,71,38,10
    CONTROL         "",IDC_SLIDER1,"msctls_trackbar32",TBS_BOTH | TBS_NOTICKS | WS_TABSTOP,148,108,100,15
    CONTROL         "",IDC_IPADDRESS1,"SysIPAddress32",WS_TABSTOP,127,55,100,15
    CONTROL         "",IDC_MFCSHELLTREE1,"MfcShellTree",WS_BORDER | WS_HSCROLL | WS_TABSTOP,148,7,138,132
    CONTROL         "",IDC_NETADDRESS1,"msctls_netaddress",ES_AUTOHSCROLL | WS_TABSTOP,7,98,70,14,WS_EX_CLIENTEDGE
    CONTROL         "",IDC_DATETIMEPICKER1,"SysDateTimePick32",DTS_RIGHTALIGN | DTS_UPDOWN | WS_TABSTOP | 0x8,30,116,100,15
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
    IDD_DIALOG1, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 302
        TOPMARGIN, 7
        BOTTOMMARGIN, 169
    END
END
#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Dialog Info
//

IDD_DIALOG1 DLGINIT
BEGIN
    IDC_MFCSHELLTREE1, 0x37c, 88, 0
0x4d3c, 0x4346, 0x6853, 0x6c65, 0x546c, 0x6572, 0x4365, 0x7274, 0x5f6c, 
0x6e45, 0x6261, 0x656c, 0x6853, 0x6c65, 0x436c, 0x6e6f, 0x6574, 0x7478, 
0x654d, 0x756e, 0x463e, 0x4c41, 0x4553, 0x2f3c, 0x464d, 0x5343, 0x6568, 
0x6c6c, 0x7254, 0x6565, 0x7443, 0x6c72, 0x455f, 0x616e, 0x6c62, 0x5365, 
0x6568, 0x6c6c, 0x6f43, 0x746e, 0x7865, 0x4d74, 0x6e65, 0x3e75, 
    0
END


/////////////////////////////////////////////////////////////////////////////
//
// AFX_DIALOG_LAYOUT
//

IDD_DIALOG1 AFX_DIALOG_LAYOUT
BEGIN
    0
END

#endif    // Russian (Russia) resources
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DIALOGBAR DIALOGEX 0, 0, 330, 16
STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
FONT 8, "MS Shell Dlg", 400, 0, 0x0
BEGIN
    LTEXT           "TODO: layout dialog bar",IDC_STATIC,126,4,77,8
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
    IDD_DIALOGBAR, DIALOG
    BEGIN
        LEFTMARGIN, 6
        RIGHTMARGIN, 323
        TOPMARGIN, 7
        BOTTOMMARGIN, 9
    END
END
#endif    // APSTUDIO_INVOKED

#endif    // English (United States) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

The dialog box is just bunch of different things that i want to use later 该对话框只是一堆不同的东西,我以后想用

Several of your controls in your dialog template are "common controls" and you need to call InitCommonControls[Ex] first before you can use them. 对话框模板中的几个控件是“公共控件”,您需要先调用InitCommonControls[Ex]然后才能使用它们。 MfcShellTree is a MFC class and can only be used in MFC applications. MfcShellTree是一个MFC类,只能在MFC应用程序中使用。

Add the DS_NOFAILCREATE style to force the dialog to be created even if some of the child controls could not be created. 添加DS_NOFAILCREATE样式以强制创建对话框,即使某些子控件无法创建也是如此。 You should also add WS_VISIBLE . 您还应该添加WS_VISIBLE

IDH_OK could be a problem if it is < WM_APP but you don't show where it is defined so I don't know. IDH_OK可能是一个问题,如果它是< WM_APP但你不显示它的定义,所以我不知道。 It is certainly missing a break; 它肯定错过了break; in the dialog procedure though. 在对话过程中。

Finally, you should check the return value from DialogBoxW , if it is 0 or -1 you should call GetLastError to find out why it failed... 最后,您应该检查DialogBoxW的返回值,如果返回值为0或-1,则应调用GetLastError找出失败的原因...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM