CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2004
    Posts
    28

    How to create/display a modal window? And surplus of win32 code

    Hello,

    I actually have two problems, but will start with a simple one.

    I'm writing a usual but very raw win32 application, i.e. with:
    #define WIN32_LEAN_AND_MEAN
    and without any MFC (well, just a wish to use plain WinAPI along with C++).

    My frist problem is I can't find info on how to create/display a modal window? I'm creating all my windows use "CreateWindowEx" and display my windows using "ShowWindow". In Delphi, I would simply write instead of FormX.Show, FormX.ShowModal. How can I do that with WinAPI?

    And problem number 2:
    This program's code becomes HUGE because of using WinAPI functions. The thing is, I have a lot of windows, and all those windows have many controls, which are also "windows" in terms of Windows as OS. So, for example, to create one button I have to write this:

    Code:
    	// Create "Update Resolution" Button for UI
    	if (!(hButtonUpdateUI = CreateWindowEx
    	  (
    		NULL,
    		"button",
    		"Update Resolution",
    		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    		225,65,
    		150, 24,
    		hMainWindow,
    		HMENU(CHANGE_UI_RESOLUTION_BTN),
    		(HINSTANCE) GetWindowLong(hMainWindow, GWL_HINSTANCE),
    		NULL
    	  )
    	 )
    	)
    	{
    		MessageBox(hMainWindow,"Could not create button control","Button creation failed",MB_OK | MB_ICONEXCLAMATION);
    		exit(0);
    	}
    And I have a lot of buttons, edit controls, comboboxes, etc. on forms. For instance, one of my forms contains 32 (!) controls, now multiply 32 by 20 (LOC to create one control) == 640 lines of code (!) for just creating the controls Am I doing something wrong? Is there a way to avoid writing so much code? I'm just going to be lost in the code, the program will look very complicated while not being complicated at all! In fact, that's the reason I stopped writing the code for awhile, because I'm feeling that it's NOT done this way.

    Any suggestions or help would be greatly appreciated.
    Last edited by Xatrix; January 14th, 2006 at 10:51 AM.

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How to create/display a modal window? And surplus of win32 code

    Quote Originally Posted by Xatrix
    My frist problem is I can't find info on how to create/display a modal window? I'm creating all my windows use "CreateWindowEx" and display my windows using "ShowWindow". In Delphi, I would simply write instead of FormX.Show, FormX.ShowModal. How can I do that with WinAPI?
    Take a look at the 'DialogBox()' function...

    Quote Originally Posted by Xatrix
    This program's code becomes HUGE because of using WinAPI functions. The thing is, I have a lot of windows, and all those windows have many controls, which are also "windows" in terms of Windows as OS.
    Well...using the plain API while creating dialog controls incorporates a number of lines of code. That is why wrapper classes such as the MFC classes exist. Using the API you need to do everything manually and that causes the code getting a little bit longer. Using wrapper classes such as the MFC just hide all the underlying code that is necessary.

  3. #3
    Join Date
    May 2004
    Posts
    28

    Re: How to create/display a modal window? And surplus of win32 code

    well, OK, I guess I'll have to cope with writing all this by hand.

    But I still don't understand how can I use DialogBox() to create a *window* (a.k.a. Form). You see, I don't need a modal *message box*, I need a usual window where I can place my own buttons and other controls, but which would be modal, so that I couldn't switch to the parent window. Can anybody give me some sample code or some link maybe?

    Thanks for the reply!

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to create/display a modal window? And surplus of win32 code

    Just an aside note:

    Do not confuse "lines of code" with "effective lines of code".
    In other words in C/C++ you can write
    Code:
    int nRet = AFunction(arg1,
                         arg2,
                         arg3,
                         arg4);
    which has four lines of code, but in fact represents only one effective line of code, and can be written as well:
    Code:
    int nRet = AFunction(arg1, arg2, arg3, arg4);
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    May 2004
    Posts
    28

    Re: How to create/display a modal window? And surplus of win32 code

    well, I'm sure that's not how LOC are counted. Nobody will write that CreateWindowEx function in one line, because it'll be unreadable, hence all those lines are "effective" ... well, of course one bracket is not a code, but that's how code will look like anyway. And just because that line with a bracket is not effective, it does *not* make my code smaller In this context, I mean LOC as space occupied by the text, that's the problem, I'm not counting them to calculate the price or anything.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to create/display a modal window? And surplus of win32 code

    Choices:
    1. Return to Delphi
    2. Accommodate with Windows API programming; soon the code will look for you not so "huge".
    3. Start using MFC; your life will become easier.
    4. ...
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    389

    Re: How to create/display a modal window? And surplus of win32 code

    Quote Originally Posted by Andreas Masur
    Well...using the plain API while creating dialog controls incorporates a number of lines of code. That is why wrapper classes such as the MFC classes exist. Using the API you need to do everything manually and that causes the code getting a little bit longer. Using wrapper classes such as the MFC just hide all the underlying code that is necessary.
    Well, i don't see all these differences between
    Code:
    CWnd button;
    button.Create("BUTTON", "I'm a button", WS_CHILD|WS_VISIBLE, CRect(0, 0, 20, 20), this);
    and
    Code:
    HWND button = NULL;
    button = CreateWindow("BUTTON", "I'm a button", WS_CHILD|WS_VISIBLE, 0,0,20,20, hwnd, NULL, GetModuleHandle(NULL), 0);
    There is just a GetModuleHandle(NULL) in addition.

    Quote Originally Posted by Xatrix
    Is there a way to avoid writing so much code?
    Well, if you use DialogBox() as Andreas suggested, all your controls goes inside a .rc file. Windows do everything for you, for example it sets the font, and you need only to sets its it and it position. For example, this code
    Code:
    // Create "Update Resolution" Button for UI
    	if (!(hButtonUpdateUI = CreateWindowEx
    	  (
    		NULL,
    		"button",
    		"Update Resolution",
    		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    		225,65,
    		150, 24,
    		hMainWindow,
    		HMENU(CHANGE_UI_RESOLUTION_BTN),
    		(HINSTANCE) GetWindowLong(hMainWindow, GWL_HINSTANCE),
    		NULL
    	  )
    	 )
    	)
    	{
    		MessageBox(hMainWindow,"Could not create button control","Button creation failed",MB_OK | MB_ICONEXCLAMATION);
    		exit(0);
    	}
    (a note: i don't see the need of that error checking, this is often used when you start learning (you get errors more frequently) but avoid them if you don't think it will fail)

    could be shorten to..
    Code:
    PUSHBUTTON      "", CHANGE_UI_RESOLUTION_BTN, 225,65,150,24
    ...inside a .rc file. stop! The font is the same as the dialog one, the position is based on this font. You don't need anything else.
    Last edited by kkez; January 14th, 2006 at 02:27 PM.

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How to create/display a modal window? And surplus of win32 code

    Quote Originally Posted by kkez
    Well, i don't see all these differences between
    Code:
    CWnd button;
    button.Create("BUTTON", "I'm a button", WS_CHILD|WS_VISIBLE, CRect(0, 0, 20, 20), this);
    and
    Code:
    HWND button = NULL;
    button = CreateWindow("BUTTON", "I'm a button", WS_CHILD|WS_VISIBLE, 0,0,20,20, hwnd, NULL, GetModuleHandle(NULL), 0);
    There is just a GetModuleHandle(NULL) in addition.
    Well...I basically meant the combination MFC and the resource editor, DDX, DDV etc.
    Last edited by Andreas Masur; January 14th, 2006 at 02:56 PM.

  9. #9
    Join Date
    May 2004
    Posts
    28

    Re: How to create/display a modal window? And surplus of win32 code

    Quote Originally Posted by kkez
    could be shorten to..
    Code:
    PUSHBUTTON      "", CHANGE_UI_RESOLUTION_BTN, 225,65,150,24
    Ok sorry, but could you elaborate a bit more about how I make this code work using recource files, and without MFC? Is there going to be some sort of a template in .rc?



    P.S. If anyone is curious to look at, I can post the whole source code when the program is completed, at least some working beta of it. And I'm sure you won't like it, cause I already don't lol
    Last edited by Xatrix; January 15th, 2006 at 07:30 AM.

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to create/display a modal window? And surplus of win32 code

    Quote Originally Posted by Xatrix
    Ok sorry, but could you elaborate a bit more about how I make this code work using recource files, and without MFC? Is there going to be some sort of a template in .rc?
    A resource script (.rc) file contains descriptions of resources (dialogs, icons, menus, etc) of your project. In Visual C++ (either if using MFC or not) you can use resource editor without care about (and without manually edit) the .rc file.
    Just you have to be sure that .rc file is included in your project.
    Quote Originally Posted by Xatrix
    P.S. If anyone is curious to look at, I can post the whole source code when the program is completed, at least some working beta of it. And I'm sure you won't like it, cause I already don't lol
    Nobody will LOL, just few can be a little nervous.
    Well I was kidding. Go ahead, zip it and upload!
    But don't forget to delete first the intermediary files (from debug and/or release folders) as well as .aps, .ncb, .opt, and .plg files.


    PS. The answers as well as my examples from the following threads may help you (how to make a Win32 "dialog-based" app):
    Progress Bar win32
    and
    ComboBox
    Last edited by ovidiucucu; January 15th, 2006 at 08:13 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  11. #11
    Join Date
    May 2004
    Posts
    28

    Re: How to create/display a modal window? And surplus of win32 code

    Hi,

    I just want to answer my own question (for those who might find this post via search). I've been reading abou that DialogBox function and tried to understand, why the window which is displayed using that function becomes modal, I was actually trying to read between the lines, there had to be something which triggered that effect, that's what I've been looking for. As I went deeper, I found out that dialog boxes simply disable the owner window , so what I actually wanted to hear in the reply, was this one line (two actually):

    Code:
    // add this line before calling the ShowWindow to open a modal window
    BOOL wasEnabled = EnableWindow(hOwner, FALSE); // disable the owner window
    
    // before calling function to close the child (modal) window, add this:
    EnableWindow(hOwner, wasEnabled);
    Usually, I think, it's pointless so save the previous state, since if you are able to *open* (read: click a button to open) another window, then the window is enabled, so you just write a pair:

    EnableWindow(hOwner, FALSE);
    [...]
    EnableWindow(hOwner, TRUE);

    Thanks again anyway,
    Oleg

  12. #12
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to create/display a modal window? And surplus of win32 code

    Quote Originally Posted by Xatrix
    Hi,

    I just want to answer my own question (for those who might find this post via search). I've been reading abou that DialogBox function and tried to understand, why the window which is displayed using that function becomes modal, I was actually trying to read between the lines, there had to be something which triggered that effect, that's what I've been looking for. As I went deeper, I found out that dialog boxes simply disable the owner window , so what I actually wanted to hear in the reply, was this one line (two actually):

    Code:
    // add this line before calling the ShowWindow to open a modal window
    BOOL wasEnabled = EnableWindow(hOwner, FALSE); // disable the owner window
    
    // before calling function to close the child (modal) window, add this:
    EnableWindow(hOwner, wasEnabled);
    Usually, I think, it's pointless so save the previous state, since if you are able to *open* (read: click a button to open) another window, then the window is enabled, so you just write a pair:

    EnableWindow(hOwner, FALSE);
    [...]
    EnableWindow(hOwner, TRUE);

    Thanks again anyway,
    Oleg
    No intend to offense, Oleg, but even some guys "who might find this post via search" will be enlighten by your DialogBox function researches, other ones will obseve that you still have a lot to dig and anyhow you did not paid too much attention to read all the answers from this thread and anyhow you don't know that not all people in this world can guess what exactly you want unless you exactly ask what you exactly want.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  13. #13
    Join Date
    May 2004
    Posts
    28

    Re: How to create/display a modal window? And surplus of win32 code

    Sorry, Ovidiu, but I fail to understand from your post what I said wrong. I did pay enough attention to posts, it's just that they were reffering to either MFC or using resource editor and DialogBox(), which I tried to avoid allthogether

  14. #14
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: How to create/display a modal window? And surplus of win32 code

    found this post via google.

    does anyone know how the message loop looks like inside the DialogBox function?

  15. #15
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to create/display a modal window? And surplus of win32 code

    Please do not wake up ancient threads!
    If you have a new problem, even somehow related, open a new thread.

    [ Thread closed ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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