Windows-API
Børre Stenseth
Moduler>Arkitektur>C/WinAPI

C program mot Windows API

Hva
Image1
C-kode mot den originale Windows API'en

Programmet som vises her er skrevet mot Windows originale, grunnleggende, API. Dette er et rent bibliotek som gir oss tilgang til de grunnleggende ressursne i operativsystemet. Objekter er et fremmedord.

Programmet er skrevet i C og gjør ikke annet enn å vise fram noen røde prikker, det har to menyer og en dialogboks (About). Merk at dette programmet kan lastes opp i Visual Studio/.net, kompilerer og kjører.

Alle C-programmer skal ha en header fil, dots.h:

/*************************************************************\
* generic.h: Header file for Generic                          *
*                                                             *
*                                                             *
\*************************************************************/
/******* Menu Defines *******/
#define IDM_ABOUT      1000
#define IDM_EXIT      1001

Selve C-koden inneholder initialisering og en løkke som plukker opp begivenheter (WinMain) og en lang switch for å tolke og utføre begivenheter (MainWndProc).

/********************************************************************\
*  cdots.c: Source code for cdots                                    *
*                                                                    *
*  Comments: simple Win32-based Application                          *
*             distribute a set of red spots whenever painted          *
*                                                                    *
*  Functions:                                                        *
*     WinMain      - Application entry point                         *
*     MainWndProc  - main window procedure                           *
*     AboutDlgProc - dialog procedure for About dialog               *
*                                                                    *
*                                                                    *
\********************************************************************/
/*********************  Header Files  *********************/
#include <windows.h>
#include "cdots.h"
/*********************  Prototypes  ***********************/
LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
LRESULT WINAPI AboutDlgProc( HWND, UINT, WPARAM, LPARAM );
/*******************  Global Variables ********************/
HANDLE ghInstance;
/********************************************************************\
*  Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int)    *
*                                                                    *
*   Purpose: Initializes Application                                 *
*                                                                    *
*  Comments: Register window class, create and display the main      *
*            window, and enter message loop.                         *
*                                                                    *
*                                                                    *
\********************************************************************/
int PASCAL WinMain( HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszCmdLine,
    int nCmdShow )
{
   WNDCLASS wc;
   MSG msg;
   HWND hWnd;
   if( !hPrevInstance )
   {
      wc.lpszClassName = "GenericAppClass";
      wc.lpfnWndProc = MainWndProc;
      wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
      wc.hInstance = hInstance;
      wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
      wc.hCursor = LoadCursor( NULL, IDC_ARROW );
      wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
      wc.lpszMenuName = "GenericAppMenu";
      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;
      RegisterClass( &wc );
   }
   ghInstance = hInstance;
   hWnd = CreateWindow( "GenericAppClass",
      "Generic Application",
      WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,
      0,
      0,
      CW_USEDEFAULT,
      CW_USEDEFAULT,
      NULL,
      NULL,
      hInstance,
      NULL
   );
   ShowWindow( hWnd, nCmdShow );
   while( GetMessage( &msg, NULL, 0, 0 ) ) {
      TranslateMessage( &msg );
      DispatchMessage( &msg );
   }
   return msg.wParam;
}
/********************************************************************\
* Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) *
*                                                                    *
*  Purpose: Processes Application Messages                           *
*                                                                    *
* Comments: The following messages are processed                     *
*                                                                    *
*           WM_PAINT                                                 *
*           WM_COMMAND                                               *
*           WM_DESTROY                                               *
*                                                                    *
*                                                                    *
\********************************************************************/
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam,
   LPARAM lParam )
{
    PAINTSTRUCT ps;
    HDC hDC;
    int ix;
    RECT R;
    HBRUSH newBrush;
    HBRUSH oldBrush;

   switch( msg ) {
/**************************************************************\
*     WM_PAINT:                                                *
\**************************************************************/
      case WM_PAINT:
            hDC = BeginPaint( hWnd, &ps );
            GetClientRect(hWnd,(LPRECT)&R);
            newBrush=CreateSolidBrush(RGB(255,0,0));
            oldBrush=SelectObject(hDC,newBrush);
            // make 20 dots
            for(ix=0;ix<20;ix++)
            {
                // where ?
                int x=10+rand()%(R.right-R.left-20);
                int y=10+rand()%(R.bottom-R.top-20);
                Ellipse(hDC,x-10,y-10,x+10,y+10);
            }
            SelectObject(hDC,oldBrush);
            EndPaint( hWnd, &ps );
         break;
/**************************************************************\
*     WM_COMMAND:                                              *
\**************************************************************/
      case WM_COMMAND:
         switch( wParam ) {
            case IDM_ABOUT:
               DialogBox( ghInstance, "AboutDlg", hWnd, (DLGPROC)
                          AboutDlgProc );
            break;
            case IDM_EXIT:
                PostQuitMessage( 0 );
            break;
         }
      break;
/**************************************************************\
*     WM_DESTROY: PostQuitMessage() is called                  *
\**************************************************************/
      case WM_DESTROY:
         PostQuitMessage( 0 );
         break;
/**************************************************************\
*     Let the default window proc handle all other messages    *
\**************************************************************/
      default:
         return( DefWindowProc( hWnd, msg, wParam, lParam ));
   }
   return 0;
}
/********************************************************************\
* Function: LRESULT CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM)*
*                                                                    *
*  Purpose: Processes "About" Dialog Box Messages                    *
*                                                                    *
* Comments: The About dialog box is displayed when the user clicks   *
*           About from the Help menu.                                *
*                                                                    *
\********************************************************************/
LRESULT CALLBACK AboutDlgProc( HWND hDlg, 
                              UINT uMsg, 
                              WPARAM wParam, 
                              LPARAM lParam )
{
   switch( uMsg ) {
      case WM_INITDIALOG:
         return TRUE;
      case WM_COMMAND:
         switch( wParam ) {
            case IDOK:
               EndDialog( hDlg, TRUE );
               return TRUE;
         }
      break;
   }
   return FALSE;
}

Alle ressursene til programmet, i dette tilfellet menyer og dialogboks, er beskrevet tekstlig i en ressursfil. I starten fantes ingen GUI- eller ressurseditor som kunne hjelpe til med dette.

// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Norwegian (Bokmal) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_NOR)
#ifdef _WIN32
LANGUAGE LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL
#pragma code_page(1252)
#endif //_WIN32


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
ABOUTDLG DIALOGEX 6, 21, 198, 99
STYLE DS_SETFONT|DS_MODALFRAME|DS_FIXEDSYS|WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU
CAPTION "About Generic"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
    DEFPUSHBUTTON   "&OK",IDOK,72,74,40,14
    LTEXT           "CDOTS",104,45,14,128,8
    LTEXT           "Et eksempel program",105,44,27,59,8
    LTEXT           "B Stenseth",106,45,45,98,8
    LTEXT           "for lenge siden",107,45,54,138,8
END

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//
GENERICAPPMENU MENU 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&Exit",                       IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About",                      IDM_ABOUT
    END
END

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
END
#endif    // APSTUDIO_INVOKED
#endif    // Norwegian (Bokmal) resources
/////////////////////////////////////////////////////////////////////////////

#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//

/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED
Referanser
Prosjektet:
https://svn.hiof.no/svn/psource/Csharpspikes/old-c-case
Vedlikehold

B.Stenseth, januar 2007

(Velkommen) Moduler>Arkitektur>C/WinAPI (MDI)