1 / 15

Windows

Windows The Fun Stuff! MSG BOOL HWND HANDLE WNDCLASS LRESULT WPARAM LPARAM CALLBACK UINT New Data Types New Files #include <windows.h> Resource Files *.rc Custom Header Files *.h New Classes WNDCLASS structure declaration (data type) in windows.h

Gabriel
Télécharger la présentation

Windows

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Windows The Fun Stuff!

  2. MSG BOOL HWND HANDLE WNDCLASS LRESULT WPARAM LPARAM CALLBACK UINT New Data Types

  3. New Files • #include <windows.h> • Resource Files *.rc • Custom Header Files *.h

  4. New Classes • WNDCLASS • structure declaration (data type) in windows.h • contains members and pointers to functions for a basic window.

  5. Encapsulation • Combining data and the functions that process the data into a single entity. • “Data” refers to the members of a structure or CLASS that hold data. • “Functions” refer to pointers to functions included as members of a structure or CLASS • Window and Dialog Classes Provided by C++ are Actually Structures Set up this Way.

  6. Writing a Windows Program

  7. STEP 1 • Initialize the Application . . . • Write a function that does the following: • declare a variable of the WNDCLASS type • fill the members of that variable with values that “customize” your window • call the RegisterClass function, passing your class • return TRUE or FALSE (BOOL) to the main function, based upon whether RegisterClass was successful or not

  8. BOOL InitApplication ( HANDLE hInstance ) • { • WNDCLASS wc; • wc.style = CS_HREDRAW | CS_VREDRAW; • wc.lpfnWndProc = MainWndProc; • wc.cbClsExtra = 0; • wc.cbWndExtra = 0; • wc.hInstance = hInstance; • wc.hIcon = LoadIcon ( hInstance, IDI_APPLICATION ); • wc.hCursor = LoadCursor ( NULL, IDC_ARROW ); • wc.hbrBackground = GetStockObject ( WHITE_BRUSH ); • wc.lpszMenuName = NULL; • wc.lpszClassName = szAppName; • if ( !RegisterClass ( &wc ) ) • return ( FALSE ); • return ( TRUE ); • }

  9. STEP 2 • Initialize an ‘Instance’ of the Application • Write a function to do the following: • Assign the current instance to a global variable for use by other windows and dialog boxes in the program. • Call the CreateWindow function, passing the App Name as specified in your class, the window’s position and size, handles to parent and child windows and the handle to the current instance. • If the window could not be created, return FALSE, otherwise call the ShowWindow function to display the window.

  10. BOOL InitInstance ( HANDLE hInstance, int nCmdShow ) • {hInst = hInstance; /* assign instance to global variable */ • hWnd = CreateWindow ( szAppName, /* ClassName */ • ”My Window", /* window title */ • WS_OVERLAPPEDWINDOW, /* window style flag*/ • 70, /* horizontal position */ • 70, /* vertical position */ • 500, /* window width */ • 350, /* window height */ • NULL, /* handle of parent window */ • NULL, /* handle of child or menu */ • hInstance, /* handle of app instance */ • NULL ); /* window creation data */ • if ( !hWnd ) /* If window could not be created, return "failure” */ • return ( FALSE ); • ShowWindow ( hWnd, nCmdShow ); /* Show the window */ • return ( TRUE ); /* Returns the value from PostQuitMessage */ • }

  11. BACK IN MAIN • Make Calls to Your InitApplication and InitInstance Functions. • Write a Windows Message Loop that Continuously Checks for New Messages (such as the click of a mouse button or press of a key on the keyboard) • When a Message is Received, the WNDCLASS Window you Created will Call the Window Procedure it points to (MainWndProc).

  12. STEP 3 • Handle the Windows Messages Received • Write MainWndProc, which does the following: • Use nested switch constructs to handle valid windows messages expected by the main window. • Makes calls to other procedures such as Dialog Box procedures, when necessary for handling messages to other resources.

  13. LRESULT CALLBACK MainWndProc ( HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam ) • { • switch ( message ) • { • case WM_COMMAND: /* Menu Selection or Dialog Box Control */ • switch (wParam) • { case CM_EXIT: /* ID of the EXIT menu item */ • DestroyWindow ( hWnd ); • case CM_MESSAGE_TO_SCREEN; /* another menu item */ • MessageBox( hWnd, "Message Text", ”Message Title", MB_OK); • default: • return ( DefWindowProc ( hWnd, message, wParam, lParam ) ); • } • default: • return ( DefWindowProc ( hWnd, message, wParam, lParam ) ); • } • }

  14. WINDOWS MESSAGES • WM_COMMAND - Sent as the result of a mouse click or key press. • Carries as part of its structure, ID’s of Menu Items, Buttons or Dialog Box Controls selected by the mouse or keyboard. • Other messages can be sent to a window or dialog box by a call to a function inside the program telling the object to behave a certain way.

  15. FLAGS • Determine Window Style or Behavior • Can Be Grouped in a Single Call to a Creation Function in Order to Add Functionality to a Window • hWnd = CreateWindow ( szAppName, /* ClassName */ • ”My Window", /* window title */ • WS_OVERLAPPEDWINDOW, • /* window style flag*/ • 70, /* horizontal position */ • 70, /* vertical position */ • 500, /* window width */ etc……... • Flags are Names for an Underlying Number Created with #define in windows.h

More Related