Knapper Überblick zur Windows-Programmierung mit C++. (), Crash-Lektion, Seite 721492
https://www.purl.org/stefan_ram/pub/c++-windows (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
C++-Kurs

Windows -Programmierung

Diese Einleitung in die Windows -Programmierung mit C++  wurde in wenig Zeit erstellt und enthält daher nur einige Stichpunkte mit Notizen und Beispiele. Sie ist noch kein sorgfältig aufgebauter Lehrtext, der ohne weitere mündliche Erklärungen verständlich ist.

Erste Schritte

Um Windows  zu programmieren, ruft ein C++-Programm Operationen der Windows -Anwendungsprogrammschnittstelle (des Windows -API [application program interface ]) auf.

Eine Beschreibung dieser Windows -Schnittstelle kann unter der folgenden URI beschafft werden.

Beschreibung der Windows -Anwendungsschnittstelle

ftp://ftp.cs.virginia.edu/pub/lcc-win32/win32hlp.exe

http://maben.homeip.net/static/s100/software/microsoft/windowsapi/

Kurze Erklärungen in deutscher Sprache
http://www.cul.de/data/win32pr1.html

Ist diese Schnittstelle stabil?

Es ist derzeit nicht mehr so, daß die Verwendung der klassische Windows -Schnittstelle als der Königsweg der Programmierung für das Betriebssystem Microsoft ® Windows  angesehen werden kann, denn inzwischen gibt es einen Nachfolger im Projekt .net  und einen Möglichen Nachfolger dieses Nachfolgers WinFX  mit einer neuen Benutzerschnittstelle Avalon. Dadurch ist die Zukunftssicherheit eines auf einer der klassischen Windows -Schnittstellen basierenden Programms in Frage gestellt. Dieses Thema wird auch in dem folgenden Artikel behandelt.

How Microsoft Lost the API War
Joel Spolsky
http://www.joelonsoftware.com/articles/APIWar.html
2004-06-13

Stabilere Schnittstellen

Es ist möglich, C++ -Programme zu schreiben, die auf stabileren Schnittstellen laufen. Etwa auf solchen, die es für verschiedene Betriebssysteme gibt. Hierfür kann beispielsweise wxWidgets  verwendet werden.

wxWidgets
http://www.wxwidgets.org/

Ein reaktives Windows-Programm

Das folgende Beispiel eines Windows -Programms ist nur reaktiv (nicht interaktiv). Es läuft nach dem Start also ohne Benutzerinteraktion ab. Es kann mit Dev-C++ 5  als Windows -Anwendung übersetzt werden, nachdem noch zwei Einfügedirektiven an den Anfang gefügt wurden. Reaktive Programme werden hier zuerst behandelt, da sie im allgemeinen einfacher sind als interaktive.

Zwei Einfügedirektiven
#include <Windows.h>
#include <tchar.h>

Toggle IE Image Setting
http://www.beginthread.com/Article/Ehsan/Changing%20IE%20Show%20Picture%20Setting/
Gemeint ist das zweite  Programm auf dieser Seite.

Ein interaktives Windows -Programm

Ein interaktives Windows -Programm kann von der folgenden Seite erhalten werden:

C++ in Action – Windows Techniques – Painting – Source Code
http://www.relisoft.com/book/win/source/win.zip

Damit dieses Programm mit Dev-C++  übersetzt werden kann, sind folgende Schritte nötig:

Microsoft Foundation Classes  (MFC )

Die Microsoft Foundation Classes  (MFC ) wurden von Microsoft  um 1992 als C++ -Bibliothek geschaffen, um die Windows -API zu kapseln.

MFC  entlastet den Programmierer durch automatische Freigabe von Objekten beim Verlassen eines Gültigkeitsbereichs; es enthält einen Rahmen für Programm, die nach dem Dokument-Ansicht-Muster aufgebaut sind; und bietet verschiedene Makros zur Abkürzung häufiger Quelltextmuster. Da diese Makros die Typprüfungen des Übersetzer aber teilweise umgingen, erschwerten sie das Finden mancher Fehler.

Ein Ziel von MFC  war es, die Windows -Programmierung in C++  zu vereinfachen. Programmierer, die durch die Windows -Programmierung in C++  überfordert sind, können aber heute auch gleich das noch viel einfachere Visual Basic  oder C#  verwenden. Daher neigt sich die Ära von MFC  vielleicht langsam ihrem Ende zu; MFC  wird aber heute noch verwendet.

MFC  wird mit Microsoft-Entwicklungssystemen verteilt und kann nicht mit anderen Entwicklungssystemen verwendet werden. Durch die Verwendung von MFC  bindet ein Programmierer sich also recht fest an Microsoft.

MFC  ist nicht zur Windows -Programmierung mit C++  notwendig. Da MFC  neben seinen Vorteilen auch Nachteile mit sich bringt (Ausschaltung der Typprüfung in einigen Fällen, höhere Komplexität), wird vor der Durchführung eines C++ -Projekts im allgemeinen zu prüfen sein, ob dieses mit oder ohne MFC  realisiert werden soll. Dabei wird es auch eine Rolle spielen, mit welcher der beiden Methoden die Entwickler mehr vertraut sind.

Einfaches Beispiel einer kleinen MFC-Anwendung
http://www.digitalmars.com/drn-bin/wwwnews?c%2B%2B.mfc/198
Gemeint ist der Quelltext von der Zeile "#include <afxwin.h>" bis zur Zeile, die mit dem Text "CHelloApp HelloApp;" beginnt.
Zentrale Informationsseite zu MFC
http://msdn.microsoft.com/library/en-us/vcmfc98/html/mfchm.asp?frame=false

Kodierungsseite einstellen

main.c

#include <stdio.h>
#include <windows.h>

int main( void )
{ SetConsoleOutputCP(437); }

Tonausgabe

#include <windows.h>

... Beep(integral_frequency,duration); ...

Hallo

#include <windows.h>

int WINAPI WinMain( HINSTANCE d1, HINSTANCE d2, LPSTR d3, int d4 )

{ MessageBox( NULL, “Hello, World!”, “”, MB_OK ); }

Rechner zeitverzögert nach unten fahren

down.cpp

#include <windows.h>

int main(){ Sleep( 3 * 1000 ); ExitWindowsEx( EWX_SHUTDOWN | EWX_FORCE, 0 ); }

EWX_FORCE zwangsweise beenden

EWX_LOGOFF Abmelden

EWX_POWEROFF Strom abschalten

EWX_REBOOT Neustart

EWX_SHUTDOWN Nach unten fahren

down1.cpp

#include <iostream> // ::std::cout
#include <ostream> // <<
#include <windows.h>
#include <stdio.h>
#include <conio.h>

typedef int( WINAPI * cfunc )( UINT );

int main()
{ HINSTANCE hLib = LoadLibrary( "SHELL32.dll" );
::std::cout << "a" << ::std::endl;
Sleep( 3 * 1000 );
::std::cout << "b" << ::std::endl;
if( hLib )
{ char mod[ 99 ];
GetModuleFileName(( HMODULE )hLib,( LPTSTR )mod, 99 );
cfunc exit_function =
( cfunc )GetProcAddress(( HMODULE )hLib, "SHExitWindowsEx" );
if( exit_function )exit_function( 2 );
FreeLibrary(( HMODULE )hLib ); }}

down2.cpp

#include <windows.h>

int main()
{ Sleep( 3 * 1000 );
HANDLE hToken; TOKEN_PRIVILEGES tkp;
if( OpenProcessToken
( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ))
{ LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[ 0 ].Luid );
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges
( hToken, FALSE, &tkp, 0,( PTOKEN_PRIVILEGES )NULL, 0);
if( GetLastError() == ERROR_SUCCESS )
{ BOOL bShutdown =
InitiateSystemShutdown( NULL, NULL, 10, TRUE, FALSE ); }}}

Notizen

Windows 9x, Me
system("C:\\WINDOWS\\rundll.exe Shell32,SHExitWindowsEX 1");

Windows 2000, XP
system("C:\\WINDOWS\\System32\\shutdown.exe -s -f -t 0 ");

rundll32.exe user.exe,ExitWindows [Fast Shutdown of Windows]
rundll32.exe user.exe,ExitWindowsExec [Restart Windows]
RunDll32.exe Shell32.dll,ExitWindowsEx,0x0 Logoff
RunDll32.exe Shell32.dll,ExitWindowsEx,0x1 Shutdown
RunDll32.exe Shell32.dll,ExitWindowsEx,0x2 Restart
RUNDLL32.EXE shell32.dll,SHExitWindowsEx 9
RUNDLL32.EXE shell32.dll,SHExitWindowsEx -1

http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/dialogboxreference/dialogboxstruct...

Zeit einlesen

/* This has "main" - not "WinMain", so it is not a Windows Project */

#include <windows.h>

#include <cstring> /* memset */

#include <cstdio> /* sscanf, sprintf */

/* begin of unspecific extension library */

#define ATOM_BUTTON 0x0080

#define ATOM_EDIT 0x0081

#define ATOM_STATIC 0x0082

#define ATOM_LIST_BOX 0x0083

#define ATOM_SCROLL_BAR 0x0084

#define ATOM_COMBO_BOX 0x0085

BOOL CALLBACK DialogProc( HWND, UINT, WPARAM, LPARAM );

LPWORD lpwAlign( LPWORD const lpIn )

{ ULONG ul =( ULONG )lpIn; ul +=3; ul >>= 2; ul <<= 2;

return( LPWORD )ul; }

void set_dialog_style

( LPDLGTEMPLATE const lpdt, DWORD const style )

{ lpdt->style = style; }

void set_dialog_number_of_controls

( LPDLGTEMPLATE const lpdt, WORD const number_of_controls )

{ lpdt->cdit = number_of_controls; }

void set_dialog_geometry

( LPDLGTEMPLATE const lpdt,

short const position_height, short const position_width,

short const extension_height, short const extension_width )

{ lpdt->x = position_width; lpdt->y = position_height;

lpdt->cx = extension_width; lpdt->cy = extension_height; }

void append_dialog

( LPDLGTEMPLATE const lpdt, DWORD const style, WORD const number_of_controls,

short const position_height, short const position_width,

short const extension_height, short const extension_width )

{ set_dialog_style( lpdt, style );

set_dialog_number_of_controls( lpdt, number_of_controls );

set_dialog_geometry

( lpdt, position_height, position_width,

extension_height, extension_width ); }

LPWORD append_dialog1( LPWORD lpw, DWORD const style,

WORD const number_of_controls,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

char const * const text )

{ { LPDLGTEMPLATE const lpdt =( LPDLGTEMPLATE )lpw;

append_dialog

( lpdt, style, number_of_controls, position_height, position_width,

extension_height, extension_width ); lpw =( LPWORD )( lpdt + 1 ); }

*lpw++ = 0; // menu

*lpw++ = 0; // class

lpw += 1 + MultiByteToWideChar( CP_ACP, 0, text, -1,( LPWSTR )lpw, 50 );

return lpw; }

void set_item_id

( LPDLGITEMTEMPLATE const lpdt, WORD const id )

{ lpdt->id = id; }

void set_item_style

( LPDLGITEMTEMPLATE const lpdt, DWORD const style )

{ lpdt->style = style; }

void set_item_geometry

( LPDLGITEMTEMPLATE const lpdt,

short const position_height, short const position_width,

short const extension_height, short const extension_width )

{ lpdt->x = position_width; lpdt->y = position_height;

lpdt->cx = extension_width; lpdt->cy = extension_height; }

LPWORD append_item0

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, DWORD const style, WORD const class_ )

{ lpw = lpwAlign( lpw );

{ LPDLGITEMTEMPLATE lpdit =( LPDLGITEMTEMPLATE )lpw;

set_item_geometry

( lpdit, position_height, position_width,

extension_height, extension_width );

set_item_id( lpdit, id );

set_item_style( lpdit, style );

lpw =( LPWORD )( lpdit + 1 ); }

*lpw++ = 0xFFFF;

*lpw++ = class_; // button class

return lpw; }

LPWORD append_item

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, DWORD const style, WORD const class_,

char const * const text )

{ lpw = append_item0( lpw, position_height, position_width,

extension_height, extension_width, id, style, class_ );

{ lpw += 1 + MultiByteToWideChar( CP_ACP, 0, text, -1,( LPWSTR )lpw, 50 ); }

*lpw++ = 0; // no creation data

return lpw; }

LPWORD append_item1

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, DWORD const style, WORD const class_, char const * text )

{ lpw = append_item0( lpw, position_height, position_width,

extension_height, extension_width, id, style, class_ );

{ LPWSTR lpwsz;

for( lpwsz =( LPWSTR )lpw; *lpwsz++ =( WCHAR )*text++; );

lpw =( LPWORD )lpwsz; }

*lpw++ = 0; // no creation data

return lpw; }

DWORD default_dialog_style =

WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;

DWORD default_label_style = WS_CHILD | WS_VISIBLE | SS_LEFT;

DWORD default_edit_style =

ES_LEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;

DWORD default_button_style =

WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP;

LPWORD append_default_label

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width, WORD const id,

char const * const text )

{ return append_item1

( lpw, position_height, position_width, extension_height, extension_width,

id, default_label_style, ATOM_STATIC, text ); }

LPWORD append_default_edit

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, position_width, extension_height, extension_width,

id, default_edit_style, ATOM_EDIT, text ); }

LPWORD append_default_button

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, position_width, extension_height, extension_width,

id, default_button_style, ATOM_BUTTON, text ); }

LPWORD append_default_dialog( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const number_of_controls, char const * const text )

{ return append_dialog1

( lpw, default_dialog_style, number_of_controls, position_height,

position_width, extension_height, extension_width, text ); }

/* end of unspecific extension library */

/* begin of specific build section (View) */

#define ID_TEXT_0 200

#define ID_TEXT_1 201

#define ID_EDIT_0 300

#define ID_EDIT_1 301

#define ID_START 400

#define ID_EXIT 500

int const label_position = 12;

int const edit_position = 40;

int const button_position = 10;

LPWORD my_label

( LPWORD lpw,

short const position_height,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, label_position, 20, 30, id, default_label_style,

ATOM_STATIC, text ); }

LPWORD my_edit

( LPWORD lpw,

short const position_height,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, edit_position, 12, 40, id, default_edit_style,

ATOM_EDIT, text ); }

LPWORD my_button

( LPWORD lpw,

short const position_height,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, button_position, 15, 80, id, default_button_style,

ATOM_BUTTON, text ); }

LPWORD my_dialog( LPWORD lpw,

short const extension_height, WORD const number_of_controls )

{ return append_dialog1

( lpw, default_dialog_style, number_of_controls, 10, 10, extension_height,

100, "Shutdown Control " ); }

void appendDialogDescription( LPWORD lpw )

{ LPWORD const lpw0 = lpw;

WORD n = 0;

lpw = my_dialog( lpw, 0, 0 );

int y = 0;

lpw = my_label( lpw, ( y += 10 )+ 2, ID_TEXT_0, "&Hours" ); ++n;

lpw = my_edit( lpw, ( y += 0 )+ 0, ID_EDIT_0, "0" ); ++n;

lpw = my_label( lpw, ( y += 20 )+ 2, ID_TEXT_1, "&Minutes" ); ++n;

lpw = my_edit( lpw, ( y += 0 )+ 0, ID_EDIT_1, "0" ); ++n;

lpw = my_button( lpw, ( y += 20 )+ 0, ID_START, "&Start Shutdown" ); ++n;

lpw = my_button( lpw, ( y += 20 )+ 0, ID_EXIT, "&Exit" ); ++n;

my_dialog( lpw0, ( y += 20 )+ 0, n ); }

void makeDialogDescription( HGLOBAL const hgbl )

{ LPWORD const lpw =( LPWORD )GlobalLock( hgbl );

if( lpw ){ appendDialogDescription( lpw ); GlobalUnlock( hgbl ); }}

int runDialog( HGLOBAL const hgbl )

{ makeDialogDescription( hgbl );

return DialogBoxIndirect

( NULL,( LPDLGTEMPLATE )hgbl, NULL,( DLGPROC )DialogProc ); }

/* end of specific build section (View) */

/* begin of specific model */

char nEditTwo_0[ 128 ]= "1" ;

char nEditTwo_1[ 128 ]= "1" ;

/* end of specific model */

/* begin of specific controller */

int main()

{ LRESULT ret = -1;

HGLOBAL const hgbl = GlobalAlloc( GMEM_ZEROINIT, 1024 );

if( hgbl )

{ ret = runDialog( hgbl );

GlobalFree( hgbl ); }

return ret; }

BOOL CALLBACK DialogProc

( HWND const hDlg, UINT const iMsg, WPARAM const wParam, LPARAM const lParam )

{ switch( iMsg )

{ case WM_INITDIALOG:

{ SetDlgItemText( hDlg, ID_EDIT_0,( LPCWSTR )nEditTwo_0 );

SetDlgItemText( hDlg, ID_EDIT_1,( LPCWSTR )nEditTwo_1 );

return TRUE; }

case WM_COMMAND:

{ switch( LOWORD( wParam ))

{ case ID_EDIT_0:

{ GetDlgItemText( hDlg, ID_EDIT_0,( LPWSTR )nEditTwo_0, 127 ); return TRUE; }

case ID_EDIT_1:

{ GetDlgItemText( hDlg, ID_EDIT_1,( LPWSTR )nEditTwo_1, 127 ); return TRUE; }

case ID_START: case IDOK:

{ int hours, minutes;

sscanf( nEditTwo_0, "%d", &hours );

sscanf( nEditTwo_1, "%d", &minutes );

minutes += hours * 60;

char buff[ 128 ];

sprintf( buff, "%d", minutes );

MessageBoxA

( NULL,( LPCSTR )buff,( LPCSTR )"Input Data", MB_OK | MB_SYSTEMMODAL | MB_NOFOCUS );

return TRUE; }

case ID_EXIT: case IDCANCEL: case IDABORT:

{ EndDialog( hDlg, 0 ); return TRUE; }} break; }}

return FALSE; }

/* end of specific controller */

Stoppuhr

#include <windows.h>

#include <iostream> /* debug output only */

#include <cstring> /* memset */

#include <cstdio> /* sscanf, sprintf */

/* ************************************************************************** */

/* ************************************************************************** */

/* ************************************************************************** */

/* begin of unspecific extension library */

#define ATOM_BUTTON 0x0080

#define ATOM_EDIT 0x0081

#define ATOM_STATIC 0x0082

#define ATOM_LIST_BOX 0x0083

#define ATOM_SCROLL_BAR 0x0084

#define ATOM_COMBO_BOX 0x0085

BOOL CALLBACK DialogProc( HWND, UINT, WPARAM, LPARAM );

LPWORD lpwAlign( LPWORD const lpIn )

{ ULONG ul =( ULONG )lpIn; ul +=3; ul >>= 2; ul <<= 2;

return( LPWORD )ul; }

void set_dialog_style

( LPDLGTEMPLATE const lpdt, DWORD const style )

{ lpdt->style = style; }

void set_dialog_number_of_controls

( LPDLGTEMPLATE const lpdt, WORD const number_of_controls )

{ lpdt->cdit = number_of_controls; }

void set_dialog_geometry

( LPDLGTEMPLATE const lpdt,

short const position_height, short const position_width,

short const extension_height, short const extension_width )

{ lpdt->x = position_width; lpdt->y = position_height;

lpdt->cx = extension_width; lpdt->cy = extension_height; }

void append_dialog

( LPDLGTEMPLATE const lpdt, DWORD const style, WORD const number_of_controls,

short const position_height, short const position_width,

short const extension_height, short const extension_width )

{ set_dialog_style( lpdt, style );

set_dialog_number_of_controls( lpdt, number_of_controls );

set_dialog_geometry

( lpdt, position_height, position_width,

extension_height, extension_width ); }

LPWORD append_dialog1( LPWORD lpw, DWORD const style,

WORD const number_of_controls,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

char const * const text )

{ { LPDLGTEMPLATE const lpdt =( LPDLGTEMPLATE )lpw;

append_dialog

( lpdt, style, number_of_controls, position_height, position_width,

extension_height, extension_width ); lpw =( LPWORD )( lpdt + 1 ); }

*lpw++ = 0; // menu

*lpw++ = 0; // class

lpw += 1 + MultiByteToWideChar( CP_ACP, 0, text, -1,( LPWSTR )lpw, 50 );

return lpw; }

void set_item_id

( LPDLGITEMTEMPLATE const lpdt, WORD const id )

{ lpdt->id = id; }

void set_item_style

( LPDLGITEMTEMPLATE const lpdt, DWORD const style )

{ lpdt->style = style; }

void set_item_geometry

( LPDLGITEMTEMPLATE const lpdt,

short const position_height, short const position_width,

short const extension_height, short const extension_width )

{ lpdt->x = position_width; lpdt->y = position_height;

lpdt->cx = extension_width; lpdt->cy = extension_height; }

LPWORD append_item0

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, DWORD const style, WORD const class_ )

{ lpw = lpwAlign( lpw );

{ LPDLGITEMTEMPLATE lpdit =( LPDLGITEMTEMPLATE )lpw;

set_item_geometry

( lpdit, position_height, position_width,

extension_height, extension_width );

set_item_id( lpdit, id );

set_item_style( lpdit, style );

lpw =( LPWORD )( lpdit + 1 ); }

*lpw++ = 0xFFFF;

*lpw++ = class_; // button class

return lpw; }

LPWORD append_item

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, DWORD const style, WORD const class_,

char const * const text )

{ lpw = append_item0( lpw, position_height, position_width,

extension_height, extension_width, id, style, class_ );

{ lpw += 1 + MultiByteToWideChar( CP_ACP, 0, text, -1,( LPWSTR )lpw, 50 ); }

*lpw++ = 0; // no creation data

return lpw; }

LPWORD append_item1

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, DWORD const style, WORD const class_, char const * text )

{ lpw = append_item0( lpw, position_height, position_width,

extension_height, extension_width, id, style, class_ );

{ LPWSTR lpwsz;

for( lpwsz =( LPWSTR )lpw; *lpwsz++ =( WCHAR )*text++; );

lpw =( LPWORD )lpwsz; }

*lpw++ = 0; // no creation data

return lpw; }

DWORD default_dialog_style =

WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;

DWORD default_label_style = WS_CHILD | WS_VISIBLE | SS_LEFT;

DWORD default_edit_style =

ES_RIGHT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;

DWORD default_button_style =

WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP;

LPWORD append_default_label

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width, WORD const id,

char const * const text )

{ return append_item1

( lpw, position_height, position_width, extension_height, extension_width,

id, default_label_style, ATOM_STATIC, text ); }

LPWORD append_default_edit

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, position_width, extension_height, extension_width,

id, default_edit_style, ATOM_EDIT, text ); }

LPWORD append_default_button

( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, position_width, extension_height, extension_width,

id, default_button_style, ATOM_BUTTON, text ); }

LPWORD append_default_dialog( LPWORD lpw,

short const position_height, short const position_width,

short const extension_height, short const extension_width,

WORD const number_of_controls, char const * const text )

{ return append_dialog1

( lpw, default_dialog_style, number_of_controls, position_height,

position_width, extension_height, extension_width, text ); }

/* end of unspecific extension library */

/* ************************************************************************** */

/* ************************************************************************** */

/* ************************************************************************** */

/* begin of specific build section (View) */

#define ID_TEXT_0 200

#define ID_EDIT_0 201

#define ID_TEXT_1 210

#define ID_EDIT_1 211

#define ID_TEXT_2 220

#define ID_EDIT_2 221

#define ID_TIMER 230

#define ID_START 400

#define ID_STOP 401

#define ID_EXIT 500

int const label_position = 12;

int const horizontal_edit_position = 50;

int const button_position = 10;

LPWORD my_label

( LPWORD lpw,

short const position_height,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, label_position, 20, 30, id, default_label_style,

ATOM_STATIC, text ); }

LPWORD my_edit

( LPWORD lpw,

short const position_height,

WORD const id, char const * const text )

{ short const extension_height = 12;

short const extension_width = 39;

return append_item1

( lpw, position_height, horizontal_edit_position,

extension_height, extension_width,

id, default_edit_style,

ATOM_EDIT, text ); }

LPWORD my_button

( LPWORD lpw,

short const position_height,

WORD const id, char const * const text )

{ return append_item1

( lpw, position_height, button_position, 15, 80, id, default_button_style,

ATOM_BUTTON, text ); }

LPWORD my_dialog( LPWORD lpw,

short const extension_height, WORD const number_of_controls )

{ return append_dialog1

( lpw, default_dialog_style, number_of_controls, 10, 10, extension_height,

100, "Countdown Timer" ); }

void appendDialogDescription( LPWORD lpw )

{ LPWORD const lpw0 = lpw;

WORD n = 0;

lpw = my_dialog( lpw, 0, 0 );

int y = 0;

lpw = my_label( lpw, ( y += 10 )+ 2, ID_TEXT_0, "&Hours" ); ++n;

lpw = my_edit( lpw, ( y += 0 )+ 0, ID_EDIT_0, "0" ); ++n;

lpw = my_label( lpw, ( y += 20 )+ 2, ID_TEXT_1, "&Minutes" ); ++n;

lpw = my_edit( lpw, ( y += 0 )+ 0, ID_EDIT_1, "0" ); ++n;

lpw = my_label( lpw, ( y += 20 )+ 2, ID_TEXT_2, "&Seconds" ); ++n;

lpw = my_edit( lpw, ( y += 0 )+ 0, ID_EDIT_2, "0" ); ++n;

lpw = my_button( lpw, ( y += 20 )+ 0, ID_START, "St&art Timer" ); ++n;

lpw = my_button( lpw, ( y += 20 )+ 0, ID_STOP, "St&op Timer" ); ++n;

lpw = my_button( lpw, ( y += 20 )+ 0, ID_EXIT, "E&xit" ); ++n;

my_dialog( lpw0, ( y += 20 )+ 0, n ); }

void makeDialogDescription( HGLOBAL const hgbl )

{ LPWORD const lpw =( LPWORD )GlobalLock( hgbl );

if( lpw ){ appendDialogDescription( lpw ); GlobalUnlock( hgbl ); }}

int runDialog( HGLOBAL const hgbl )

{ makeDialogDescription( hgbl );

return DialogBoxIndirect

( NULL,( LPDLGTEMPLATE )hgbl, NULL,( DLGPROC )DialogProc ); }

/* end of specific build section (View) */

/* begin of specific model */

char nEditTwo_0[ 128 ]= "1" ;

char nEditTwo_1[ 128 ]= "1" ;

char nEditTwo_2[ 128 ]= "1" ;

/* end of specific model */

/* begin of specific controller */

int main()

{ LRESULT ret = -1;

HGLOBAL const hgbl = GlobalAlloc( GMEM_ZEROINIT, 1024 );

if( hgbl )

{ ret = runDialog( hgbl );

GlobalFree( hgbl ); }

return ret; }

int started = 0;

DWORD start_time;

long run_time;

long start_h;

long start_m;

long start_s;

VOID CALLBACK TimerProc(HWND hDlg, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)

{ int seconds;

char buffer[ 128 ];

const DWORD now_time = GetTickCount();

const long passed =( long )now_time -( long )start_time;

long left =( run_time - passed )/1000; // run_time ist falsch

const long h = left /( 60L * 60L ); left -= h *( 60L * 60L );

const long m = left / 60L; left -= m * 60L;

const long s = left; left -= s;

sprintf( buffer, "%ld", h ); SetDlgItemText( hDlg, ID_EDIT_0, buffer );

sprintf( buffer, "%ld", m ); SetDlgItemText( hDlg, ID_EDIT_1, buffer );

sprintf( buffer, "%ld", s ); SetDlgItemText( hDlg, ID_EDIT_2, buffer );

}

BOOL CALLBACK DialogProc

( HWND const hDlg, UINT const iMsg, WPARAM const wParam, LPARAM const lParam )

{ switch( iMsg )

{ case WM_INITDIALOG:

{ SetDlgItemText( hDlg, ID_EDIT_0, nEditTwo_0 );

SetDlgItemText( hDlg, ID_EDIT_1, nEditTwo_1 );

SetDlgItemText( hDlg, ID_EDIT_2, nEditTwo_2 );

started = 0;

return TRUE; }

case WM_COMMAND:

{ switch( LOWORD( wParam ))

{ case ID_EDIT_0:

{ GetDlgItemText( hDlg, ID_EDIT_0, nEditTwo_0, 127 ); return TRUE; }

case ID_EDIT_1:

{ GetDlgItemText( hDlg, ID_EDIT_1, nEditTwo_1, 127 ); return TRUE; }

case ID_START: case IDOK:

{ if( !started )

{ start_time = GetTickCount();

{ char buffer[ 128 ];

GetDlgItemText( hDlg, ID_EDIT_0, buffer, 127 ); sscanf( buffer, "%ld", &start_h );

GetDlgItemText( hDlg, ID_EDIT_1, buffer, 127 ); sscanf( buffer, "%ld", &start_m );

GetDlgItemText( hDlg, ID_EDIT_2, buffer, 127 ); sscanf( buffer, "%ld", &start_s );

run_time = start_h;

run_time =( run_time * 60L )+ start_m; /* was "+=" */

run_time =( run_time * 60L )+ start_s;

run_time *= 1000L; }

const HWND parent = hDlg;

const UINT timer_id = ID_TIMER;

const UINT delay_in_ms = 1000;

const TIMERPROC timer_callback = TimerProc ;

const UINT success =

SetTimer( parent, timer_id, delay_in_ms, timer_callback );

if( !success )

MessageBox

( parent, "Could not SetTimer.", "Error", MB_OK | MB_ICONEXCLAMATION );

started = 1; }

break; }

case WM_TIMER: /* did not work */

{ MessageBox

( NULL, "received", "received", MB_OK | MB_SYSTEMMODAL | MB_NOFOCUS );

SetDlgItemText( hDlg, ID_EDIT_2, "1" );

break; }

case ID_STOP:

{ if( started )

{ const HWND parent = hDlg;

const UINT timer_id = ID_TIMER;

KillTimer( parent, timer_id ); started = 0; }

return TRUE; }

case ID_EXIT: case IDCANCEL: case IDABORT:

{ EndDialog( hDlg, 0 ); return TRUE; }} break; }}

return FALSE; }

/* end of specific controller */

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram721492 stefan_ram:721492 Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd721492, slrprddef721492, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/c++-windows