CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    How to Activate WM_CHAR Message with Edit Box (from DIALOG)

    I try to build one application as following:

    The Main Window With Window Class = CLASS1 ; and With Window Proc =wnproc()

    Then I create a dialog (Child window) By Using Function : CreateDialog(..,..,..) with window proc = wndProcDlg().

    Then I add one Edit Box Class inside the dialog;

    My Problem is when I add WM_CHAR inside window Proc of Dialog , It is not working!!??

    Then I do not know how can I manage the character that I want to let user add inside Edit Box ;

    I want to add in Edit box only 5 Digit with floating point (Example : 33.25)

    Kindly see the code and Help

    Code:
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Design RC Rectangle Section as Per ACI 318-11 Code
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    #include "stdafx.h"
    #include "Win32Project11.h"
    #include <string>
    #include <math.h>
    #include <wchar.h>
    //#include <stdlib.h>
    
    
    #define MAX_LOADSTRING 100
    
    // Global Variables:
    HINSTANCE hInst;								// current instance
    TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
     static HWND hdlgmodeles;
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Globale Variable Define Concrete Section ~~~~~~~~~~~~~~~~~~
     int Width  ; // Width of Section
     int Height ;  // Height of Section
     int fc     ;  // Strength of concrete ACI code
     int Fy     ;  // Strength of Reinforcemnt	ACI code
     double  Mu  ;  // Moment  Mu of Load
     double Qu   ;  //	 Shear Strngth 
     
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Forward declarations of functions included in this code module: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ATOM				MyRegisterClass(HINSTANCE hInstance);
    BOOL				InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);//wind Proc of Main Window
    INT_PTR CALLBACK	wndprocDesign(HWND, UINT, WPARAM, LPARAM);//Wind Proc of Dlg
    
    int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                         _In_opt_ HINSTANCE hPrevInstance,
                         _In_ LPTSTR    lpCmdLine,
                         _In_ int       nCmdShow)
    {
    	UNREFERENCED_PARAMETER(hPrevInstance);
    	UNREFERENCED_PARAMETER(lpCmdLine);
    
     	// TODO: Place code here.
    	MSG msg;
    	HACCEL hAccelTable;
    	  
    	// Initialize global strings
    	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    	LoadString(hInstance, IDC_WIN32PROJECT11, szWindowClass, MAX_LOADSTRING);
    	MyRegisterClass(hInstance);
    
    	// Perform application initialization:
    	if (!InitInstance (hInstance, nCmdShow))
    	{
    		return FALSE;
    	}
    
    	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT11));
    
    	// Main message loop:									  
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    	
    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	} 
    
         }
    
    	return (int) msg.wParam;
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    
    	wcex.style			= CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc	= WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInstance;
    	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT11));
    	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_WIN32PROJECT11);
    	wcex.lpszClassName	= szWindowClass;
    	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    	return RegisterClassEx(&wcex);
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
    
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
    
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Wind Proc of Main Window Class
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    	//SendMessage(hWnd,IDM_ABOUT,0,0);
    	 
    	switch (message)
    	{
    	 case WM_KEYDOWN:
         case WM_KEYUP:
         case WM_CHAR:
    		 int a;
    		 a= wParam;
    
    		break;
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		// Parse the menu selections:
    		switch (wmId)
    		{
    
    							
    	break;
    
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:
    		if (hdlgmodeles ==NULL)
    		{
     
    	        hdlgmodeles=	CreateDialog(hInst,MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, wndprocDesign);	   
            	ShowWindow(hdlgmodeles,SW_SHOW);
    		}
    		hdc = BeginPaint(hWnd, &ps);
    
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~~~~WndProc for Dialog Box ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    BOOL CALLBACK wndprocDesign(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
    	UNREFERENCED_PARAMETER(lParam);
    	switch (message)
    	{
    	
    	case WM_INITDIALOG:
    			
    		int iD;iD=GetDlgCtrlID(hDlg);
    	//   SendDlgItemMessageA(hDlg,EDIT_HEIGHT,ES_CENTER,wParam,lParam);	
    	    return (INT_PTR)TRUE;
    	
        case WM_CHAR:
                    /// Here is problem WM_CHAR NOT WORKING ??!!!
    	   break;
    
    	case WM_COMMAND:
    			  
    	   
    		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    		{		
    			TCHAR txtHeight[10]  ;	  // Get Height from Text Box   			       TCHAR txtWidth [10]  ;    // Get Width  from Text Box		 						
    			TCHAR txtFc[10]      ;   //  Get fc     from Text Box   
    			TCHAR txtFy[10]      ;   //	 Get Fy     from Text Box   
    			TCHAR txtMu[10]      ;  //   Get Mu     from Text Box      
    			TCHAR txtQu[10]      ;  //   Get Qu     from Text Box   
    					
    		    	GetDlgItemText(hDlg,EDIT_HEIGHT,txtHeight,10);	  // 
    			    GetDlgItemText(hDlg,EDIT_WIDTH,txtWidth,  10);	  //
    			    GetDlgItemText(hDlg,EDIT_Fc,txtFc,        10);	  // G
    			    GetDlgItemText(hDlg,EDIT_Fy,txtFy,        10);	  // G
    			    GetDlgItemText(hDlg,EDIT_Mu,txtMu,        10);	  //
    				GetDlgItemText(hDlg,EDIT_Qu,txtQu,        10);    // 
    
    				
       
    				Height = _wtof(txtHeight);  // To convert TCHAR to // /
    				Width  = _wtof(txtWidth );  // To convert TCHAR 	
    				fc     = _wtof(txtFc    );
    				Fy     = _wtof(txtFy    );
    				Mu     = _wtof(txtMu    );
    				Qu     = _wtof(txtQu    );
    
    
    		    	return (INT_PTR)TRUE;
    		}
    		break;		  
    	}
    
    	return 0;
    }

  2. #2
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: How to Activate WM_CHAR Message with Edit Box (from DIALOG)

    I got solution for first problem
    To Set the no of Char in Edit Box Use this Function;
    SendDlgItemMessageA(hDlg,EDIT_BOX1,EM_LIMITTEXT,4,lParam);

    Here I Put No of Char of Edit_Box1 as Example wParam = 4 , in this case the Edit Box will recieve only 4 Char

  3. #3
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: How to Activate WM_CHAR Message with Edit Box (from DIALOG)

    I hope Igor , Marc Gregoire and VictorN will provide us a solution for this subject..

    My Best Regards

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

    Re: How to Activate WM_CHAR Message with Edit Box (from DIALOG)

    It's time to start using MSDN professionally, don't you think that?

    WM_CHAR message :
    Posted to the window with the keyboard focus when a WM_KEYDOWN message is translated by the TranslateMessage function. The WM_CHAR message contains the character code of the key that was pressed.
    Dialog window itself almost never gets the focus. So, wrong place for the message handling.
    Last edited by Igor Vartanov; October 20th, 2012 at 01:31 AM.
    Best regards,
    Igor

  5. #5
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: How to Activate WM_CHAR Message with Edit Box (from DIALOG)

    Thanks Igor for your perfect answer , But is there any tool to manage the Input Char ,

    I Understood that Dialog Box Manage only the the Keyboard interface for Few Keys Only as Example (Enter ,Alt,Space) But not All KeyBoard ...

    But My Problem is not solved , is there any solution to manage KeyBorad inputs throw wind Proc of Dialog Box...

    Kindly Accept My Best Regards.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to Activate WM_CHAR Message with Edit Box (from DIALOG)

    Victor Nijegorodov

  7. #7
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: How to Activate WM_CHAR Message with Edit Box (from DIALOG)

    Thanks Victor For your answers , After searching for this subject and as per Igor Answer also ,
    Modeles Dialog can not recieve WM_CHAR Message ,

    And I can suggest some ideas to manage CHAR ,

    1. The User can get data from Edit Box By Using function GetDlgItemText() ,
    and then The Program can Check the Inut Char , if it valid ok, if not Valid the Program can send message to user to
    input data again

    2. The user can use these properties to control the input
    as Example , assume there is Dialog with handle hDlg ,
    to let user inpit only 6 CHAR in edit box EDIT_Box1 as example the user can use this function:

    SendDlgItemMessageA(hDlg,EDIT_Box1,EM_LIMITTEXT,6 ,lParam);

    Here we send Message to dialog Box hDlg , the EDIT_Box item will recieve message EM_LIMITTEXT and will put
    lParam =6 i.e Edit_Box1 will let user to input 6 Chars in edit Box.

    if we want to let EDIT_Box1 recieves only Numbers ,as example 2357 ; we can write this message;

    SendDlgItemMessageA(hDlg,EDIT_HEIGHT,ES_NUMBER,wParam,lParam);

    Note , We can set this properties also from *.rc File of dialog

    3. the 3rd Idea , the programmer can add a set of Buttons
    and let user input chars only from these Buttons, as example the Calculator Application,
    From this idea , the user has only limited set of chars that he can input to Edit Box..

    These are three ideas to Mangae Chars from Dialog Box...

    Finally I send my best regards to Igor , and Victor for their helping and answers...

    Regards..

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