CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Posts
    0

    Question How do I set up a modeless dialog box

    I'm relatively new to programming and i was reading a tutorial to set to modeless dialogs. However I got it to work once and now it keeps returning null.I'm using dev c++ 4.9.9.2 portable and ResEdit (Unicode). Does anyone know what the problem is?
    code listed below

    ---------------------------------------------------------------------------------------------------------main.cpp



    #include <windows.h> //contains the functions we need to use to make native windows apps
    #include "resource.h" //resource header file we included for menus and icons

    #define ID_FILE_EXIT 9001
    #define ID_STUFF_GO 9002

    const char g_szClassName[] = "myWindowClass"; //window class name
    #define WNDNAME "Window title" // title of window

    HWND g_hToolsbar = NULL;

    LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); //declares window procedure for later
    BOOL CALLBACK mydialogproc(HWND htools, UINT umsg, WPARAM wparam, LPARAM lparam);

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)// start of program
    {
    //windows class window handel and message varrible declarations
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;


    //window class data
    wc.cbSize = sizeof(WNDCLASSEX); // size of the windows class
    wc.style = 0; // class style
    wc.lpfnWndProc = wndproc; // name or handel to the wndproc function
    wc.cbClsExtra = 0; // amount of extra class data
    wc.cbWndExtra = 0; // amount of extra window data
    wc.hInstance = hInstance; // handle to this instance of the application
    wc.hIcon = NULL; // 32 x 32 icon
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); // cursor
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // handle to brush to be used for background
    wc.lpszMenuName = NULL; // menu name from resource
    wc.lpszClassName = g_szClassName; // window class name
    wc.hIconSm = NULL; // 16 x 16 icon

    if(!RegisterClassEx(&wc)) //register and check if registration was sucessful with os
    {
    MessageBox(NULL, "Failed to register window class", "Error", MB_OK);
    return -1;
    }

    hwnd = CreateWindowEx( // create a handle to the window
    WS_EX_CLIENTEDGE,//exstyle
    g_szClassName, // widow class name
    WNDNAME, // title of the window
    WS_OVERLAPPEDWINDOW, //style
    CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, // x position, y position, width, height
    NULL, NULL, hInstance, NULL); // parrent, handel to Menu, handle to instance, null

    if(hwnd == NULL) //check if sucessful
    {
    MessageBox(NULL, "Create window failed", "Error", MB_OK);
    return -1;
    }

    ShowWindow(hwnd, nCmdShow); // show window
    UpdateWindow(hwnd); // idk but its there
    //message loop
    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
    if(!IsDialogMessage(g_hToolsbar, &msg))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    return msg.wParam;//idk *** but it works
    }

    LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) //wndproc function
    {
    switch(msg)
    {
    case WM_CREATE:
    {
    HMENU hmenu, hsubmenu;
    HICON hicon, hiconsm;
    hmenu = CreateMenu();

    hsubmenu = CreatePopupMenu();
    AppendMenu(hsubmenu, MF_STRING, ID_FILE_EXIT, "E&xit");
    AppendMenu(hmenu,MF_STRING | MF_POPUP, (UINT)hsubmenu, "&File");

    hsubmenu = CreatePopupMenu();
    AppendMenu(hsubmenu, MF_STRING, ID_STUFF_GO, "&Go");
    AppendMenu(hmenu,MF_STRING | MF_POPUP, (UINT)hsubmenu, "&Stuff");

    SetMenu(hwnd, hmenu);
    HINSTANCE hinstance = GetModuleHandle(NULL);
    g_hToolsbar = CreateDialog(hinstance, MAKEINTRESOURCE(IDD_MYDIALOG), hwnd, mydialogproc);
    if(g_hToolsbar == NULL)
    {
    MessageBox(hwnd, "error", "sf", MB_OK);
    PostQuitMessage(0);
    }
    ShowWindow(g_hToolsbar, SW_SHOW);



    break;
    }
    case WM_COMMAND: // menu commands
    switch(LOWORD(wparam))
    {
    case ID_FILE_EXIT:
    DestroyWindow(hwnd);
    break;
    case ID_STUFF_GO:
    MessageBox(hwnd,"Go away","Go",MB_OK | MB_ICONINFORMATION);
    break;
    }
    break;
    case WM_CLOSE: // if close button is pressed
    DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd, msg, wparam, lparam);
    }
    return 0;

    }
    BOOL CALLBACK mydialogproc(HWND htools, UINT umsg, WPARAM wparam, LPARAM lparam)
    {
    switch(umsg)
    {
    case WM_COMMAND:
    switch(LOWORD(wparam))
    {
    }
    default:
    return FALSE;
    }
    return TRUE;
    }

    ----------------------------------------------------------------------------------------------------------resource.rc

    // Generated by ResEdit 1.4.8
    // Copyright (C) 2006-2009
    // http://www.resedit.net

    #include <windows.h>
    #include <commctrl.h>
    #include <richedit.h>
    #include "resource.h"



    //
    // Dialog resources
    //
    LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
    IDD_MYDIALOG DIALOG 0, 0, 186, 95
    STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_FIXEDSYS | WS_VISIBLE | WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_POPUP | WS_SYSMENU
    CAPTION "Dialog"
    FONT 8, "Ms Shell Dlg 2"
    {
    DEFPUSHBUTTON "OK", IDOK, 67, 6, 50, 14
    PUSHBUTTON "Cancel", IDCANCEL, 67, 59, 50, 14
    }


    -----------------------------------------------------------------------------------------------------------resource.h

    #ifndef IDC_STATIC
    #define IDC_STATIC (-1)
    #endif

    #define IDD_MYDIALOG 10000

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How do I set up a modeless dialog box

    Your code is okay, and being properly built in VC++ it runs fine. Evidently it's something wrong with your project. Most probably, you miss resources to get compiled or linked to your binary. To find the reason out, you need to analyze the last error code after getting NULL.
    Best regards,
    Igor

  3. #3
    Join Date
    Oct 2009
    Posts
    5

    Smile Re: How do I set up a modeless dialog box

    Hi The Brtl!

    Your code for a modeless dialog box works quite well with Dev c++ 4.9.9.2
    after I added the linker 'libcomctl32.a' in the parameter linker box in the project options.

    Hope this will set it right for you.

    jameson91

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured