⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ltweight.cpp

📁 Window Wrapper for WinCE Win32 "Hello World" a
💻 CPP
字号:
//***************************************************************************
//  LtWeight.cpp
//
//  This is a WinCE application using the Win32 API.  The difference between
//  this and a simple "hello" program is the object wrapper for each window.
//
//  Requirements:
//    1. create a simple Win32 app
//    2. Create the main window using a call to CreateWindow or a variant
//    3. Create a simple base class that allows the use of objects
//    4. Allow child windows to inherit from the base class
//  Secondary Goals:
//
//  Known Problems:
//***************************************************************************

#include "stdafx.h"
#include "LtWeight.h"
#include <commctrl.h>
#include <aygshell.h>
#include <sipapi.h>

#define MAX_LOADSTRING 100

static SHACTIVATEINFO s_sai;

CMainWindow MainWindow;


//***************************************************************************
//  FUNCTION: WinMain
//
//  PURPOSE:  Entry point and Main Message Loop.
//
//  COMMENTS:
//***************************************************************************
int WINAPI WinMain( HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    int       nCmdShow )
{
   MSG msg;
   HACCEL hAccelTable;

   // Perform application initialization
   if ( !MainWindow.InitInstance( hInstance, nCmdShow ) )
   {
      return FALSE;
   }

   hAccelTable = ::LoadAccelerators( hInstance, (LPCTSTR)IDC_LTWEIGHT );

   // Main message loop
   int status;
   while ( ( status = GetMessage( &msg, NULL, 0, 0 ) ) != 0 )
   {
      if ( status == -1 )
         return -1;
      if ( !TranslateAccelerator( msg.hwnd, hAccelTable, &msg ) )
      {
         TranslateMessage( &msg );
         DispatchMessage( &msg );
      }
   }

   return msg.wParam;
}   /*   WinMain   */


//***************************************************************************
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE:  Registers the window class.
//
//  COMMENTS:
//    It is important to call this function so that the application 
//    will get 'well formed' small icons associated with it.
//***************************************************************************
ATOM CMainWindow::MyRegisterClass( HINSTANCE hInstance, LPTSTR szWindowClass )
{
   WNDCLASS wc;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = (WNDPROC) BaseWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LTWEIGHT));
    wc.hCursor       = 0;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName  = 0;
    wc.lpszClassName = szWindowClass;

   return RegisterClass( &wc );
}   /*   MyRegisterClass   */


//***************************************************************************
//  FUNCTION: InitInstance
//
//  PURPOSE:  Saves instance handle and creates main window
//
//  COMMENTS:
//    In this function, we save the instance handle.  Load the app strings
//    for title and class.  Switch to an already loaded instance of this app
//    if one is already found running.  Create and display the main window.
//***************************************************************************
BOOL CMainWindow::InitInstance( HINSTANCE hInstance, int nCmdShow )
{
   HWND  hWnd = NULL;
   TCHAR szTitle[MAX_LOADSTRING];            // The title bar text
   TCHAR szWindowClass[MAX_LOADSTRING];      // The window class name

   hwndCB = 0;
   hInst = hInstance;      // Store instance handle in our global variable

   // Initialize global strings
   LoadString( hInstance, IDC_LTWEIGHT, szWindowClass, MAX_LOADSTRING );
   LoadString( hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING );

   // If it is already running, then focus on the window
   hWnd = FindWindow(szWindowClass, szTitle);   
   if ( hWnd ) 
   {
      SetForegroundWindow ((HWND) (((DWORD)hWnd) | 0x01));    
      return 0;
   } 

   MyRegisterClass( hInstance, szWindowClass );
   
   RECT  rect;
   GetClientRect( hWnd, &rect );
   
   hWnd = CreateWindow( szWindowClass, szTitle, WS_VISIBLE,
                        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                        CW_USEDEFAULT, NULL, NULL, hInstance, (void *)this );
   if ( !hWnd )
   {  
      return FALSE;
   }
   // When the main window is created using CW_USEDEFAULT the height of the menubar (if one
   // is created is not taken into account). So we resize the window after creating it
   // if a menubar is present
   {
      RECT rc;
      GetWindowRect( hWnd, &rc );
      rc.bottom -= MENU_HEIGHT;
      if ( hwndCB )
         MoveWindow( hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE );
   }

   ShowWindow( hWnd, nCmdShow );
   UpdateWindow( hWnd );

   return TRUE;
}   /*   InitInstance   */


//***************************************************************************
//  FUNCTION: CreateRpCommandBar
//
//  PURPOSE:  Create the Menu Bar
//
//  Comments:
//    MenuBar became CommandBar in CE 3.0 when additional functionality
//    was added.
//***************************************************************************
HWND CMainWindow::CreateRpCommandBar( HWND hwnd )
{
   SHMENUBARINFO mbi;

   memset( &mbi, 0, sizeof( SHMENUBARINFO ) );
   mbi.cbSize     = sizeof( SHMENUBARINFO );
   mbi.hwndParent = hwnd;
   mbi.nToolBarId = IDM_MENU;
   mbi.hInstRes   = hInst;
   mbi.nBmpId     = 0;
   mbi.cBmpImages = 0;

   if ( !SHCreateMenuBar( &mbi ) ) 
      return NULL;

   return mbi.hwndMB;
}   /*   CreateRpCommandBar   */


//***************************************************************************
//  FUNCTION: BaseWndProc
//
//  PURPOSE:  Pass the callback message on to the window owner.
//
//  COMMENTS:
//    All Windows Message Loops can be inherited from this method.  When it
//    gets a message, the pointer to the owning object is retrieved and the
//    message is sent to the Window Procedure for that object.
//
//    On entry the object pointers are saved as the windows are created
//    along with win handle and the instance.
//
//    Upon exit it is decided if a call needs to be made to the Default
//    Window Procedure (DefWindowProc).  This way all of the WndProcs can
//    be the same coding style regardless of window type (window vs dialog).
//***************************************************************************
LRESULT CALLBACK CBaseWindow::BaseWndProc( HWND hwnd, UINT msg,
                                           WPARAM wParam, LPARAM lParam )
{
   // check to see if a copy of the 'this' pointer needs to be saved
   if ( msg == WM_CREATE )
   {
      CBaseWindow *pObj = reinterpret_cast<CBaseWindow *>((long)((LPCREATESTRUCT)lParam)->lpCreateParams);
      ::SetWindowLong( hwnd, GWL_USERDATA, (LONG)((LPCREATESTRUCT)lParam)->lpCreateParams );
      pObj->hWnd = hwnd;
      pObj->DlgFlag = FALSE;
      if ( pObj->hInst == 0 )
         pObj->hInst = MainWindow.hInst;
   }
   if ( msg == WM_INITDIALOG )
   {
      CBaseWindow *pObj = reinterpret_cast<CBaseWindow *>(lParam);
      ::SetWindowLong( hwnd, GWL_USERDATA, lParam );
      pObj->hWnd = hwnd;
      pObj->DlgFlag = TRUE;
      pObj->hInst = MainWindow.hInst;
   }

   BOOL bProcessed = FALSE;
   LRESULT lResult;

   // Retrieve the pointer
   CBaseWindow *pObj = WinGetLong<CBaseWindow *>( hwnd, GWL_USERDATA );

   // Filter message through child classes
   if ( pObj )
      lResult = pObj->WndProc( hwnd, msg, wParam, lParam, &bProcessed );
   else
      return ( pObj->DlgFlag ? FALSE : TRUE ); // message not processed

   if ( pObj->DlgFlag )
      return bProcessed;     // processing a dialog message return TRUE if processed
   else
      if ( !bProcessed )
         // If message was unprocessed and not a dialog, send it back to Windows.
         lResult = DefWindowProc( hwnd, msg, wParam, lParam );

   return lResult;           // processing a window message return FALSE if processed
}   /*   BaseWndProc   */


//***************************************************************************
//  FUNCTION: WndProc
//
//  PURPOSE:  Processes messages for the main window.
//
//  COMMENTS:
//***************************************************************************
LRESULT CMainWindow::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, PBOOL pbProcessed )
{
   HDC hdc;
   int wmId, wmEvent;
   PAINTSTRUCT ps;
   TCHAR szHello[MAX_LOADSTRING];

   // call the base class first
   LRESULT lResult = CBaseWindow::WndProc( hWnd, message, wParam, lParam, pbProcessed );
   BOOL bWasProcessed = *pbProcessed;
   *pbProcessed = TRUE;

   switch ( message )
   {
      case WM_COMMAND:
         wmId    = LOWORD(wParam); 
         wmEvent = HIWORD(wParam); 
         // Parse the menu selections:
         switch (wmId)
         {  
            case IDM_HELP_ABOUT:
               {
                  CAboutWindow *pAboutWindow = new( CAboutWindow );
                  DialogBoxParam( GetInstance(), (LPCTSTR)IDD_ABOUTBOX, hWnd,
                                  (DLGPROC)pAboutWindow->BaseWndProc, (long)pAboutWindow );
                  delete pAboutWindow;
               }
               break;
            case IDOK:
            case IDM_EXIT:
               SendMessage( hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd );
               SendMessage ( hWnd, WM_CLOSE, 0, 0 );
               break;
            default:
               // the message was not processed
               // indicate if the base class handled it
               *pbProcessed = bWasProcessed;
               lResult = FALSE;
               return lResult;
         }
         break;
      case WM_CREATE:
         hwndCB = CreateRpCommandBar( hWnd );
         break;
      case WM_PAINT:
         RECT rt;
         hdc = BeginPaint( hWnd, &ps );
         GetClientRect( hWnd, &rt );
         LoadString( GetInstance(), IDS_HELLO, szHello, MAX_LOADSTRING );
         DrawText( hdc, szHello, _tcslen( szHello ), &rt, 
                   DT_SINGLELINE | DT_VCENTER | DT_CENTER );
         EndPaint( hWnd, &ps );
         break; 
      case WM_DESTROY:
         CommandBar_Destroy( hwndCB );
         PostQuitMessage( 0 );
         break;
      case WM_SETTINGCHANGE:
         SHHandleWMSettingChange( hWnd, wParam, lParam, &s_sai );
         break;
      default:
         // the message was not processed
         // indicate if the base class handled it
         *pbProcessed = bWasProcessed;
         lResult = FALSE;
         return lResult;
   }
   return lResult;
}   /*   CMainWindow::WndProc   */


//***************************************************************************
//  FUNCTION: CAboutWindow::WndProc
//
//  PURPOSE:  Mesage handler for the About box.
//
//  Comments:
//***************************************************************************
LRESULT CAboutWindow::WndProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam, PBOOL pbProcessed )
{
   SHINITDLGINFO shidi;

   // call the base class first
   LRESULT lResult = CBaseWindow::WndProc( hDlg, message, wParam, lParam, pbProcessed );
   BOOL bWasProcessed = *pbProcessed;
   *pbProcessed = TRUE;

   switch ( message )
   {
      case WM_INITDIALOG:
         // Create a Done button and size the dialog full screen.  
         shidi.dwMask = SHIDIM_FLAGS;
         shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
         shidi.hDlg = hDlg;
         SHInitDialog( &shidi );
         break; 

      case WM_COMMAND:
         if ( LOWORD(wParam) == IDOK )
         {
            EndDialog( hDlg, LOWORD( wParam ) );
            return TRUE;
         }
         // not processed, so just fall through here.

      default:
         // the message was not processed
         // indicate if the base class handled it
         *pbProcessed = bWasProcessed;
         lResult = FALSE;
         return lResult;
   }
   return lResult;
}   /*   CAboutWindow::WndProc   */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -