accelerators

Discussion in 'Win32' started by johnsy2004, Feb 25, 2008.

  1. johnsy2004

    johnsy2004 New Member

    Joined:
    Feb 24, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    i am very confused.

    i know that i am using the correct functions and such but my accelerators do not work. i am using Dev-C++ to compile and edit. my source files look like this:

    main.cpp
    Code:
    #include <windows.h>
    #include "resources.h"
    #include "main.h"
    
    LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg,
                                 WPARAM wParam, LPARAM lParam);
    
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	MSG   Msg;
    	char *ClsName = "Win32OOP";
    	char *WndName = "Object-Oriented Win32 Programming";
    
    	// Initialize the application class
    	WApplication WApplication(hInstance, ClsName, MainWndProc, MAKEINTRESOURCE(IDR_MENU));
    	WApplication.Register();
    
    	// Create the main window
    	WWindow Wnd;
        Wnd.Create(hInstance, ClsName, WndName, NULL, WS_OVERLAPPEDWINDOW);
    	// Display the main winow
        HACCEL hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_MENU));
        Wnd.Show(SW_SHOWNORMAL, hInstance);
    	
    	while ( GetMessage(&Msg, NULL, 0, 0))
    {
            if (!TranslateAccelerator(Wnd.operator HWND(), hAccel, &Msg))
                TranslateMessage(&Msg);
                
                DispatchMessage(&Msg);
              
        }
        return 0;
    }
    
    LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg,
                                 WPARAM wParam, LPARAM lParam)
    {
    	switch(Msg)
    	{
                   case WM_COMMAND:{
                        switch(wParam){
                                       case CMD_FILE_EXIT:{
                                            PostQuitMessage(WM_QUIT);
                                            }break;
                                       case CMD_FILE_OPEN:{
                                            MessageBox(NULL, "hello", "hello", MB_ICONERROR|MB_OK);
                                            }break;
                             }
                   }break;
    	case WM_DESTROY:{
    		PostQuitMessage(WM_QUIT);
        }break;
        
    		return 0;
    	}
    
    	return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    
    main.h

    Code:
    #include <windows.h>
    
    class WApplication {
          public:
                 WApplication (HINSTANCE hInstance, char *classname,
                              WNDPROC WndProc, LPCTSTR MenuName = NULL);
                              
                 void Register();
                 
          protected:
                    WNDCLASSEX _WndClsEx;
                        
    };
    
    WApplication::WApplication(HINSTANCE hInstance, char *classname,
                               WNDPROC WndProc, LPCTSTR MenuName)
    {
        _WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    	_WndClsEx.style         = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
    	_WndClsEx.lpfnWndProc   = WndProc;
    	_WndClsEx.cbClsExtra    = 0;
    	_WndClsEx.cbWndExtra    = 0;
    	_WndClsEx.hInstance     = hInstance;
    	_WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    	_WndClsEx.hCursor       = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_MYCURSOR));
    	_WndClsEx.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
    	_WndClsEx.lpszMenuName  = MenuName;
    	_WndClsEx.lpszClassName = classname;
    	_WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    }
    
    void WApplication::Register()
    {
         RegisterClassEx(&_WndClsEx);
         }
    
    class WWindow{
          public:
                 WWindow();
                 
                 HWND Create(HINSTANCE hinst,
                             LPCTSTR clsname,
         		             LPCTSTR wndname,
    	                     HWND parent   = NULL,
    	                     DWORD dStyle  = WS_BORDER | WS_SYSMENU | WS_CAPTION,
    	                     DWORD dXStyle = 0,
    		                 int x         = CW_USEDEFAULT,
                             int y         = CW_USEDEFAULT,
    		                 int width     = CW_USEDEFAULT,
                             int height    = CW_USEDEFAULT);
            
                 BOOL Show(int dCmdShow, HINSTANCE hInstance);
                 HACCEL accelreturn();
                 int error();
                 
                 operator HWND();
                       
          protected:
                    HWND _hwnd;
                    HACCEL hAccel;        
    };
    
    WWindow::WWindow(){
                       
                       _hwnd = NULL;
                       
                       }
    
    HWND WWindow::Create(HINSTANCE hinst,
                    LPCTSTR clsname,
                    LPCTSTR wndname,
                    HWND parent,
                    DWORD dStyle,
                    DWORD dXStyle,
                    int x,
                    int y,
                    int width,
                    int height){
                        
                        _hwnd = CreateWindowEx(dXStyle,
                                clsname,
                                wndname,
                                dStyle,
                                x,
                                y, 
                                width, 
                                height, 
                                parent, 
                                NULL, 
                                hinst, 
                                NULL);
                                
                        if(_hwnd != NULL)
                        return _hwnd;
                        
                        return NULL;
    }
                        
    BOOL WWindow::Show(int dCmdShow, HINSTANCE hInstance){           
                        if (ShowWindow(_hwnd, dCmdShow) && UpdateWindow(_hwnd))
                        return true;
                        
                        return false;
    }
    
    WWindow::operator HWND(){
                      return _hwnd;
    }
    
    int WWindow::error(){
                     MessageBox(NULL, "Could Not Start Properly.", "ERROR!", MB_OK|MB_ICONERROR);
                     
                     return 0;
                     }
    HACCEL WWindow::accelreturn(){
           
           return hAccel;
           }
    
    
    resources.h


    Code:
    #define IDR_MENU 2000
    #define CMD_FILE_NEW 2001
    #define CMD_FILE_EXIT 2002
    #define CMD_FILE_OPEN 2003
    #define CMD_HELP_CONTENTS 2004
    #define IDD_DIALOGBOX 2010
    #define IDD_LISTBOX 2011
    #define IDA_ACCELERATOR 2100
    #define IDC_MYCURSOR 2005
    
    resources.rc

    Code:
    #include "resources.h"
    
    IDC_MYCURSOR CURSOR DISCARDABLE "golden.cur"
    
    IDR_MENU MENU DISCARDABLE
    BEGIN
         POPUP "&File"
               BEGIN
               MENUITEM "&New", CMD_FILE_NEW
               MENUITEM "&Open...", CMD_FILE_OPEN
               MENUITEM SEPARATOR
               MENUITEM "E&xit", CMD_FILE_EXIT
         END
         POPUP "&Help"
               BEGIN
               MENUITEM "&Contents...", CMD_HELP_CONTENTS
         END
    END
    
    IDR_MENU ACCELERATORS DISCARDABLE
    BEGIN
    "O", CMD_FILE_OPEN, VIRTKEY, CONTROL, NOINVERT
    END
    
    
    any ideas? thanks in advance.

    johnsy2004.


    P.S sorry about the long post :D
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice