How to create dialogbox with windows API

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

How to create dialogbox with windows API

Post by SemiconductorCat » Sat Apr 13, 2013 5:08 am

Hi all today I'm going to discuss you how to create dialog boxes using winapi.I'm
going to use the programming language as "C/C++" not only because most of you here
know it, but it's the only computer language that I could be comfortable with.

Modal and Modeless Dialog Boxes

Simply modal dialog box could block sending messages to other window's. There are two kind of modal
dialogs , one is application level modal dialogs and next is system wide modal dialogs.

In windows you may imagine sometimes , some dialog-popups will block you accessing from other windows.
Such windows are system modal dialog boxes. A good example to a system wide modal window is what's when
you press the ATL+CTL+DELETE (that shows list of options ,shutdown , task manager etc etc).
So yes a system

In application modal window, you only could block the windows that are belongs to the same thread.
There is a one application level message queue for every thread, so application wide modal windows/dialogs
only applicable to that application level message queue, but system modal window will block the messages
to other windows from the system message queue.

Today I'm not going to discuss about system wide modal dialogs, but application wide modal /modeless dialog
boxes.

Creating a Dialog Box.

In this tutorial I'm going to create a simple About Box, Something that looks like bellow. [The actual picture
is taken from my final year project for BIT].
Image


The easiest way to create a dialog box is to use a resource dialog template. So the code bellow illustrates the
resource file. Let's go line by line.

Code: Select all


/* about dialog box */
ID_DIALOG_ABOUT DIALOGEX 50, 50, 316, 183
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About "
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",ABOUT_OK,259,162,50,14
    CONTROL         "",ABOUT_IMAGE,"Static",SS_BITMAP,7,7,179,185
    LTEXT           "Developed By Undergard of Matrix Institute of  Information Technology (http://matrix-edu.com/)",ABOUT_STATIC,154,10,144,83
END
DS_SETFONT, DS_MODALFRAME, DS_FIXEDSYS are dialog box and window styles,
You could find what they actually means in here, http://msdn.microsoft.com/en-us/library ... 85%29.aspx

WS_POPUP , WS_CAPTION and WS_SYSMENU are window styles. As I already told you Dialog is also a window.
Simply CreateDialog() or DialogBox() are just abstractions built top of CreateWindow() call.
So simply here: http://msdn.microsoft.com/en-us/library ... 85%29.aspx
describes what they mean in here.

ABOU_IMAGE , ABOUT_OK, ABOUT_STATIC are just window identifiers. They are just constrains defined in the
bellow header file.

and in the resource.h file we have defined it's identifiers, as bellow.

Code: Select all


/* about dialog box */
#define ID_DIALOG_ABOUT 3001
#define ABOUT_OK        3002
#define ABOUT_IMAGE     3003
#define ABOUT_STATIC    3004
In resource.h file, you may wonder why I have started with 3001 ? The reason is I have skipped around 200
space of values for every resource, even the does not have that much of identifiers , that's is for future
extensions. For a instance, think you going to add a new button to an existing dialog box. Then you may
feel glad about what I did like this.

Note that I'm going to display a image on this About Box, so simply we need to use a "Static" control for that.
And we need to give it's style as SS_BITMAP,

NOTE: In windows naming conventions, they use SS_ prefix for constrains associated with static window controls.
WS_ for window styles, DS_ for dialog box styles. Practice this prefix convention and you may navigate easily
through the universe of WinAPI.



Then we need to create the dialog box, It is done by CreateDialog() or DialogBox() api calls. You could choose
one depending on whether you going to create a Modal or Modeless dialog box.Use CreateDialog() to create
modeless dialog boxes and DialogBox to modal dialog boxes.

In my final year project I have mentioned that dialog as a modal dialog box. So I'm using DialogBox.

One thing to you should also note here. Even this is a modal dialog box , I have always check whether
the dialog box already have been created or not. This is good for several reasons, I do always
suggest these kind of defensive programming principles.[always look guilty on the parameters passed to you].

Next thing is your dialog box needs a dialog box procedure.You could see it on source file bellow named
as "AboutDialogProc" , you also need to pass it's address when you calling the DialogBox().

Dialog box could have several messages, as I already told DialogBox is also an abstraction of a win32 window.
So all the window messages starting with prefix "WM_" are applicable here too. But fortunately the
default window procedure will take care about them when we return FALSE.

In this application I have decided to process WM_INITDIALOG message, so I could change the bitmap
of the bitmap static control there. Simply I have already loaded my bitmap before calling DialogBox(), so
I simply send the STM_SETIMAGE message to static child control with bitmap handle.

The child window could notify the parent window through the WM_COMMAND message. For a example
when the button is clicked, I have decided to delete the loaded bitmap and end the dialog there.
Please do not call DeleteObject() before calling EndDialog(), because it will delete a object in the
memory which is already using and it will leads to a memory access violation.

I have done the same thing in the WM_CLOSE message too. WM_CLOSE message will be send to the
window/dialog procedure when user presses the [X] close button on title bar or system menu.

Code: Select all

/*

  file: about dialog 
  
  author: [email protected]
  
 */
 #include "unicode.h"
 #include <windows.h>
 #include "resource.h"
 #include "about_dialog.h"
 
 static int is_created = 0;
 extern TCHAR szAppName [];
 extern HWND hwnd ;
 static HBITMAP hBitmap = NULL;
 HWND hwndImageCtl = NULL;
 
 INT_PTR CALLBACK AboutDialogProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) 
 {
   HDC hdc,hdcMem;
   switch ( message)
   {
     case WM_INITDIALOG:
	   	  SendDlgItemMessage(hwnd,ABOUT_IMAGE,STM_SETIMAGE,(LPARAM)IMAGE_BITMAP,(LPARAM)hBitmap);
	   
	      InvalidateRect(hwndImageCtl,NULL,FALSE);
	      return TRUE;

       case WM_COMMAND:
	      switch ( LOWORD(wParam) )
		  {
		    case ABOUT_OK:
			    
				EndDialog(hwnd,0);
				DeleteObject(hBitmap);
				is_created =0;
			    return TRUE;
			
		    default:
			  return FALSE;
		  
		  }
	    
          return FALSE; 
		case WM_CLOSE:
          EndDialog(hwnd,0);
          DeleteObject(hBitmap);
          is_created=0;
          return TRUE;  
      default: 
	    break;
		
   
   }
   return FALSE;
 }
 
 void AboutDialog_Show()
 {
   if ( is_created )
     MessageBox(NULL,L"About Dialog Have Been Already Created !" , szAppName , MB_OK);
   else 
   {
      hBitmap = (HBITMAP)LoadImage( NULL ,\
	         L".\\img\\matrix.bmp",IMAGE_BITMAP,179,185,LR_LOADFROMFILE |LR_DEFAULTCOLOR|LR_CREATEDIBSECTION );
	  if(hBitmap == NULL)
	   MessageBox(NULL,L"LoadImage have been failed" , szAppName, MB_OK);
	   
     is_created = 1;
	 DialogBox( GetModuleHandle(NULL),MAKEINTRESOURCE(ID_DIALOG_ABOUT),\
	                hwnd, AboutDialogProc); 
	 return;
   } 
 }

I suggest you to go through this code once,feel free to navigate to MSDN over and over. But review the
code completely.Even next day if you ask what that file's API call on line XXX will do, sometimes I may
not be able to answer it directly without lookup in API docs.Simply code reviewing is not memorizing the
API's but going through them and understand what program will do [actually run it from your mind] .


--happy coding and code reviewing--
Post Reply

Return to “C/C++ Programming”