simple modal dialog

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Burt

    #1

    simple modal dialog

    I need to put up a simple modal dialog, and am surprised how hard it seems
    to be. Am I missing something here?

    I have created a dialog resource IDD_DELETE_S9. It has some static text
    plus 3 buttons. The buttons have ids of IDOK, IDCANCEL and IDC_RENAME. All
    I want to do is put up this modal dialog asking the user what to do. All I
    care is which button he pressed.

    I tried using

    int answer = DialogBox(0, MAKEINTRESOURCE (IDD_DELETE_S9) ,
    hwnd, (DLGPROC)Import _DialogProc);

    but the dialog never shows and it just drops through, giving me a non-useful
    'answer'.

    Everything I am now reading in MSDN seems to indicate that I need to write a
    new class inheriting from CDialog to handle this and return the button
    pressed. Is that really needed?

    This sure seems like something that should be accomplished in a one-line
    call...?


  • Scott McPhillips [MVP]

    #2
    Re: simple modal dialog

    Burt wrote:[color=blue]
    > I need to put up a simple modal dialog, and am surprised how hard it seems
    > to be. Am I missing something here?
    >
    > I have created a dialog resource IDD_DELETE_S9. It has some static text
    > plus 3 buttons. The buttons have ids of IDOK, IDCANCEL and IDC_RENAME. All
    > I want to do is put up this modal dialog asking the user what to do. All I
    > care is which button he pressed.
    >
    > I tried using
    >
    > int answer = DialogBox(0, MAKEINTRESOURCE (IDD_DELETE_S9) ,
    > hwnd, (DLGPROC)Import _DialogProc);
    >
    > but the dialog never shows and it just drops through, giving me a non-useful
    > 'answer'.
    >
    > Everything I am now reading in MSDN seems to indicate that I need to write a
    > new class inheriting from CDialog to handle this and return the button
    > pressed. Is that really needed?
    >
    > This sure seems like something that should be accomplished in a one-line
    > call...?[/color]

    It's only hard if you have no idea what you are doing. If you are using
    MFC then the DialogBox API call is not applicable.

    In Visual C++ the IDE 'class wizard' writes the new class derived from
    CDialog for you. Right click on your dialog template and select Class
    Wizard. Name your CDialog class.

    CYourDialog dlg;
    dlg.DoModal();

    DoModal returns IDOK or IDCANCEL. Detecting your unique button requires
    you to do something, like set a bool when it is clicked.

    --
    Scott McPhillips [VC++ MVP]

    Comment

    • Jeff Partch [MVP]

      #3
      Re: simple modal dialog

      "Burt" <burt@mindsto rm-inc.com> wrote in message
      news:2Q1we.9051 8$vH1.35519@fe0 4.news.easynews .com...[color=blue]
      > I need to put up a simple modal dialog, and am surprised how hard it seems
      > to be. Am I missing something here?
      >
      > I have created a dialog resource IDD_DELETE_S9. It has some static text
      > plus 3 buttons. The buttons have ids of IDOK, IDCANCEL and IDC_RENAME.[/color]
      All[color=blue]
      > I want to do is put up this modal dialog asking the user what to do. All[/color]
      I[color=blue]
      > care is which button he pressed.
      >
      > I tried using
      >
      > int answer = DialogBox(0, MAKEINTRESOURCE (IDD_DELETE_S9) ,
      > hwnd, (DLGPROC)Import _DialogProc);[/color]

      What happens if you replace your (HINSTANCE)0 argument with
      GetModuleHandle (NULL)...?
      [color=blue]
      >
      > but the dialog never shows and it just drops through, giving me a[/color]
      non-useful[color=blue]
      > 'answer'.[/color]

      What does it return?
      [color=blue]
      >
      > Everything I am now reading in MSDN seems to indicate that I need to write[/color]
      a[color=blue]
      > new class inheriting from CDialog to handle this and return the button
      > pressed. Is that really needed?[/color]

      Not really.
      [color=blue]
      >
      > This sure seems like something that should be accomplished in a one-line
      > call...?[/color]

      Yup. Followed by evaluation of the return of course.

      --
      Jeff Partch [VC++ MVP]


      Comment

      • Burt

        #4
        Re: simple modal dialog

        >> I tried using[color=blue][color=green]
        >>
        >> int answer = DialogBox(0, MAKEINTRESOURCE (IDD_DELETE_S9) ,
        >> hwnd, (DLGPROC)Import _DialogProc);[/color]
        >
        > What happens if you replace your (HINSTANCE)0 argument with
        > GetModuleHandle (NULL)...?[/color]

        Same thing. No change in behavior
        [color=blue]
        >[color=green]
        >>
        >> but the dialog never shows and it just drops through, giving me a[/color]
        > non-useful[color=green]
        >> 'answer'.[/color]
        >
        > What does it return?[/color]

        -1
        [color=blue][color=green]
        >> This sure seems like something that should be accomplished in a one-line
        >> call...?[/color]
        >
        > Yup. Followed by evaluation of the return of course.[/color]

        Yes, I have code folloing that that looks like:

        if (answer == IDCANCEL)
        return FALSE; // user decided to stop

        etc. So you are saying that my DialogBox call should be enough? It never
        enters my CALLBACK and never shows the dialog. Any idea why that would be?


        Comment

        • Burt Johnson

          #5
          Re: simple modal dialog

          Scott McPhillips [MVP] <org-dot-mvps-at-scottmcp> wrote:
          [color=blue]
          > It's only hard if you have no idea what you are doing. If you are using
          > MFC then the DialogBox API call is not applicable.[/color]

          I never said I knew what I was doing. I have only been using MFC for a
          couple months, after 32 years programming other environments. No need
          to be insulting though...

          Why is DialogBox not applicable for MFC? Is that another of the
          'managed memory model' (or whatever it is called) routines? If so, how
          the heck can I tell the difference when reading MSDN. I have led down
          that path a couple times now, and it is frustrating, to say the least.
          [color=blue]
          >
          > In Visual C++ the IDE 'class wizard' writes the new class derived from
          > CDialog for you. Right click on your dialog template and select Class
          > Wizard. Name your CDialog class.
          >
          > CYourDialog dlg;
          > dlg.DoModal();
          >
          > DoModal returns IDOK or IDCANCEL. Detecting your unique button requires
          > you to do something, like set a bool when it is clicked.[/color]

          Yeah, I know how to go through that hoop. Just seems silly to have to
          add a whole new class just to get something simple like this. In other
          environments I have programmed in, it was a simple one-line call with
          the addition of a resource to define the appearance (which I have
          created).


          --
          - Burt Johnson
          MindStorm, Inc.

          Comment

          • William DePalo [MVP VC++]

            #6
            Re: simple modal dialog

            "Burt Johnson" <burt@mindsto rm-inc.com> wrote in message
            news:1gyu2tx.1k ec1cfihl85aN%bu rt@mindstorm-inc.com...[color=blue]
            > Scott McPhillips [MVP] <org-dot-mvps-at-scottmcp> wrote:
            >[color=green]
            >> It's only hard if you have no idea what you are doing. If you are using
            >> MFC then the DialogBox API call is not applicable.[/color]
            >
            > I never said I knew what I was doing. I have only been using MFC for a
            > couple months, after 32 years programming other environments. No need
            > to be insulting though...
            >
            > Why is DialogBox not applicable for MFC?[/color]

            DialogBox() is a function in the Win32 API. MFC provides a CDialog class to
            simplify things and hide some of the details.
            [color=blue]
            > Is that another of the 'managed memory model' (or whatever
            > it is called) routines? If so, how the heck can I tell the
            > difference when reading MSDN.[/color]

            No, it is not. MFC is a class library used by and large to build native
            applications.
            [color=blue]
            > I have led down that path a couple times now, and it is frustrating,
            > to say the least.[/color]

            Well, it is at worst an embarassment of choices. There is the native API,
            MFC, ATL, WTL, .Net Framework, etc
            [color=blue]
            > Yeah, I know how to go through that hoop. Just seems silly to have to
            > add a whole new class just to get something simple like this.[/color]

            I guess that depends on one's perspective and expectations.
            [color=blue]
            > In other environments I have programmed in, it was a simple one-line call
            > with
            > the addition of a resource to define the appearance (which I have
            > created).[/color]

            Well, there is the MessageBox() function which can put up a dialog in one
            line but you'll have to settle for predefined sets of buttons such as ok, or
            yes and no or yes and no and cancel etc

            Back to your problem ... One thing you can do is to pass the ID of the
            button used to close the dialog to the EndDialog() call that dismisses the
            dialog. EndDialog() sees to it that that value is returned by the call to
            DialogBox(). Since control IDs are small positive numbers, if you get 0 or a
            negative result then the call failed. In that case call GetlastError() to
            determine the reason why the call failed. If you don't understand the code -
            they are defined in <winerror.h> - you can post it here.

            Regards,
            Will



            Comment

            • Jeff Partch [MVP]

              #7
              Re: simple modal dialog

              "Burt" <burt@mindsto rm-inc.com> wrote in message
              news:Y43we.2559 37$JA.73942@fe0 1.news.easynews .com...[color=blue][color=green][color=darkred]
              > >> but the dialog never shows and it just drops through, giving me a[/color]
              > > non-useful[color=darkred]
              > >> 'answer'.[/color]
              > >
              > > What does it return?[/color]
              >
              > -1[/color]

              Do then follow the advice to see what GetLastError has to say. What OS are
              you testing on?
              [color=blue]
              >[color=green][color=darkred]
              > >> This sure seems like something that should be accomplished in a[/color][/color][/color]
              one-line[color=blue][color=green][color=darkred]
              > >> call...?[/color]
              > >
              > > Yup. Followed by evaluation of the return of course.[/color]
              >
              > Yes, I have code folloing that that looks like:
              >
              > if (answer == IDCANCEL)
              > return FALSE; // user decided to stop
              >
              > etc. So you are saying that my DialogBox call should be enough? It never
              > enters my CALLBACK and never shows the dialog. Any idea why that would[/color]
              be?

              I agree that it's not the MFC way to so it -- and concur with the
              admonitions for the most part, but in a quick test it works for me -- even
              using the (HINSTANCE)0.

              --
              Jeff Partch [VC++ MVP]



              Comment

              Working...