Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Accelerators

My win main goes like this:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpszCmdLine, int cmdShow)
{
      InitCommonControls() ;

      DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc) ;

      return FALSE ;
}


Because I have a modal box and I have any message loop, I'm not able to catch my accelerators.

What am I suppose to do??
0
asimov_
Asked:
asimov_
  • 6
  • 5
1 Solution
 
asimov_Author Commented:
Moreover, I'm not able to have a modeless box because I'm not able to dispatch my message...
0
 
nietodCommented:
In the dialog procedure, call TranslateAccelerator on WM_KEYDOWN and WM_SYSKEYDOWN messages.  (You will have to "make" a MSG structure out of the message and parameters passed to the dialgo procedure.  

Let me know if you have any questions.
0
 
asimov_Author Commented:
Hello,
 Your answer is good, but maybe my own question wasn't clear enough! Sorry, it's my fault. I'll give to you an entire program code from Petzold book. Goes like this :

/*----------------------------------------
   HEXCALC.C -- Hexadecimal Calculator
                (c) Charles Petzold, 1996
  ----------------------------------------*/

#include <windows.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
     {
     static char  szAppName[] = "HexCalc" ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASSEX   wndclass ;

     wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = DLGWINDOWEXTRA ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     wndclass.hIconSm       = LoadIcon (hInstance, szAppName) ;

     RegisterClassEx (&wndclass) ;

     hwnd = CreateDialog (hInstance, szAppName, 0, NULL) ;

     ShowWindow (hwnd, iCmdShow) ;

     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
     }

void ShowNumber (HWND hwnd, UINT iNumber)
     {
     char szBuffer[20] ;

     SetDlgItemText (hwnd, VK_ESCAPE, strupr (ltoa (iNumber, szBuffer, 16))) ;
     }

DWORD CalcIt (UINT iFirstNum, int iOperation, UINT iNum)
     {
     switch (iOperation)
          {
          case '=' : return iNum ;
          case '+' : return iFirstNum +  iNum ;
          case '-' : return iFirstNum -  iNum ;
          case '*' : return iFirstNum *  iNum ;
          case '&' : return iFirstNum &  iNum ;
          case '|' : return iFirstNum |  iNum ;
          case '^' : return iFirstNum ^  iNum ;
          case '<' : return iFirstNum << iNum ;
          case '>' : return iFirstNum >> iNum ;
          case '/' : return iNum ? iFirstNum / iNum : UINT_MAX ;
          case '%' : return iNum ? iFirstNum % iNum : UINT_MAX ;
          default  : return 0 ;
          }
     }

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     {
     static BOOL  bNewNumber = TRUE ;
     static int   iOperation = '=' ;
     static UINT  iNumber, iFirstNum ;
     HWND         hButton ;

     switch (iMsg)
          {
          case WM_KEYDOWN :                   // left arrow --> backspace
               if (wParam != VK_LEFT)
                    break ;
               wParam = VK_BACK ;
                                             // fall through
          case WM_CHAR :
               if ((wParam = toupper (wParam)) == VK_RETURN)
                    wParam = '=' ;

               hButton = GetDlgItem (hwnd, wParam) ;

               if (hButton != NULL)
                    {
                    SendMessage (hButton, BM_SETSTATE, 1, 0) ;
                    SendMessage (hButton, BM_SETSTATE, 0, 0) ;
                    }
               else
                    {
                    MessageBeep (0) ;
                    break ;
                    }
                                             // fall through
          case WM_COMMAND :
               SetFocus (hwnd) ;

               if (LOWORD (wParam) == VK_BACK)         // backspace
                    ShowNumber (hwnd, iNumber /= 16) ;

               else if (LOWORD (wParam) == VK_ESCAPE)  // escape
                    ShowNumber (hwnd, iNumber = 0) ;

               else if (isxdigit (LOWORD (wParam)))    // hex digit
                    {
                    if (bNewNumber)
                         {
                         iFirstNum = iNumber ;
                         iNumber = 0 ;
                         }
                    bNewNumber = FALSE ;

                    if (iNumber <= UINT_MAX >> 4)
                         ShowNumber (hwnd, iNumber = 16 * iNumber + wParam -
                              (isdigit (wParam) ? '0' : 'A' - 10)) ;
                    else
                         MessageBeep (0) ;
                    }
               else                                    // operation
                    {
                    if (!bNewNumber)
                         ShowNumber (hwnd, iNumber =
                              CalcIt (iFirstNum, iOperation, iNumber)) ;
                    bNewNumber = TRUE ;
                    iOperation = LOWORD (wParam) ;
                    }
               return 0 ;

          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;
          }
     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
     }


And now, the .rc file :
/*----------------------------
   HEXCALC.RC resource script
  ----------------------------*/

#include <windows.h>

HexCalc ICON hexcalc.ico

HexCalc DIALOG 32768, 0, 102, 122
     STYLE WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
     CLASS "HexCalc"
     CAPTION "Hex Calculator"
     {
     PUSHBUTTON "D",       68,  8,  24, 14, 14
     PUSHBUTTON "A",       65,  8,  40, 14, 14
     PUSHBUTTON "7",       55,  8,  56, 14, 14
     PUSHBUTTON "4",       52,  8,  72, 14, 14
     PUSHBUTTON "1",       49,  8,  88, 14, 14
     PUSHBUTTON "0",       48,  8, 104, 14, 14
     PUSHBUTTON "0",       27, 26,   4, 50, 14
     PUSHBUTTON "E",       69, 26,  24, 14, 14
     PUSHBUTTON "B",       66, 26,  40, 14, 14
     PUSHBUTTON "8",       56, 26,  56, 14, 14
     PUSHBUTTON "5",       53, 26,  72, 14, 14
     PUSHBUTTON "2",       50, 26,  88, 14, 14
     PUSHBUTTON "Back",     8, 26, 104, 32, 14
     PUSHBUTTON "C",       67, 44,  40, 14, 14
     PUSHBUTTON "F",       70, 44,  24, 14, 14
     PUSHBUTTON "9",       57, 44,  56, 14, 14
     PUSHBUTTON "6",       54, 44,  72, 14, 14
     PUSHBUTTON "3",       51, 44,  88, 14, 14
     PUSHBUTTON "+",       43, 62,  24, 14, 14
     PUSHBUTTON "-",       45, 62,  40, 14, 14
     PUSHBUTTON "*",       42, 62,  56, 14, 14
     PUSHBUTTON "/",       47, 62,  72, 14, 14
     PUSHBUTTON "%",       37, 62,  88, 14, 14
     PUSHBUTTON "Equals",  61, 62, 104, 32, 14
     PUSHBUTTON "&&",      38, 80,  24, 14, 14
     PUSHBUTTON "|",      124, 80,  40, 14, 14
     PUSHBUTTON "^",       94, 80,  56, 14, 14
     PUSHBUTTON "<",       60, 80,  72, 14, 14
     PUSHBUTTON ">",       62, 80,  88, 14, 14
     }




My question is : I would like to use a dialog box like in this example. I want a class definition because I need a message loop. However, with this technic gave by Petzold, I'm not able to insert a menu and an accelerators table to my programm. I would like that you give me the example above with the code to insert menu and accelerators. Test it, because, those solutions aren't good:
wndclass.lpszMenuName = "menu" ;
LoadMenu... ;
Give the menu name to dialog ressource ;

If you're able to give me an answer, I'll consider you like the most famous et bright programmer on earth!!!

LAst two months, I tried to have a program with a window defined in vc's ressource, a menu, and accelerators!

Please help me,

Asimov_

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
nietodCommented:
I can not follow you.  Are you saying you want your program to be a dialog box program (just one top-level window that is a dialog box) or do you want your program to be a regular window program  (one with a mesage loop.)
0
 
asimov_Author Commented:
I want a program to be a regular window program (one qith message loop). But, my window program must be design into vc's resources (insert dialog box). Moreover, I want to use menu and accelerators. And I don't know how to match those three caracteristics.
0
 
nietodCommented:
I don't think there is any way to use VC's dialgo box editing features to create a regular window.   They are used to create a "dialog template resource" which consists of information use to construct the dialog window and its child windows (controls).   Unfortunately windows does not provide any means for you to use that type of resource to build a regular window.

However, you could write a procedure that interprets this resource and builds the main window and its child windows for you.   If you want to go that route, look at the documentation for DLGTEMPLATE and DLGITEMTEMPLATE these define the structure of the resource.
0
 
asimov_Author Commented:
k, thanks for your answer but if you're looking for the example I gave you. Those dialog template ressource are used for the principal window. Take a look at this and give me your comments.


0
 
nietodCommented:
>>Those dialog template ressource are used for the principal window.
They ARE used?  or you WANT to use them?  
0
 
asimov_Author Commented:
I would like to do exactly like this example, but with a menu  and accelerators. (Sorry, if you don't understand all i'm saying, because english is not my firts language).

I'll try to explain myself: in this example, we can see that a principal window is created with createdialog(). To be created, all childs windows is defined in the .rc file. Then, with a unique call of createdialog(), the window and all childs windows are created.

That's what I want to do. I want to describe another window and another childs (in the .rc file) for a new program. Moreover, to create my principal window and all childs windows, I want to use createdialog(). Then, that's what i'm not able to: I would like to put in my program a menu and accelerators.

Thanks for your patience.
0
 
asimov_Author Commented:
I'll ask my question differently!
Take the example that i gave you and you only add the code to insert a menu and accelerators (please test it to be sure that's ok). It's all i want to do.
0
 
nietodCommented:
What you are talking about doing is a lot of work.  I do not have that sort of time.  Windows provides no mechansim for what you want, you will have to create it yourself, or give up on this approach.  Take a look at my 2nd paragraph of comments on Jan 18th at 1:22.  You will have to write a procedure that will  parse the dialog template resources and build the windows from the information in the resource.  Windows does this for you when it creates a dialog, but does not do it for you for a regular window.  You will have to do it yourself, or give up.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now