├── src ├── application.cpp ├── default_window_class.cpp ├── default_mdichild_class.cpp ├── thread.cpp ├── dialog_enddialog.cpp ├── passlparam.h ├── window_gettext.cpp ├── menu_getstring.cpp ├── listview_getitemtext.cpp ├── regkey_enum.cpp ├── statusbar_gettiptext.cpp ├── windowclass_constructor.cpp ├── toolbar_getbuttontext.cpp ├── statusbar_gettext.cpp ├── tabcontrol_getitemtext.cpp ├── listbox_gettext.cpp ├── treeitem_getitemtext.cpp ├── combobox_getlbtext.cpp ├── comboboxex_getitemtext.cpp ├── edit_getline.cpp ├── dialog_go1.cpp ├── regkey_enumkeys2.cpp ├── dialog_go2.cpp ├── modelessdialog_create1.cpp ├── regkey_queryvalue.cpp ├── modelessdialog_create2.cpp ├── modelessdialog_create3.cpp ├── regkey_deletekeytree.cpp ├── window_class.cpp ├── regkey_enumkeys.cpp ├── winmain.cpp ├── socket.cpp ├── mdiwindow.cpp ├── mdichild.cpp ├── window.cpp ├── dialog.cpp ├── mdichild_defaultproc.cpp └── window_defaultproc.cpp ├── examples ├── dialogs │ ├── resource.h │ ├── dlg_modal.cpp │ ├── dlg_modeless.cpp │ └── dialog.rc ├── multithreading │ ├── resource.h │ ├── Multithreading.cpp │ └── mtdialog.rc ├── sdi.cpp ├── mdi.cpp └── CMakeLists.txt ├── License.txt ├── Readme.md └── CMakeLists.txt /src/application.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | namespace WinAPI { 7 | ApplicationBase* ApplicationBase::p_application = 0; 8 | } 9 | -------------------------------------------------------------------------------- /src/default_window_class.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | namespace WinAPI { 7 | WindowClass wndClassDefault; 8 | } // namespace WinAPI 9 | -------------------------------------------------------------------------------- /src/default_mdichild_class.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | namespace WinAPI { 7 | MDIChildWindowClass mdiChildWndClassDefault; 8 | } // namespace WinAPI 9 | -------------------------------------------------------------------------------- /src/thread.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | DWORD WINAPI Thread::TheProc( LPVOID lpParam ) 7 | { 8 | reinterpret_cast( lpParam )->Run(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /src/dialog_enddialog.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | bool Dialog::EndDialog( int nResult ) 7 | { 8 | if ( ! bModal ) return false; 9 | HWND hwndEnd = hWnd; 10 | hWnd = NULL; 11 | SetWindowLong( hwndEnd, GWL_USERDATA, 0 ); 12 | return ::EndDialog( hwndEnd, nResult ) != FALSE; 13 | } 14 | -------------------------------------------------------------------------------- /src/passlparam.h: -------------------------------------------------------------------------------- 1 | 2 | namespace WinAPI { 3 | 4 | struct PASSLPARAM { 5 | LPVOID lpParam; 6 | GenericWindow* pWindow; 7 | HWND* phwnd; 8 | }; 9 | 10 | inline WPoint LParam2Point( LPARAM lParam ) 11 | { 12 | const POINTS pts = MAKEPOINTS( lParam ); 13 | return WPoint( pts.x, pts.y ); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/window_gettext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring GenericWindow::GetText() const 7 | { 8 | tstring s; 9 | const int len = GetWindowTextLength( hWnd ); 10 | if ( len > 0 ) { 11 | s.resize( len + 1 ); 12 | GetWindowText( hWnd, &s[0], len + 1 ); 13 | s.resize( len ); 14 | } 15 | return s; 16 | } 17 | -------------------------------------------------------------------------------- /src/menu_getstring.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring Menu::GetString( int id, bool b_command ) const 7 | { 8 | const int MAX_CHARS = 256; 9 | tstring s( MAX_CHARS, '\0' ); 10 | const int num_chars = GetMenuString( hMenu, id, &s[0], MAX_CHARS, b_command?MF_BYCOMMAND:MF_BYPOSITION ); 11 | s.resize( num_chars ); 12 | return s; 13 | } 14 | -------------------------------------------------------------------------------- /src/listview_getitemtext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring ListView::GetItemText( int iItem, int iSubItem ) const 7 | { 8 | tstring s; 9 | const int len = 128; 10 | s.resize( len + 1 ); 11 | s[0] = s[len] = 0; 12 | ListView_GetItemText( hWnd, iItem, iSubItem, &s[0], len + 1 ); 13 | s.resize( _tcslen( s.data() ) ); 14 | return s; 15 | } 16 | -------------------------------------------------------------------------------- /src/regkey_enum.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | int RegKey::EnumKey( DWORD dwIndex, tstring* p_name, PFILETIME lpftLastWriteTime ) const 7 | { 8 | p_name->resize( 256 ); 9 | DWORD count = 256; 10 | const int retval = RegEnumKeyEx( hKey, dwIndex, &(*p_name)[0], &count, 0, 0, 0, lpftLastWriteTime ); 11 | p_name->resize( count ); 12 | return retval; 13 | } 14 | -------------------------------------------------------------------------------- /src/statusbar_gettiptext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring StatusBar::GetTipText( int nPart ) const 7 | { 8 | tstring s; 9 | const int len = 128; 10 | s.resize( len + 1 ); 11 | s[0] = s[len] = 0; 12 | SendMessage( SB_GETTIPTEXT, MAKEWPARAM( nPart, len ), reinterpret_cast( &s[0] ) ); 13 | s.resize( _tcslen( s.data() ) ); 14 | return s; 15 | } 16 | -------------------------------------------------------------------------------- /src/windowclass_constructor.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | WindowClass::WindowClass( WNDPROC wndProc, UINT style, int cbClsExtra, int cbWndExtra, HICON hIcon, HCURSOR hCursor, HBRUSH hbrBackground, LPCTSTR lpszMenuName, HINSTANCE hInstance ) 7 | { 8 | Register( wndProc, style, cbClsExtra, cbWndExtra, hIcon, hCursor, hbrBackground, lpszMenuName, hInstance ); 9 | } 10 | -------------------------------------------------------------------------------- /src/toolbar_getbuttontext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring Toolbar::GetButtonText( int idCommand ) const 7 | { 8 | tstring s; 9 | const int len = 128; 10 | s.resize( len + 1 ); 11 | s[0] = s[len] = 0; 12 | const int new_len = SendMessage( TB_GETBUTTONTEXT, idCommand, reinterpret_cast( &s[0] ) ); 13 | s.resize( new_len ); 14 | return s; 15 | } 16 | -------------------------------------------------------------------------------- /src/statusbar_gettext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring StatusBar::GetText( int nPart ) const 7 | { 8 | tstring s; 9 | const int len = LOWORD( SendMessage( SB_GETTEXTLENGTH, nPart ) ); 10 | s.resize( len + 1 ); 11 | s[0] = s[len] = 0; 12 | SendMessage( SB_GETTEXT, nPart, reinterpret_cast( &s[0] ) ); 13 | s.resize( _tcslen( s.data() ) ); 14 | return s; 15 | } 16 | -------------------------------------------------------------------------------- /src/tabcontrol_getitemtext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring TabControl::GetItemText( int nItem ) const 7 | { 8 | tstring s; 9 | const int len = 128; 10 | s.resize( len + 1 ); 11 | s[0] = s[len] = 0; 12 | TCITEM item; 13 | item.mask = TCIF_TEXT; 14 | item.pszText = &s[0]; 15 | item.cchTextMax = len; 16 | GetItem( nItem, &item ); 17 | s.resize( _tcslen( s.data() ) ); 18 | return s; 19 | } 20 | -------------------------------------------------------------------------------- /src/listbox_gettext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring ListBox::GetText( int nIndex ) const 7 | { 8 | tstring s; 9 | const int len = SendMessage( LB_GETTEXTLEN, nIndex ); 10 | if ( len == LB_ERR ) 11 | return _T(""); 12 | s.resize( len + 1 ); 13 | int len2 = SendMessage( LB_GETTEXT, nIndex, reinterpret_cast( &s[0] ) ); 14 | if ( len2 < 0 ) len2 = 0; 15 | s.resize( len2 ); 16 | return s; 17 | } 18 | -------------------------------------------------------------------------------- /src/treeitem_getitemtext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring TreeItem::GetItemText() const 7 | { 8 | tstring s; 9 | const int len = 128; 10 | s.resize( len + 1 ); 11 | s[0] = s[len] = 0; 12 | TVITEM item; 13 | item.mask = TVIF_HANDLE | TVIF_TEXT; 14 | item.hItem = hitem; 15 | item.pszText = &s[0]; 16 | item.cchTextMax = len; 17 | GetItem( &item ); 18 | s.resize( _tcslen( s.data() ) ); 19 | return s; 20 | } 21 | -------------------------------------------------------------------------------- /src/combobox_getlbtext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring ComboBox::GetLBText( int nIndex ) const 7 | { 8 | tstring s; 9 | const int len = SendMessage( CB_GETLBTEXTLEN, nIndex ); 10 | if ( len == CB_ERR ) 11 | return _T(""); 12 | s.resize( len + 1 ); 13 | int len2 = SendMessage( CB_GETLBTEXT, nIndex, reinterpret_cast( &s[0] ) ); 14 | if ( len2 < 0 ) len2 = 0; 15 | s.resize( len2 ); 16 | return s; 17 | } 18 | -------------------------------------------------------------------------------- /src/comboboxex_getitemtext.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring ComboBoxEx::GetItemText( int nItem ) const 7 | { 8 | tstring s; 9 | const int len = 128; 10 | s.resize( len + 1 ); 11 | s[0] = s[len] = 0; 12 | COMBOBOXEXITEM item; 13 | item.mask = CBEIF_TEXT; 14 | item.iItem = nItem; 15 | item.pszText = &s[0]; 16 | item.cchTextMax = len; 17 | GetItem( &item ); 18 | s.resize( _tcslen( s.data() ) ); 19 | return s; 20 | } 21 | -------------------------------------------------------------------------------- /src/edit_getline.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | tstring Edit::GetLine( int nLine ) const 7 | { 8 | tstring s; 9 | const int len = LineLength( LineIndex( nLine ) ); 10 | if ( len < 1 ) return s; 11 | s.resize( len + 1 ); 12 | *reinterpret_cast( &s[0] ) = static_cast( len + 1 ); 13 | const int len2 = SendMessage( EM_GETLINE, nLine, reinterpret_cast( &s[0] ) ); 14 | s.resize( len ); 15 | return s; 16 | } 17 | -------------------------------------------------------------------------------- /src/dialog_go1.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | BOOL CALLBACK WinAPIDefaultDlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ); 8 | 9 | int Dialog::Go( HWND hwndParent, LPARAM lParam, HINSTANCE hInstance ) 10 | { 11 | if ( ! bModal ) return -1; 12 | PASSLPARAM pass = { reinterpret_cast( lParam ), this, &hWnd }; 13 | return static_cast( DialogBoxParam( hInstance, lpTemplate, hwndParent, WinAPIDefaultDlgProc, reinterpret_cast( &pass ) ) ); 14 | } 15 | -------------------------------------------------------------------------------- /examples/dialogs/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by dialog.rc 4 | // 5 | #define IDD_DIALOG1 101 6 | #define IDD_SAMPLE 101 7 | 8 | // Next default values for new objects 9 | // 10 | #ifdef APSTUDIO_INVOKED 11 | #ifndef APSTUDIO_READONLY_SYMBOLS 12 | #define _APS_NEXT_RESOURCE_VALUE 102 13 | #define _APS_NEXT_COMMAND_VALUE 40001 14 | #define _APS_NEXT_CONTROL_VALUE 1001 15 | #define _APS_NEXT_SYMED_VALUE 101 16 | #endif 17 | #endif 18 | -------------------------------------------------------------------------------- /src/regkey_enumkeys2.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | int RegKey::EnumKeys( vector* p_keys ) const 7 | { 8 | p_keys->clear(); 9 | DWORD num_sub_keys = 0; 10 | int retval = QueryInfo( &num_sub_keys ); 11 | if ( retval != ERROR_SUCCESS ) return retval; 12 | p_keys->reserve( num_sub_keys ); 13 | tstring name; 14 | for ( int i=num_sub_keys-1; i >= 0; i-- ) { 15 | retval = EnumKey( i, &name ); 16 | if ( retval != ERROR_SUCCESS ) return retval; 17 | p_keys->push_back( name ); 18 | } 19 | return ERROR_SUCCESS; 20 | } 21 | -------------------------------------------------------------------------------- /examples/multithreading/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by mtdialog.rc 4 | // 5 | #define IDD_SAMPLE 101 6 | #define IDC_PROGRESS 1001 7 | 8 | // Next default values for new objects 9 | // 10 | #ifdef APSTUDIO_INVOKED 11 | #ifndef APSTUDIO_READONLY_SYMBOLS 12 | #define _APS_NEXT_RESOURCE_VALUE 102 13 | #define _APS_NEXT_COMMAND_VALUE 40001 14 | #define _APS_NEXT_CONTROL_VALUE 1002 15 | #define _APS_NEXT_SYMED_VALUE 101 16 | #endif 17 | #endif 18 | -------------------------------------------------------------------------------- /src/dialog_go2.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | BOOL CALLBACK WinAPIDefaultDlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ); 8 | 9 | int Dialog::Go( LPCDLGTEMPLATE hDialogTemplate, HWND hwndParent, LPARAM lParam, HINSTANCE hInstance ) 10 | { 11 | if ( ! bModal ) return -1; 12 | PASSLPARAM pass = { reinterpret_cast( lParam ), this, &hWnd }; 13 | return static_cast( DialogBoxIndirectParam( hInstance, hDialogTemplate, hwndParent, WinAPIDefaultDlgProc, reinterpret_cast( &pass ) ) ); 14 | } 15 | -------------------------------------------------------------------------------- /src/modelessdialog_create1.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | BOOL CALLBACK WinAPIDefaultDlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ); 8 | 9 | void ModelessDialog::Create( LPCTSTR _lpTemplate, HWND hWndParent, LPARAM lParam, HINSTANCE hInstance ) 10 | { 11 | lpTemplate = _lpTemplate; 12 | PASSLPARAM pass = { reinterpret_cast( lParam ), this, &hWnd }; 13 | CreateDialogParam( hInstance, lpTemplate, hWndParent, WinAPIDefaultDlgProc, reinterpret_cast( &pass ) ); 14 | b_created = true; 15 | } 16 | -------------------------------------------------------------------------------- /src/regkey_queryvalue.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | int RegKey::QueryValue( LPCTSTR lpValueName, tstring* p_string, LPDWORD lpType ) const 7 | { 8 | *p_string = _T(""); 9 | DWORD data_size = 0; 10 | const int retval = QueryValueSize( lpValueName, &data_size, lpType ); 11 | if ( retval != ERROR_SUCCESS ) return retval; 12 | p_string->resize( data_size ); 13 | const int retval2 = QueryValue( lpValueName, &(*p_string)[0], &data_size ); 14 | if ( retval2 == ERROR_SUCCESS ) p_string->resize( p_string->length() - 1 ); 15 | return retval2; 16 | } 17 | -------------------------------------------------------------------------------- /src/modelessdialog_create2.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | BOOL CALLBACK WinAPIDefaultDlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ); 8 | 9 | void ModelessDialog::Create( int _lpTemplate, HWND hWndParent, LPARAM lParam, HINSTANCE hInstance ) 10 | { 11 | lpTemplate = MAKEINTRESOURCE( _lpTemplate ); 12 | PASSLPARAM pass = { reinterpret_cast( lParam ), this, &hWnd }; 13 | CreateDialogParam( hInstance, lpTemplate, hWndParent, WinAPIDefaultDlgProc, reinterpret_cast( &pass ) ); 14 | b_created = true; 15 | } 16 | -------------------------------------------------------------------------------- /src/modelessdialog_create3.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | BOOL CALLBACK WinAPIDefaultDlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ); 8 | 9 | void ModelessDialog::Create( LPCDLGTEMPLATE hDialogTemplate, HWND hWndParent, LPARAM lParam, HINSTANCE hInstance ) 10 | { 11 | lpTemplate = 0; 12 | PASSLPARAM pass = { reinterpret_cast( lParam ), this, &hWnd }; 13 | CreateDialogIndirectParam( hInstance, hDialogTemplate, hWndParent, WinAPIDefaultDlgProc, reinterpret_cast( &pass ) ); 14 | b_created = true; 15 | } 16 | -------------------------------------------------------------------------------- /src/regkey_deletekeytree.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | int RegKey::DeleteKeyTree( LPCTSTR lpSubKey ) 7 | { 8 | RegKey subkey; 9 | const int retval = subkey.Open( *this, lpSubKey ); 10 | if ( retval != ERROR_SUCCESS ) 11 | return retval; 12 | TCHAR name[ 256 ]; 13 | for ( int i=0; ; i++ ) { 14 | DWORD name_size = 256; 15 | if ( RegEnumKeyEx( subkey, i, name, &name_size, 0, 0, 0, 0 ) != ERROR_SUCCESS ) 16 | break; 17 | const int retval = subkey.DeleteKeyTree( name ); 18 | if ( retval != ERROR_SUCCESS ) 19 | return retval; 20 | } 21 | subkey.Close(); 22 | return Delete( lpSubKey ); 23 | } 24 | -------------------------------------------------------------------------------- /src/window_class.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | void WindowClass::Register( WNDPROC wndProc, UINT style, int cbClsExtra, int cbWndExtra, HICON hIcon, HCURSOR hCursor, HBRUSH hbrBackground, LPCTSTR lpszMenuName, HINSTANCE hInstance ) 7 | { 8 | static int cls_index = 0; 9 | cls_index++; 10 | wsprintf( szClassName, _T("Class%08X"), cls_index ); 11 | WNDCLASS wc = { 12 | style, 13 | wndProc, 14 | cbClsExtra, 15 | cbWndExtra, 16 | hInstance, 17 | hIcon, 18 | hCursor, 19 | hbrBackground, 20 | lpszMenuName, 21 | szClassName 22 | }; 23 | if ( RegisterClass( &wc ) == 0 ) 24 | szClassName[0] = 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/regkey_enumkeys.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | int RegKey::EnumKeys( ENUMKEYS* p_keys ) const 7 | { 8 | p_keys->clear(); 9 | DWORD num_sub_keys = 0; 10 | int retval = QueryInfo( &num_sub_keys ); 11 | if ( retval != ERROR_SUCCESS ) return retval; 12 | p_keys->reserve( num_sub_keys ); 13 | for ( int i=num_sub_keys-1; i >= 0; i-- ) { 14 | ENUMKEY ek; 15 | ek.name.resize( 256 ); 16 | ek.key_class.resize( 256 ); 17 | DWORD name_len = 256; 18 | DWORD key_class_len = 256; 19 | retval = RegEnumKeyEx( hKey, i, &ek.name[0], &name_len, 0, &ek.key_class[0], &key_class_len, &ek.last_file_time ); 20 | if ( retval != ERROR_SUCCESS ) return retval; 21 | p_keys->push_back( ek ); 22 | } 23 | return ERROR_SUCCESS; 24 | } 25 | -------------------------------------------------------------------------------- /examples/sdi.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Include main WinAPI Wrapper header 3 | #include 4 | 5 | // Include the WinAPI Wrapper namespace 6 | using namespace WinAPI; 7 | 8 | // Define main window class based on a regular window class 9 | class MainWindow: public Window { 10 | public: 11 | // Constructor creates the actual window (through CreateWindowEx) 12 | MainWindow() { 13 | Create( _T("Sample App") ); 14 | } 15 | // During window destruction the message queue is stopped 16 | virtual void OnDestroy() { 17 | PostQuitMessage( 0 ); 18 | } 19 | // Paint "Hello, World!" during WM_PAINT 20 | virtual void OnPaint() { 21 | PaintDC( *this ).TextOut( WPoint(50,50), _T("Hello, World!") ); 22 | } 23 | }; 24 | 25 | // The application object using the main window class 26 | namespace { 27 | Application< MainWindow > app; 28 | } 29 | 30 | USE_LIB_WINMAIN 31 | -------------------------------------------------------------------------------- /examples/dialogs/dlg_modal.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Include main WinAPI Wrapper header 3 | #include 4 | // Include resource header 5 | #include "resource.h" 6 | 7 | // Include the WinAPI Wrapper namespace 8 | using namespace WinAPI; 9 | 10 | // Define main window class based on a modal dialog class 11 | class MainWindow: public Dialog { 12 | public: 13 | // Constructor sets up dialog box template 14 | MainWindow(): Dialog( IDD_SAMPLE ) { } 15 | // If IDOK or IDCANCEL comes from the dialog - end it 16 | virtual void OnCommand( int nIdentifier, int nNotifyCode, HWND hwndControl ) { 17 | switch ( nIdentifier ) { 18 | 19 | case IDOK: 20 | case IDCANCEL: 21 | EndDialog( nIdentifier ); 22 | 23 | } 24 | } 25 | }; 26 | 27 | // Main window function that displays the modal dialog 28 | int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) 29 | { 30 | MainWindow dlg; 31 | dlg.Go(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /examples/dialogs/dlg_modeless.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Include main WinAPI Wrapper header 3 | #include 4 | // Include resource header 5 | #include "resource.h" 6 | 7 | // Include the WinAPI Wrapper namespace 8 | using namespace WinAPI; 9 | 10 | // Define main window class based on a modeless dialog class 11 | class MainWindow: public ModelessDialog { 12 | public: 13 | // Constructor creates the actual window from a dialog resource 14 | MainWindow( HWND hwndParent = 0 ) { 15 | Create( IDD_SAMPLE, hwndParent ); 16 | } 17 | // During window destruction the message queue is stopped 18 | virtual void OnDestroy() { 19 | PostQuitMessage( 0 ); 20 | } 21 | // If IDOK or IDCANCEL comes from the dialog - end it 22 | virtual void OnCommand( int nIdentifier, int nNotifyCode, HWND hwndControl ) { 23 | switch ( nIdentifier ) { 24 | 25 | case IDOK: 26 | case IDCANCEL: 27 | Destroy(); 28 | 29 | } 30 | } 31 | }; 32 | 33 | // The application object using the main window class 34 | namespace { 35 | Application< MainWindow, ModelessDialogAccel< MainWindow > > app; 36 | } 37 | 38 | USE_LIB_WINMAIN 39 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | 2 | WINAPI WRAPPER LIBRARY LICENSE AGREEMENT 3 | 4 | Copyright (c) 2001-2003 Chris Dragan. All rights reserved. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), 8 | to deal in the Software without restriction, including without limitation 9 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | and/or sell copies of the Software, and to permit persons to whom 11 | the Software is furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included 14 | in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 21 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 22 | THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /src/winmain.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | #ifndef MINGW 5 | #include 6 | #endif 7 | 8 | namespace WinAPI { 9 | 10 | // 11 | // Default module handle. The old code used a hardcoded value assuming it 12 | // wouldn't change for the main executable but unfortunatly it eventually 13 | // did. 14 | // 15 | HINSTANCE g_hInstance = GetModuleHandle(NULL); 16 | 17 | // 18 | // Defining WinMain here complicates builds with g++ because unresolved symbols 19 | // are only searched for in libraries linked after where they appear. Because 20 | // the CRT is supposed to call WinMain, it can't see a symbol defined here. 21 | // 22 | // Instead, use the macro - USE_LIB_WINMAIN - to define WinMain in your exe 23 | // code if you don't want to define your own. You had to write: 24 | // Application< MainWindow > app; 25 | // anyway so this isn't a major change. 26 | // 27 | int WINAPI Lib_WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) 28 | { 29 | #ifndef MINGW 30 | _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); 31 | #endif 32 | ApplicationBase* p_application = ApplicationBase::p_application; 33 | if ( p_application != 0 ) 34 | p_application->Run(); 35 | return 0; 36 | } 37 | 38 | } // namespace WinAPI 39 | -------------------------------------------------------------------------------- /src/socket.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | namespace { 7 | 8 | class WindowsSockets { 9 | bool b_init_ok; 10 | 11 | WindowsSockets(): b_init_ok( false ) { 12 | WSADATA wsa; 13 | b_init_ok = WSAStartup( 0x0202, &wsa ) == 0; 14 | } 15 | public: 16 | ~WindowsSockets() { WSACleanup(); } // made public for VS6 to compile 17 | static bool IsInitialized() { 18 | static WindowsSockets sockets; 19 | return sockets.b_init_ok; 20 | } 21 | }; 22 | 23 | } 24 | 25 | Socket::Socket( bool b_udp ) 26 | : socket( empty ) 27 | { 28 | const int type = b_udp ? SOCK_DGRAM : SOCK_STREAM; 29 | 30 | if ( WindowsSockets::IsInitialized() ) 31 | socket = static_cast( ::socket( AF_INET, type, 0 ) ); 32 | if ( socket >= 0 ) { 33 | int bdcast = 1; 34 | setsockopt( socket, SOL_SOCKET, SO_BROADCAST, reinterpret_cast( &bdcast ), sizeof( bdcast ) ); 35 | } 36 | } 37 | 38 | Socket::Socket( int type, int protocol ) 39 | : socket( empty ) 40 | { 41 | if ( WindowsSockets::IsInitialized() ) 42 | socket = static_cast( ::socket( AF_INET, type, protocol ) ); 43 | if ( socket >= 0 ) { 44 | int bdcast = 1; 45 | setsockopt( socket, SOL_SOCKET, SO_BROADCAST, reinterpret_cast( &bdcast ), sizeof( bdcast ) ); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # WinAPI-Wrapper for C++ 2 | *Forked from [cdragan/WinAPI-Wrapper](https://github.com/cdragan/WinAPI-Wrapper) with some minor compatability fixes and a switch to the CMAKE build system.* 3 | 4 | This is a C++ wrapper for WINAPI to take some of the clumsiness out of programming WIN32 applications. Most classes wrap the calls to the WINAPI functions so that they keep track of the handles for you and define some sensible defaults. 5 | 6 | Almost all the documentation in the origional repo help dir still applies and is recommended reading. However a major change is that the library default `WinMain` function must now be defined in your code for compatability with non MSCV linkers. You can easiliy do this using the `USE_LIB_WINMAIN` macro. For example the `sdi.cpp` example app is now: 7 | 8 | ```C++ 9 | // Include main WinAPI Wrapper header 10 | #include 11 | 12 | // Include the WinAPI Wrapper namespace 13 | using namespace WinAPI; 14 | 15 | // Define main window class based on a regular window class 16 | class MainWindow: public Window { 17 | public: 18 | // Constructor creates the actual window (through CreateWindowEx) 19 | MainWindow() { 20 | Create( "Sample App" ); 21 | } 22 | // During window destruction the message queue is stopped 23 | virtual void OnDestroy() { 24 | PostQuitMessage( 0 ); 25 | } 26 | // Paint "Hello, World!" during WM_PAINT 27 | virtual void OnPaint() { 28 | PaintDC( *this ).TextOut( WPoint(50,50), "Hello, World!" ); 29 | } 30 | }; 31 | 32 | // The application object using the main window class 33 | namespace { 34 | Application< MainWindow > app; 35 | } 36 | 37 | USE_LIB_WINMAIN 38 | ``` 39 | This example assumes the UNICODE macro isn't defined for simplicity's sake. The real example supports ASCII and and UNICODE chars. 40 | 41 | All modifications are made available under the origional license of the library (see `license.txt`). -------------------------------------------------------------------------------- /examples/mdi.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Include main WinAPI Wrapper header 3 | #include 4 | 5 | // Include the WinAPI Wrapper namespace 6 | using namespace WinAPI; 7 | 8 | // Define MDI child window class 9 | class DocumentWindow: public MDIChildWindow { 10 | public: 11 | // Constructor creates the actual window 12 | DocumentWindow( HWND hwndParent ) { 13 | Create( _T("Document"), hwndParent ); 14 | } 15 | // Paint "Hello, World!" during WM_PAINT 16 | virtual void OnPaint() { 17 | PaintDC( *this ).TextOut( WPoint(50,50), _T("Hello, World!") ); 18 | } 19 | }; 20 | 21 | // Define main window class based on a MDI frame window class 22 | class MainWindow: public MDIFrameWindow { 23 | public: 24 | // Constructor creates the actual window (through CreateWindowEx) 25 | MainWindow() { 26 | Create( _T("Sample App") ); 27 | } 28 | // Command that creates MDI children 29 | enum { ID_CREATE = 101 }; 30 | // During window creation, create the client window and a sample child 31 | virtual bool OnCreate( LPCREATESTRUCT lpCreateStruct = 0 ) { 32 | // Create menu bar 33 | Menu menubar; 34 | menubar.Create(); 35 | Menu submenu; 36 | submenu.Create(); 37 | submenu.Append( _T("&Create"), ID_CREATE ); 38 | menubar.Append( _T("&Window"), submenu ); 39 | SetMenu( menubar ); 40 | 41 | // Create client window and an MDI child 42 | CreateClient( GetClientRect(), submenu ); 43 | new DocumentWindow( GetClientWindow() ); 44 | return true; 45 | } 46 | // During window destruction the message queue is stopped 47 | virtual void OnDestroy() { 48 | PostQuitMessage( 0 ); 49 | } 50 | // WM_COMMAND 51 | virtual void OnCommand( int nIdentifier, int nNotifyCode, HWND hwndControl ) { 52 | switch ( nIdentifier ) { 53 | 54 | // Create a new document window 55 | case ID_CREATE: 56 | new DocumentWindow( GetClientWindow() ); 57 | break; 58 | 59 | // Default WM_COMMAND processing (for MDI commands) 60 | default: 61 | MDIFrameWindow::OnCommand( nIdentifier, nNotifyCode, hwndControl ); 62 | 63 | } 64 | } 65 | }; 66 | 67 | // The application object using the main window class 68 | namespace { 69 | Application< MainWindow, MDIAccel< MainWindow > > app; 70 | } 71 | 72 | USE_LIB_WINMAIN 73 | -------------------------------------------------------------------------------- /examples/multithreading/Multithreading.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Include main WinAPI Wrapper header 3 | #include 4 | // Include resource header 5 | #include "resource.h" 6 | 7 | // Include the WinAPI Wrapper namespace 8 | using namespace WinAPI; 9 | 10 | // Define main window class based on a modeless dialog class and on a thread class 11 | class MainWindow: public ModelessDialog, public Thread { 12 | ProgressBar progress; // the progress bar control 13 | bool b_interrupt; // task interrupt flag (user's Cancel button) 14 | public: 15 | // Constructor 16 | MainWindow( HWND hwndParent = 0 ): b_interrupt( false ) { 17 | 18 | // Load common controls (for the progress bar) 19 | INITCOMMONCONTROLSEX icc = { sizeof(INITCOMMONCONTROLSEX), ICC_PROGRESS_CLASS }; 20 | InitCommonControlsEx( &icc ); 21 | 22 | // Create the thread in suspended state 23 | Thread::Create( CREATE_SUSPENDED ); 24 | 25 | // Create the actual window from a dialog resource 26 | ModelessDialog::Create( IDD_SAMPLE, hwndParent ); 27 | } 28 | // Initialize dialog 29 | virtual bool OnInitDialog( HWND hwndFocus, LPARAM lParam ) { 30 | 31 | // Save progress bar control's HWND 32 | progress.Assign( GetDlgItem( IDC_PROGRESS ) ); 33 | 34 | // Run the created thread 35 | Resume(); 36 | return true; 37 | } 38 | // During window destruction the message queue is stopped 39 | virtual void OnDestroy() { 40 | PostQuitMessage( 0 ); 41 | } 42 | // Window exit command 43 | enum { ID_FINISHED = 102 }; 44 | // Handle WM_COMMAND 45 | virtual void OnCommand( int nIdentifier, int nNotifyCode, HWND hwndControl ) { 46 | switch ( nIdentifier ) { 47 | 48 | // The task has been finished - close window 49 | case ID_FINISHED: 50 | Destroy(); 51 | break; 52 | 53 | // The user cancelled the task - signal an interrupt 54 | case IDCANCEL: 55 | b_interrupt = true; 56 | break; 57 | 58 | } 59 | } 60 | // The actual thread 61 | virtual void Run() { 62 | progress.SetRange( 0, 100 ); 63 | for ( int i=0; i < 100; i++ ) { 64 | 65 | // Simulate one step of a task (by Sleep()ing) 66 | Sleep( 50 ); 67 | 68 | // Display progress 69 | progress.SetPos( i+1 ); 70 | 71 | // Check if the user requested an interrupt 72 | if ( b_interrupt ) 73 | break; 74 | } 75 | 76 | // Task finished - close the window 77 | PostMessage( WM_COMMAND, ID_FINISHED ); 78 | } 79 | }; 80 | 81 | // The application object using the main window class 82 | namespace { 83 | Application< MainWindow, ModelessDialogAccel< MainWindow > > app; 84 | } 85 | 86 | USE_LIB_WINMAIN 87 | -------------------------------------------------------------------------------- /src/mdiwindow.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | 4 | using namespace WinAPI; 5 | 6 | MDIFrameWindow::~MDIFrameWindow() 7 | { 8 | } 9 | 10 | bool MDIFrameWindow::CreateClient( const RECT& rc, HMENU hMenu, UINT idFirstChild, DWORD dwStyle, DWORD dwExStyle ) 11 | { 12 | if ( ! client_wnd.IsCreated() ) { 13 | CLIENTCREATESTRUCT ccs = { hMenu, idFirstChild }; 14 | return client_wnd.Create( _T(""), _T("MDICLIENT"), dwStyle, rc, *this, 0, dwExStyle, &ccs ); 15 | } else 16 | return true; 17 | } 18 | 19 | int MDIFrameWindow::HandleMessage( int uMsg, int wParam, int lParam ) 20 | { 21 | return static_cast( DefFrameProc( hWnd, client_wnd, uMsg, wParam, lParam ) ); 22 | } 23 | 24 | #ifndef PURE_WRAPPER 25 | 26 | void MDIFrameWindow::OnClose() 27 | { 28 | DefFrameProc( hWnd, client_wnd, WM_CLOSE, 0, 0 ); 29 | } 30 | 31 | int MDIFrameWindow::OnNotify( int nIdentifier, LPNMHDR pnmh ) 32 | { 33 | return static_cast( DefFrameProc( hWnd, client_wnd, WM_NOTIFY, nIdentifier, reinterpret_cast( pnmh ) ) ); 34 | } 35 | 36 | void MDIFrameWindow::OnCommand( int nIdentifier, int nNotifyCode, HWND hwndControl ) 37 | { 38 | DefFrameProc( hWnd, client_wnd, WM_COMMAND, MAKEWPARAM( nIdentifier, nNotifyCode ), windows_cast( hwndControl ) ); 39 | } 40 | 41 | void MDIFrameWindow::OnPaint() 42 | { 43 | DefFrameProc( hWnd, client_wnd, WM_PAINT, 0, 0 ); 44 | } 45 | 46 | void MDIFrameWindow::OnSize( int sizing, WSize new_size ) 47 | { 48 | DefFrameProc( hWnd, client_wnd, WM_SIZE, sizing, MAKELPARAM( new_size.cx, new_size.cy ) ); 49 | } 50 | 51 | void MDIFrameWindow::OnMouseMove( WPoint point, int keys ) 52 | { 53 | DefFrameProc( hWnd, client_wnd, WM_MOUSEMOVE, keys, MAKELPARAM( point.x, point.y ) ); 54 | } 55 | 56 | void MDIFrameWindow::OnMouseDown( WPoint point, int keys, int button ) 57 | { 58 | DefFrameProc( hWnd, client_wnd, button==left_button ? WM_LBUTTONDOWN : (button==right_button ? WM_RBUTTONDOWN : WM_MBUTTONDOWN), keys, MAKELPARAM( point.x, point.y ) ); 59 | } 60 | 61 | void MDIFrameWindow::OnMouseUp( WPoint point, int keys, int button ) 62 | { 63 | DefFrameProc( hWnd, client_wnd, button==left_button ? WM_LBUTTONUP : (button==right_button ? WM_RBUTTONUP : WM_MBUTTONUP), keys, MAKELPARAM( point.x, point.y ) ); 64 | } 65 | 66 | void MDIFrameWindow::OnMouseDblClk( WPoint point, int keys, int button ) 67 | { 68 | DefFrameProc( hWnd, client_wnd, button==left_button ? WM_LBUTTONDOWN : (button==right_button ? WM_RBUTTONDOWN : WM_MBUTTONDOWN), keys, MAKELPARAM( point.x, point.y ) ); 69 | } 70 | 71 | void MDIFrameWindow::OnKeyDown( int key, int keyData ) 72 | { 73 | DefFrameProc( hWnd, client_wnd, WM_KEYDOWN, key, keyData ); 74 | } 75 | 76 | void MDIFrameWindow::OnKeyUp( int key, int keyData ) 77 | { 78 | DefFrameProc( hWnd, client_wnd, WM_KEYUP, key, keyData ); 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/mdichild.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | bool MDIChildWindow::Create( LPCTSTR lpszWindowName, const MDIChildWindowClass& wndClass, 8 | HWND hWndParent, const RECT& rc, HMENU hMenu, DWORD dwStyle, 9 | DWORD dwExStyle, LPVOID lpParam, HINSTANCE hInstance ) 10 | { 11 | PASSLPARAM pass = { lpParam, this, &hWnd }; 12 | const TCHAR* class_name = wndClass; 13 | // the const_cast is a VS6 backwards compatibility issue 14 | CreateMDIWindow( const_cast(class_name), const_cast(lpszWindowName), dwStyle, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hWndParent, hInstance, reinterpret_cast( &pass ) ); 15 | b_created = GenericWindow::IsCreated(); 16 | return b_created; 17 | } 18 | 19 | MDIChildWindow::~MDIChildWindow() 20 | { 21 | } 22 | 23 | int MDIChildWindow::HandleMessage( int uMsg, int wParam, int lParam ) 24 | { 25 | return static_cast( DefMDIChildProc( hWnd, uMsg, wParam, lParam ) ); 26 | } 27 | 28 | #ifndef PURE_WRAPPER 29 | 30 | void MDIChildWindow::OnClose() 31 | { 32 | DefMDIChildProc( hWnd, WM_CLOSE, 0, 0 ); 33 | } 34 | 35 | int MDIChildWindow::OnNotify( int nIdentifier, LPNMHDR pnmh ) 36 | { 37 | return static_cast( DefMDIChildProc( hWnd, WM_NOTIFY, nIdentifier, reinterpret_cast( pnmh ) ) ); 38 | } 39 | 40 | void MDIChildWindow::OnPaint() 41 | { 42 | DefMDIChildProc( hWnd, WM_PAINT, 0, 0 ); 43 | } 44 | 45 | void MDIChildWindow::OnSize( int sizing, WSize new_size ) 46 | { 47 | DefMDIChildProc( hWnd, WM_SIZE, sizing, MAKELPARAM( new_size.cx, new_size.cy ) ); 48 | } 49 | 50 | void MDIChildWindow::OnMouseMove( WPoint point, int keys ) 51 | { 52 | DefMDIChildProc( hWnd, WM_MOUSEMOVE, keys, MAKELPARAM( point.x, point.y ) ); 53 | } 54 | 55 | void MDIChildWindow::OnMouseDown( WPoint point, int keys, int button ) 56 | { 57 | DefMDIChildProc( hWnd, button==left_button ? WM_LBUTTONDOWN : (button==right_button ? WM_RBUTTONDOWN : WM_MBUTTONDOWN), keys, MAKELPARAM( point.x, point.y ) ); 58 | } 59 | 60 | void MDIChildWindow::OnMouseUp( WPoint point, int keys, int button ) 61 | { 62 | DefMDIChildProc( hWnd, button==left_button ? WM_LBUTTONUP : (button==right_button ? WM_RBUTTONUP : WM_MBUTTONUP), keys, MAKELPARAM( point.x, point.y ) ); 63 | } 64 | 65 | void MDIChildWindow::OnMouseDblClk( WPoint point, int keys, int button ) 66 | { 67 | DefMDIChildProc( hWnd, button==left_button ? WM_LBUTTONDOWN : (button==right_button ? WM_RBUTTONDOWN : WM_MBUTTONDOWN), keys, MAKELPARAM( point.x, point.y ) ); 68 | } 69 | 70 | void MDIChildWindow::OnKeyDown( int key, int keyData ) 71 | { 72 | DefMDIChildProc( hWnd, WM_KEYDOWN, key, keyData ); 73 | } 74 | 75 | void MDIChildWindow::OnKeyUp( int key, int keyData ) 76 | { 77 | DefMDIChildProc( hWnd, WM_KEYUP, key, keyData ); 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /src/window.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | bool Window::Create( LPCTSTR lpszWindowName, const WindowClass& wndClass, 8 | DWORD dwStyle, const RECT& rc, HWND hWndParent, HMENU hMenu, 9 | DWORD dwExStyle, LPVOID lpParam, HINSTANCE hInstance ) 10 | { 11 | PASSLPARAM pass = { lpParam, this, &hWnd }; 12 | GenericWindow::Create( lpszWindowName, wndClass, dwStyle, rc, hWndParent, hMenu, dwExStyle, reinterpret_cast( &pass ), hInstance ); 13 | b_created = GenericWindow::IsCreated(); 14 | return b_created; 15 | } 16 | 17 | Window::~Window() 18 | { 19 | b_created = false; 20 | if ( hWnd != NULL ) 21 | SetWindowLong( hWnd, GWL_USERDATA, 0 ); 22 | } 23 | 24 | int Window::HandleMessage( int uMsg, int wParam, int lParam ) 25 | { 26 | return static_cast( DefWindowProc( hWnd, uMsg, wParam, lParam ) ); 27 | } 28 | 29 | #ifndef PURE_WRAPPER 30 | 31 | bool Window::OnCreate( LPCREATESTRUCT lpCreateStruct ) 32 | { 33 | return true; 34 | } 35 | 36 | void Window::OnClose() 37 | { 38 | DefWindowProc( hWnd, WM_CLOSE, 0, 0 ); 39 | } 40 | 41 | void Window::OnDestroy() 42 | { 43 | } 44 | 45 | void Window::OnCommand( int nIdentifier, int nNotifyCode, HWND hwndControl ) 46 | { 47 | } 48 | 49 | int Window::OnNotify( int nIdentifier, LPNMHDR pnmh ) 50 | { 51 | return static_cast( DefWindowProc( hWnd, WM_NOTIFY, nIdentifier, reinterpret_cast( pnmh ) ) ); 52 | } 53 | 54 | void Window::OnPaint() 55 | { 56 | DefWindowProc( hWnd, WM_PAINT, 0, 0 ); 57 | } 58 | 59 | void Window::OnSize( int sizing, WSize new_size ) 60 | { 61 | DefWindowProc( hWnd, WM_SIZE, sizing, MAKELPARAM( new_size.cx, new_size.cy ) ); 62 | } 63 | 64 | void Window::OnMouseMove( WPoint point, int keys ) 65 | { 66 | DefWindowProc( hWnd, WM_MOUSEMOVE, keys, MAKELPARAM( point.x, point.y ) ); 67 | } 68 | 69 | void Window::OnMouseDown( WPoint point, int keys, int button ) 70 | { 71 | DefWindowProc( hWnd, button==left_button ? WM_LBUTTONDOWN : (button==right_button ? WM_RBUTTONDOWN : WM_MBUTTONDOWN), keys, MAKELPARAM( point.x, point.y ) ); 72 | } 73 | 74 | void Window::OnMouseUp( WPoint point, int keys, int button ) 75 | { 76 | DefWindowProc( hWnd, button==left_button ? WM_LBUTTONUP : (button==right_button ? WM_RBUTTONUP : WM_MBUTTONUP), keys, MAKELPARAM( point.x, point.y ) ); 77 | } 78 | 79 | void Window::OnMouseDblClk( WPoint point, int keys, int button ) 80 | { 81 | DefWindowProc( hWnd, button==left_button ? WM_LBUTTONDOWN : (button==right_button ? WM_RBUTTONDOWN : WM_MBUTTONDOWN), keys, MAKELPARAM( point.x, point.y ) ); 82 | } 83 | 84 | void Window::OnKeyDown( int key, int keyData ) 85 | { 86 | DefWindowProc( hWnd, WM_KEYDOWN, key, keyData ); 87 | } 88 | 89 | void Window::OnKeyUp( int key, int keyData ) 90 | { 91 | DefWindowProc( hWnd, WM_KEYUP, key, keyData ); 92 | } 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================= 2 | # winapi library 3 | # 4 | # target names: winapi, winapi_examples (not built by default) 5 | # 6 | cmake_minimum_required (VERSION 3.4) 7 | 8 | set (WINAPI_PROJECT_NAME winapi) 9 | 10 | 11 | 12 | #----------------------------------------------------------------------------- 13 | # Add the examples as targets to the winapi_examples pseudo-target 14 | set (EXAMPLES_TARGET_NAMES ) 15 | add_subdirectory (examples) 16 | add_custom_target("${WINAPI_PROJECT_NAME}_examples") 17 | add_dependencies("${WINAPI_PROJECT_NAME}_examples" ${EXAMPLES_TARGET_NAMES}) 18 | 19 | 20 | 21 | #----------------------------------------------------------------------------- 22 | # Create the library target 23 | project (${WINAPI_PROJECT_NAME}) 24 | 25 | set (PROJECT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) 26 | set (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) 27 | 28 | set (PROJECT_SRCS 29 | ${PROJECT_SOURCE_DIR}/application.cpp 30 | ${PROJECT_SOURCE_DIR}/comboboxex_getitemtext.cpp 31 | ${PROJECT_SOURCE_DIR}/combobox_getlbtext.cpp 32 | ${PROJECT_SOURCE_DIR}/default_mdichild_class.cpp 33 | ${PROJECT_SOURCE_DIR}/default_window_class.cpp 34 | ${PROJECT_SOURCE_DIR}/dialog.cpp 35 | ${PROJECT_SOURCE_DIR}/dialog_enddialog.cpp 36 | ${PROJECT_SOURCE_DIR}/dialog_go1.cpp 37 | ${PROJECT_SOURCE_DIR}/dialog_go2.cpp 38 | ${PROJECT_SOURCE_DIR}/edit_getline.cpp 39 | ${PROJECT_SOURCE_DIR}/listbox_gettext.cpp 40 | ${PROJECT_SOURCE_DIR}/listview_getitemtext.cpp 41 | ${PROJECT_SOURCE_DIR}/mdichild.cpp 42 | ${PROJECT_SOURCE_DIR}/mdichild_defaultproc.cpp 43 | ${PROJECT_SOURCE_DIR}/mdiwindow.cpp 44 | ${PROJECT_SOURCE_DIR}/menu_getstring.cpp 45 | ${PROJECT_SOURCE_DIR}/modelessdialog_create1.cpp 46 | ${PROJECT_SOURCE_DIR}/modelessdialog_create2.cpp 47 | ${PROJECT_SOURCE_DIR}/modelessdialog_create3.cpp 48 | ${PROJECT_SOURCE_DIR}/regkey_deletekeytree.cpp 49 | ${PROJECT_SOURCE_DIR}/regkey_enum.cpp 50 | ${PROJECT_SOURCE_DIR}/regkey_enumkeys.cpp 51 | ${PROJECT_SOURCE_DIR}/regkey_enumkeys2.cpp 52 | ${PROJECT_SOURCE_DIR}/regkey_queryvalue.cpp 53 | ${PROJECT_SOURCE_DIR}/socket.cpp 54 | ${PROJECT_SOURCE_DIR}/statusbar_gettext.cpp 55 | ${PROJECT_SOURCE_DIR}/statusbar_gettiptext.cpp 56 | ${PROJECT_SOURCE_DIR}/tabcontrol_getitemtext.cpp 57 | ${PROJECT_SOURCE_DIR}/thread.cpp 58 | ${PROJECT_SOURCE_DIR}/toolbar_getbuttontext.cpp 59 | ${PROJECT_SOURCE_DIR}/treeitem_getitemtext.cpp 60 | ${PROJECT_SOURCE_DIR}/window.cpp 61 | ${PROJECT_SOURCE_DIR}/windowclass_constructor.cpp 62 | ${PROJECT_SOURCE_DIR}/window_class.cpp 63 | ${PROJECT_SOURCE_DIR}/window_defaultproc.cpp 64 | ${PROJECT_SOURCE_DIR}/window_gettext.cpp 65 | ${PROJECT_SOURCE_DIR}/winmain.cpp 66 | ) 67 | 68 | add_library (${PROJECT_NAME} STATIC ${PROJECT_SRCS}) 69 | 70 | target_include_directories ( ${PROJECT_NAME} 71 | PUBLIC ${PROJECT_INCLUDE_DIR} 72 | PRIVATE ${PROJECT_SOURCE_DIR} 73 | ) 74 | 75 | -------------------------------------------------------------------------------- /examples/dialogs/dialog.rc: -------------------------------------------------------------------------------- 1 | // Microsoft Visual C++ generated resource script. 2 | // 3 | #include "resource.h" 4 | 5 | #define APSTUDIO_READONLY_SYMBOLS 6 | ///////////////////////////////////////////////////////////////////////////// 7 | // 8 | // Generated from the TEXTINCLUDE 2 resource. 9 | // 10 | #include "afxres.h" 11 | 12 | ///////////////////////////////////////////////////////////////////////////// 13 | #undef APSTUDIO_READONLY_SYMBOLS 14 | 15 | ///////////////////////////////////////////////////////////////////////////// 16 | // Polish resources 17 | 18 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_PLK) 19 | #ifdef _WIN32 20 | LANGUAGE LANG_POLISH, SUBLANG_DEFAULT 21 | #pragma code_page(1250) 22 | #endif //_WIN32 23 | 24 | ///////////////////////////////////////////////////////////////////////////// 25 | // 26 | // Dialog 27 | // 28 | 29 | IDD_SAMPLE DIALOGEX 0, 0, 187, 75 30 | STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | 31 | WS_CAPTION | WS_SYSMENU 32 | CAPTION "Sample App" 33 | FONT 8, "MS Shell Dlg", 400, 0, 0x1 34 | BEGIN 35 | DEFPUSHBUTTON "OK",IDOK,68,54,50,14 36 | GROUPBOX "",IDC_STATIC,7,7,173,40 37 | LTEXT "Hello, World!",IDC_STATIC,71,24,43,8 38 | END 39 | 40 | 41 | ///////////////////////////////////////////////////////////////////////////// 42 | // 43 | // DESIGNINFO 44 | // 45 | 46 | #ifdef APSTUDIO_INVOKED 47 | GUIDELINES DESIGNINFO 48 | BEGIN 49 | IDD_SAMPLE, DIALOG 50 | BEGIN 51 | LEFTMARGIN, 7 52 | RIGHTMARGIN, 180 53 | TOPMARGIN, 7 54 | BOTTOMMARGIN, 68 55 | END 56 | END 57 | #endif // APSTUDIO_INVOKED 58 | 59 | #endif // Polish resources 60 | ///////////////////////////////////////////////////////////////////////////// 61 | 62 | 63 | ///////////////////////////////////////////////////////////////////////////// 64 | // English (U.S.) resources 65 | 66 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) 67 | #ifdef _WIN32 68 | LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US 69 | #pragma code_page(1252) 70 | #endif //_WIN32 71 | 72 | #ifdef APSTUDIO_INVOKED 73 | ///////////////////////////////////////////////////////////////////////////// 74 | // 75 | // TEXTINCLUDE 76 | // 77 | 78 | 1 TEXTINCLUDE 79 | BEGIN 80 | "resource.h\0" 81 | END 82 | 83 | 2 TEXTINCLUDE 84 | BEGIN 85 | "#include ""afxres.h""\r\n" 86 | "\0" 87 | END 88 | 89 | 3 TEXTINCLUDE 90 | BEGIN 91 | "\r\n" 92 | "\0" 93 | END 94 | 95 | #endif // APSTUDIO_INVOKED 96 | 97 | #endif // English (U.S.) resources 98 | ///////////////////////////////////////////////////////////////////////////// 99 | 100 | 101 | 102 | #ifndef APSTUDIO_INVOKED 103 | ///////////////////////////////////////////////////////////////////////////// 104 | // 105 | // Generated from the TEXTINCLUDE 3 resource. 106 | // 107 | 108 | 109 | ///////////////////////////////////////////////////////////////////////////// 110 | #endif // not APSTUDIO_INVOKED 111 | 112 | -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #============================================================================= 2 | # winapi examples 3 | # 4 | cmake_minimum_required (VERSION 3.4) 5 | project (examples) 6 | 7 | # build a list of target names so they can be returned to the parent makelist 8 | set (EXAMPLES_TARGET_NAMES_LOCAL ) 9 | 10 | 11 | 12 | #----------------------------------------------------------------------------- 13 | # macro: add_example_executable - avoid writing common commands over and over 14 | 15 | macro (add_example_executable name srcs include_dirs libs) 16 | 17 | set (EXAMPLES_TARGET_NAMES_LOCAL ${EXAMPLES_TARGET_NAMES_LOCAL} ${name} ) 18 | add_executable (${name} WIN32 EXCLUDE_FROM_ALL ${srcs}) 19 | target_include_directories (${name} PRIVATE 20 | ${include_dirs} 21 | $ 22 | ) 23 | target_link_libraries (${name} ${WINAPI_PROJECT_NAME} ${libs}) 24 | 25 | endmacro (add_example_executable) 26 | 27 | 28 | 29 | #----------------------------------------------------------------------------- 30 | # EXAMPLE1 sdi 31 | set (EXAMPLE1_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 32 | set (EXAMPLE1_SRCS ${EXAMPLE1_SOURCE_DIR}/sdi.cpp) 33 | add_example_executable (sdi ${EXAMPLE1_SRCS} ${EXAMPLE1_SOURCE_DIR} "") 34 | 35 | 36 | 37 | #----------------------------------------------------------------------------- 38 | # EXAMPLE2 mdi - multiple document interface 39 | set (EXAMPLE2_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 40 | set (EXAMPLE2_SRCS ${EXAMPLE2_SOURCE_DIR}/mdi.cpp) 41 | add_example_executable (mdi ${EXAMPLE2_SRCS} ${EXAMPLE2_SOURCE_DIR} "") 42 | 43 | 44 | 45 | #----------------------------------------------------------------------------- 46 | # EXAMPLE3 dlg_modal - modal dialog 47 | set (EXAMPLE3_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dialogs) 48 | 49 | set (EXAMPLE3_SRCS 50 | ${EXAMPLE3_SOURCE_DIR}/dlg_modal.cpp 51 | ${EXAMPLE3_SOURCE_DIR}/dialog.rc 52 | ) 53 | 54 | add_example_executable (dlg_modal "${EXAMPLE3_SRCS}" ${EXAMPLE3_SOURCE_DIR} "") 55 | 56 | 57 | 58 | #----------------------------------------------------------------------------- 59 | # EXAMPLE4 dlg_modeless - modeless dialog 60 | set (EXAMPLE4_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dialogs) 61 | 62 | set (EXAMPLE4_SRCS 63 | ${EXAMPLE4_SOURCE_DIR}/dlg_modeless.cpp 64 | ${EXAMPLE4_SOURCE_DIR}/dialog.rc 65 | ) 66 | 67 | add_example_executable (dlg_modeless "${EXAMPLE4_SRCS}" ${EXAMPLE4_SOURCE_DIR} "") 68 | 69 | 70 | 71 | #----------------------------------------------------------------------------- 72 | # EXAMPLE5 multithreading 73 | set (EXAMPLE5_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/multithreading) 74 | 75 | set (EXAMPLE5_SRCS 76 | ${EXAMPLE5_SOURCE_DIR}/multithreading.cpp 77 | ${EXAMPLE5_SOURCE_DIR}/mtdialog.rc 78 | ) 79 | 80 | add_example_executable ( 81 | multithreading "${EXAMPLE5_SRCS}" ${EXAMPLE5_SOURCE_DIR} comctl32 82 | ) 83 | 84 | 85 | 86 | #----------------------------------------------------------------------------- 87 | # Return target names to parent make list 88 | set (EXAMPLES_TARGET_NAMES ${EXAMPLES_TARGET_NAMES_LOCAL} PARENT_SCOPE) 89 | -------------------------------------------------------------------------------- /examples/multithreading/mtdialog.rc: -------------------------------------------------------------------------------- 1 | // Microsoft Visual C++ generated resource script. 2 | // 3 | #include "resource.h" 4 | 5 | #define APSTUDIO_READONLY_SYMBOLS 6 | ///////////////////////////////////////////////////////////////////////////// 7 | // 8 | // Generated from the TEXTINCLUDE 2 resource. 9 | // 10 | #include "afxres.h" 11 | 12 | ///////////////////////////////////////////////////////////////////////////// 13 | #undef APSTUDIO_READONLY_SYMBOLS 14 | 15 | ///////////////////////////////////////////////////////////////////////////// 16 | // Polish resources 17 | 18 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_PLK) 19 | #ifdef _WIN32 20 | LANGUAGE LANG_POLISH, SUBLANG_DEFAULT 21 | #pragma code_page(1250) 22 | #endif //_WIN32 23 | 24 | ///////////////////////////////////////////////////////////////////////////// 25 | // 26 | // Dialog 27 | // 28 | 29 | IDD_SAMPLE DIALOGEX 0, 0, 187, 80 30 | STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | 31 | WS_CAPTION | WS_SYSMENU 32 | CAPTION "Sample App" 33 | FONT 8, "MS Shell Dlg", 400, 0, 0x1 34 | BEGIN 35 | PUSHBUTTON "Cancel",IDCANCEL,68,59,50,14 36 | GROUPBOX "Progress",IDC_STATIC,7,7,173,44 37 | CONTROL "",IDC_PROGRESS,"msctls_progress32",WS_BORDER | 0x1,17, 38 | 23,152,14 39 | END 40 | 41 | 42 | ///////////////////////////////////////////////////////////////////////////// 43 | // 44 | // DESIGNINFO 45 | // 46 | 47 | #ifdef APSTUDIO_INVOKED 48 | GUIDELINES DESIGNINFO 49 | BEGIN 50 | IDD_SAMPLE, DIALOG 51 | BEGIN 52 | LEFTMARGIN, 7 53 | RIGHTMARGIN, 180 54 | TOPMARGIN, 7 55 | BOTTOMMARGIN, 73 56 | END 57 | END 58 | #endif // APSTUDIO_INVOKED 59 | 60 | #endif // Polish resources 61 | ///////////////////////////////////////////////////////////////////////////// 62 | 63 | 64 | ///////////////////////////////////////////////////////////////////////////// 65 | // English (U.S.) resources 66 | 67 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) 68 | #ifdef _WIN32 69 | LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US 70 | #pragma code_page(1252) 71 | #endif //_WIN32 72 | 73 | #ifdef APSTUDIO_INVOKED 74 | ///////////////////////////////////////////////////////////////////////////// 75 | // 76 | // TEXTINCLUDE 77 | // 78 | 79 | 1 TEXTINCLUDE 80 | BEGIN 81 | "resource.h\0" 82 | END 83 | 84 | 2 TEXTINCLUDE 85 | BEGIN 86 | "#include ""afxres.h""\r\n" 87 | "\0" 88 | END 89 | 90 | 3 TEXTINCLUDE 91 | BEGIN 92 | "\r\n" 93 | "\0" 94 | END 95 | 96 | #endif // APSTUDIO_INVOKED 97 | 98 | #endif // English (U.S.) resources 99 | ///////////////////////////////////////////////////////////////////////////// 100 | 101 | 102 | 103 | #ifndef APSTUDIO_INVOKED 104 | ///////////////////////////////////////////////////////////////////////////// 105 | // 106 | // Generated from the TEXTINCLUDE 3 resource. 107 | // 108 | 109 | 110 | ///////////////////////////////////////////////////////////////////////////// 111 | #endif // not APSTUDIO_INVOKED 112 | 113 | -------------------------------------------------------------------------------- /src/dialog.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | BOOL CALLBACK WinAPIDefaultDlgProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ) 8 | { 9 | Dialog* pWindow = windows_cast( GetWindowLong( hwndDlg, GWL_USERDATA ) ); 10 | if ( pWindow == NULL ) { 11 | if ( uMsg == WM_INITDIALOG ) { 12 | PASSLPARAM* pass = reinterpret_cast( lParam ); 13 | if ( pass == 0 ) 14 | return FALSE; 15 | pWindow = static_cast( pass->pWindow ); 16 | if ( pWindow == 0 ) 17 | return FALSE; 18 | *pass->phwnd = hwndDlg; 19 | lParam = reinterpret_cast( pass->lpParam ); 20 | SetWindowLong( hwndDlg, GWL_USERDATA, windows_cast( pWindow ) ); 21 | } else 22 | return FALSE; 23 | } 24 | switch ( uMsg ) { 25 | case WM_NCDESTROY: 26 | pWindow->HandleMessage( uMsg, static_cast( wParam ), static_cast( lParam ) ); 27 | if ( pWindow->IsCreated() ) 28 | Window::Delete( pWindow ); 29 | return FALSE; 30 | #ifndef PURE_WRAPPER 31 | case WM_INITDIALOG: 32 | return pWindow->OnInitDialog( reinterpret_cast( wParam ), lParam ); 33 | case WM_DESTROY: 34 | pWindow->OnDestroy(); 35 | return 0; 36 | case WM_COMMAND: 37 | pWindow->OnCommand( LOWORD( wParam ), HIWORD( wParam ), reinterpret_cast( lParam ) ); 38 | return true; 39 | case WM_NOTIFY: 40 | return pWindow->OnNotify( static_cast( wParam ), reinterpret_cast( lParam ) ); 41 | case WM_PAINT: 42 | pWindow->OnPaint(); 43 | return 0; 44 | case WM_SIZE: 45 | pWindow->OnSize( static_cast( wParam ), WSize( LOWORD( lParam ), HIWORD( lParam ) ) ); 46 | return 0; 47 | case WM_MOUSEMOVE: 48 | pWindow->OnMouseMove( LParam2Point( lParam ), static_cast( wParam ) ); 49 | return FALSE; 50 | case WM_LBUTTONDOWN: 51 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 52 | return FALSE; 53 | case WM_LBUTTONUP: 54 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 55 | return FALSE; 56 | case WM_RBUTTONDOWN: 57 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 58 | return FALSE; 59 | case WM_RBUTTONUP: 60 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 61 | return FALSE; 62 | case WM_MBUTTONDOWN: 63 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 64 | return FALSE; 65 | case WM_MBUTTONUP: 66 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 67 | return FALSE; 68 | #endif 69 | default: 70 | return pWindow->HandleMessage( uMsg, static_cast( wParam ), static_cast( lParam ) ); 71 | } 72 | } 73 | 74 | Dialog::~Dialog() 75 | { 76 | } 77 | 78 | int Dialog::HandleMessage( int uMsg, int wParam, int lParam ) 79 | { 80 | return 0; 81 | } 82 | 83 | #ifndef PURE_WRAPPER 84 | 85 | void Dialog::OnClose() 86 | { 87 | } 88 | 89 | void Dialog::OnDestroy() 90 | { 91 | } 92 | 93 | bool Dialog::OnInitDialog( HWND hwndFocus, LPARAM lParam ) 94 | { 95 | return true; 96 | } 97 | 98 | void Dialog::OnCommand( int nIdentifier, int nNotifyCode, HWND hwndControl ) 99 | { 100 | } 101 | 102 | int Dialog::OnNotify( int nIdentifier, LPNMHDR pnmh ) 103 | { 104 | return 0; 105 | } 106 | 107 | void Dialog::OnMouseMove( WPoint point, int keys ) 108 | { 109 | } 110 | 111 | void Dialog::OnMouseDown( WPoint point, int keys, int button ) 112 | { 113 | } 114 | 115 | void Dialog::OnMouseUp( WPoint point, int keys, int button ) 116 | { 117 | } 118 | 119 | void Dialog::OnMouseDblClk( WPoint point, int keys, int button ) 120 | { 121 | } 122 | 123 | bool Dialog::OnCreate( LPCREATESTRUCT lpCreateStruct ) 124 | { 125 | return true; 126 | } 127 | 128 | void Dialog::OnPaint() 129 | { 130 | } 131 | 132 | void Dialog::OnSize( int sizing, WSize new_size ) 133 | { 134 | } 135 | 136 | void Dialog::OnKeyDown( int key, int keyData ) 137 | { 138 | } 139 | 140 | void Dialog::OnKeyUp( int key, int keyData ) 141 | { 142 | } 143 | 144 | #endif 145 | -------------------------------------------------------------------------------- /src/mdichild_defaultproc.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | namespace { 8 | 9 | LRESULT CALLBACK MDIChildWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) 10 | { 11 | Window* pWindow = windows_cast( GetWindowLong( hWnd, GWL_USERDATA ) ); 12 | bool b_def = true; 13 | do { 14 | if ( pWindow == 0 || *pWindow != hWnd ) { 15 | if ( uMsg == WM_NCCREATE ) { 16 | LPCREATESTRUCT lpCreate = reinterpret_cast( lParam ); 17 | LPMDICREATESTRUCT lpMDICreate = reinterpret_cast( lpCreate->lpCreateParams ); 18 | PASSLPARAM* pass = reinterpret_cast( lpMDICreate->lParam ); 19 | if ( pass == 0 ) 20 | break; 21 | pWindow = static_cast( pass->pWindow ); 22 | if ( pWindow == 0 ) 23 | break; 24 | *pass->phwnd = hWnd; 25 | lpCreate->lpCreateParams = pass->lpParam; 26 | SetWindowLong( hWnd, GWL_USERDATA, windows_cast( pWindow ) ); 27 | } else 28 | break; 29 | } 30 | b_def = false; 31 | } while ( false ); 32 | if ( b_def ) return DefMDIChildProc( hWnd, uMsg, wParam, lParam ); 33 | switch ( uMsg ) { 34 | case WM_NCDESTROY: 35 | pWindow->HandleMessage( uMsg, static_cast( wParam ), static_cast( lParam ) ); 36 | if ( pWindow->IsCreated() ) 37 | Window::Delete( pWindow ); 38 | return 0; 39 | #ifndef PURE_WRAPPER 40 | case WM_CREATE: 41 | return pWindow->OnCreate( reinterpret_cast( lParam ) ) ? 0 : -1; 42 | case WM_DESTROY: 43 | pWindow->OnDestroy(); 44 | return 0; 45 | case WM_CLOSE: 46 | pWindow->OnClose(); 47 | return 0; 48 | case WM_COMMAND: 49 | pWindow->OnCommand( LOWORD( wParam ), HIWORD( wParam ), reinterpret_cast( lParam ) ); 50 | return 0; 51 | case WM_NOTIFY: 52 | return pWindow->OnNotify( static_cast( wParam ), reinterpret_cast( lParam ) ); 53 | case WM_PAINT: 54 | pWindow->OnPaint(); 55 | return 0; 56 | case WM_SIZE: 57 | pWindow->OnSize( static_cast( wParam ), WSize( LOWORD( lParam ), HIWORD( lParam ) ) ); 58 | return 0; 59 | case WM_MOUSEMOVE: 60 | pWindow->OnMouseMove( LParam2Point( lParam ), static_cast( wParam ) ); 61 | return FALSE; 62 | case WM_LBUTTONDOWN: 63 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 64 | return FALSE; 65 | case WM_LBUTTONUP: 66 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 67 | return FALSE; 68 | case WM_RBUTTONDOWN: 69 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 70 | return FALSE; 71 | case WM_RBUTTONUP: 72 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 73 | return FALSE; 74 | case WM_MBUTTONDOWN: 75 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 76 | return FALSE; 77 | case WM_MBUTTONUP: 78 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 79 | return FALSE; 80 | case WM_LBUTTONDBLCLK: 81 | pWindow->OnMouseDblClk( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 82 | return FALSE; 83 | case WM_RBUTTONDBLCLK: 84 | pWindow->OnMouseDblClk( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 85 | return FALSE; 86 | case WM_MBUTTONDBLCLK: 87 | pWindow->OnMouseDblClk( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 88 | return FALSE; 89 | case WM_KEYDOWN: 90 | pWindow->OnKeyDown( static_cast( wParam ), static_cast( lParam ) ); 91 | return FALSE; 92 | case WM_KEYUP: 93 | pWindow->OnKeyUp( static_cast( wParam ), static_cast( lParam ) ); 94 | return FALSE; 95 | #endif 96 | default: 97 | return pWindow->HandleMessage( uMsg, static_cast( wParam ), static_cast( lParam ) ); 98 | } 99 | } 100 | 101 | } // namespace 102 | 103 | MDIChildWindowClass::MDIChildWindowClass( UINT style, int cbClsExtra, int cbWndExtra, HICON hIcon, HCURSOR hCursor, HBRUSH hbrBackground, LPCTSTR lpszMenuName, HINSTANCE hInstance ) 104 | : WindowClass( MDIChildWndProc, style, cbClsExtra, cbWndExtra, hIcon, hCursor, hbrBackground, lpszMenuName, hInstance ) 105 | { 106 | } 107 | -------------------------------------------------------------------------------- /src/window_defaultproc.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "winapi.h" 3 | #include "passlparam.h" 4 | 5 | using namespace WinAPI; 6 | 7 | namespace { 8 | 9 | LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) 10 | { 11 | Window* pWindow = windows_cast( GetWindowLong( hWnd, GWL_USERDATA ) ); 12 | bool b_def = true; 13 | do { 14 | if ( pWindow == 0 || *pWindow != hWnd ) { 15 | if ( uMsg == WM_NCCREATE ) { 16 | LPCREATESTRUCT lpCreate = reinterpret_cast( lParam ); 17 | PASSLPARAM* pass = reinterpret_cast( lpCreate->lpCreateParams ); 18 | if ( pass == 0 ) 19 | break; 20 | pWindow = static_cast( pass->pWindow ); 21 | if ( pWindow == 0 ) 22 | break; 23 | *pass->phwnd = hWnd; 24 | lpCreate->lpCreateParams = pass->lpParam; 25 | SetWindowLong( hWnd, GWL_USERDATA, windows_cast( pWindow ) ); 26 | } else 27 | break; 28 | } 29 | b_def = false; 30 | } while ( false ); 31 | if ( b_def ) return DefWindowProc( hWnd, uMsg, wParam, lParam ); 32 | switch ( uMsg ) { 33 | case WM_NCDESTROY: 34 | pWindow->HandleMessage( uMsg, static_cast( wParam ), static_cast( lParam ) ); 35 | if ( pWindow->IsCreated() ) 36 | Window::Delete( pWindow ); 37 | return 0; 38 | #ifndef PURE_WRAPPER 39 | case WM_CREATE: 40 | return pWindow->OnCreate( reinterpret_cast( lParam ) ) ? 0 : -1; 41 | case WM_DESTROY: 42 | pWindow->OnDestroy(); 43 | return 0; 44 | case WM_CLOSE: 45 | pWindow->OnClose(); 46 | return 0; 47 | case WM_COMMAND: 48 | pWindow->OnCommand( LOWORD( wParam ), HIWORD( wParam ), reinterpret_cast( lParam ) ); 49 | return 0; 50 | case WM_NOTIFY: 51 | return pWindow->OnNotify( static_cast( wParam ), reinterpret_cast( lParam ) ); 52 | case WM_PAINT: 53 | pWindow->OnPaint(); 54 | return 0; 55 | case WM_SIZE: 56 | pWindow->OnSize( static_cast( wParam ), WSize( LOWORD( lParam ), HIWORD( lParam ) ) ); 57 | return 0; 58 | case WM_MOUSEMOVE: 59 | pWindow->OnMouseMove( LParam2Point( lParam ), static_cast( wParam ) ); 60 | return FALSE; 61 | case WM_LBUTTONDOWN: 62 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 63 | return FALSE; 64 | case WM_LBUTTONUP: 65 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 66 | return FALSE; 67 | case WM_RBUTTONDOWN: 68 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 69 | return FALSE; 70 | case WM_RBUTTONUP: 71 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 72 | return FALSE; 73 | case WM_MBUTTONDOWN: 74 | pWindow->OnMouseDown( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 75 | return FALSE; 76 | case WM_MBUTTONUP: 77 | pWindow->OnMouseUp( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 78 | return FALSE; 79 | case WM_LBUTTONDBLCLK: 80 | pWindow->OnMouseDblClk( LParam2Point( lParam ), static_cast( wParam ), Window::left_button ); 81 | return FALSE; 82 | case WM_RBUTTONDBLCLK: 83 | pWindow->OnMouseDblClk( LParam2Point( lParam ), static_cast( wParam ), Window::right_button ); 84 | return FALSE; 85 | case WM_MBUTTONDBLCLK: 86 | pWindow->OnMouseDblClk( LParam2Point( lParam ), static_cast( wParam ), Window::middle_button ); 87 | return FALSE; 88 | case WM_KEYDOWN: 89 | pWindow->OnKeyDown( static_cast( wParam ), static_cast( lParam ) ); 90 | return FALSE; 91 | case WM_KEYUP: 92 | pWindow->OnKeyUp( static_cast( wParam ), static_cast( lParam ) ); 93 | return FALSE; 94 | #endif 95 | default: 96 | return pWindow->HandleMessage( uMsg, static_cast( wParam ), static_cast( lParam ) ); 97 | } 98 | } 99 | 100 | } // namespace 101 | 102 | WindowClass::WindowClass( UINT style, int cbClsExtra, int cbWndExtra, HICON hIcon, HCURSOR hCursor, HBRUSH hbrBackground, LPCTSTR lpszMenuName, HINSTANCE hInstance ) 103 | { 104 | Register( WndProc, style, cbClsExtra, cbWndExtra, hIcon, hCursor, hbrBackground, lpszMenuName, hInstance ); 105 | } 106 | 107 | WindowClass::WindowClass( HICON hIcon, HBRUSH hbrBackground, LPCTSTR lpszMenuName, UINT style, HCURSOR hCursor, HINSTANCE hInstance ) 108 | { 109 | Register( WndProc, style, 0, 0, hIcon, hCursor, hbrBackground, lpszMenuName, hInstance ); 110 | } 111 | --------------------------------------------------------------------------------