#include #include // yleisten kontrollien otsikkotiedosto HINSTANCE hInstance; LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; // ikkunaproseduurin esittely int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static char szAppName[ ] = "EditList" ; // 'ohjelman nimi' HWND hwnd ; // kahvamuuttuja MSG msg ; // sanomastrukstuuri WNDCLASSEX wndclass ; // ikkunan luokkastruktuuri wndclass.cbSize = sizeof (wndclass) ; // struktuurin koko wndclass.style = CS_HREDRAW | CS_VREDRAW ; // ikkunan tyyli wndclass.lpfnWndProc = WndProc ; // ikkunaproseduurin nimi wndclass.cbClsExtra = 0 ; // ikkunan luokan sisältä ohjelman... wndclass.cbWndExtra = 0 ; // ...omaan käyttöön varattua tilaa wndclass.hInstance = hInstance ; // lomakkeen kahva wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; // ikoni wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; // kursori wndclass.hbrBackground = (HBRUSH) COLOR_WINDOW ; // taustaväri wndclass.lpszMenuName = NULL ; // valikon kahva wndclass.lpszClassName = szAppName ; // ikkunan luokan nimi wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ; // pieni ikoni RegisterClassEx (&wndclass) ; // ikkunan luokan rekisteröinti hwnd = CreateWindow (szAppName, // ikkunan luokan nimi "Rivien lisäys", // ikkunan otsikko WS_OVERLAPPEDWINDOW, // ikkunan tyyli CW_USEDEFAULT, // x-positio aluksi CW_USEDEFAULT, // y-positio aluksi 500, // leveys aluksi 300, // korkeus aluksi NULL, // emoikkunan kahva NULL, // ikkunan valikon kahva hInstance, // ohjelman ilmentymän kahva NULL) ; // luontiparametrit ShowWindow (hwnd, iCmdShow) ; // ikkunan piirto UpdateWindow (hwnd) ; // ikkunan päivitys InitCommonControls ( ) ; // yleisten kontrollien rekisteröinti while (GetMessage (&msg, NULL, 0, 0)) // sanomasilmukan alku { TranslateMessage (&msg) ; // viestien käsittely ja... DispatchMessage (&msg) ; // ...ikkunaproseduurin kutsu } return msg.wParam ; // käyttöjärjestelmälle palautettava arvo } LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { static HWND hwndStatus, hwndList1, hwndEdit, hwndGroupbox, hwndButton1, hwndButton2, hwndButton3 ; static int lokerot[2] ; // tilarivin lokerot static char buffer[100] ; static int index, rivit = 8 ; switch (iMsg) // sanomien käsittelyrakenne { case WM_CREATE : // initialisaatiosanoman käsittely { HFONT hFont = GetStockObject (DEFAULT_GUI_FONT) ; // kysytään oletusfontin kahva hwndGroupbox = CreateWindow ("button", NULL, WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 0, 0, 0, 0, hwnd, (HMENU) 1, hInstance, NULL) ; hwndEdit = CreateWindowEx (WS_EX_CLIENTEDGE, "edit", "Aloita...", WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 0, 0, 0, 0, hwnd, (HMENU) 2, hInstance, NULL) ; SendMessage ( hwndEdit, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0) ) ; hwndButton1 = CreateWindow ("button", "Lisää listaan", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0, hwnd, (HMENU) 3, hInstance, NULL) ; SendMessage ( hwndButton1, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0) ) ; hwndList1 = CreateWindowEx (WS_EX_CLIENTEDGE, "listbox", NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD, 0, 0, 0, 0, hwnd, (HMENU) 4, hInstance, NULL) ; SendMessage ( hwndList1, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0) ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Antti" ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Kaija" ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Mikko" ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Pekka" ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Liisa" ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Jussi" ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Tiina" ) ; SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) "Susanna" ) ; hwndButton2 = CreateWindow ("button", "Poista rivi", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0, hwnd, (HMENU) 5, hInstance, NULL) ; SendMessage ( hwndButton2, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0) ) ; hwndButton3 = CreateWindow ("button", "Tyhjennä lista", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0, hwnd, (HMENU) 6, hInstance, NULL) ; SendMessage ( hwndButton3, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0) ) ; hwndStatus = CreateWindowEx ( NULL, STATUSCLASSNAME, "Aloita...", WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, 0, 0, 0, 0, hwnd, (HMENU) 7, hInstance, NULL ) ; SendMessage ( hwndStatus, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0) ) ; SendMessage ( hwndStatus, SB_SETPARTS, 2, (LPARAM) lokerot ) ; itoa(rivit, buffer, 10); SendMessage ( hwndStatus, SB_SETTEXT, 1, (LPARAM) buffer ) ; return 0 ; } case WM_SIZE : // ikkunan kokoa muutetaan lokerot[0] = LOWORD(lParam) - 100 ; lokerot[1] = LOWORD(lParam) ; MoveWindow ( hwndGroupbox, 10, 10, LOWORD(lParam)-20, HIWORD(lParam)-35, TRUE ) ; MoveWindow ( hwndEdit, 50, 50, LOWORD(lParam)-250, 19, TRUE ) ; MoveWindow ( hwndList1, 50, 100, LOWORD(lParam)-250, HIWORD(lParam)-150, TRUE ) ; MoveWindow ( hwndButton1, LOWORD(lParam)-180, 50, 130, 30, TRUE ) ; MoveWindow ( hwndButton2, LOWORD(lParam)-180, 100, 130, 30, TRUE ) ; MoveWindow ( hwndButton3, LOWORD(lParam)-180, 140, 130, 30, TRUE ) ; MoveWindow ( hwndStatus, NULL, NULL, NULL, NULL, NULL ) ; SendMessage ( hwndStatus, SB_SETPARTS, 2, (LPARAM) lokerot ) ; return 0; case WM_COMMAND : // lapsi-ikkunoiden sanomien käsittely switch (HIWORD(wParam)) { case LBN_DBLCLK: // listboxia tuplanaputettiin SendMessage ( hwnd, WM_COMMAND, MAKEWPARAM(5, BN_CLICKED), NULL ) ; SendMessage ( hwndStatus, SB_SETTEXT, 0, (LPARAM) "Poistit rivin tuplanapautuksella..." ) ; return 0; case BN_CLICKED: // nappia on painettu if ( LOWORD (wParam) == 3 ) { GetWindowText ( hwndEdit, buffer, 99 ) ; if ( strcmp( buffer , "" ) == TRUE ) { SendMessage ( hwndList1, LB_ADDSTRING, 0, (LPARAM) buffer ) ; SendMessage ( hwndStatus, SB_SETTEXT, 0, (LPARAM) "Lisäsit juuri rivin..." ) ; rivit += 1 ; itoa(rivit, buffer, 10); SendMessage ( hwndStatus, SB_SETTEXT, 1, (LPARAM) buffer ) ; } else SendMessage ( hwndStatus, SB_SETTEXT, 0, (LPARAM) "Rivin lisäys ei onnistunut..." ) ; } if ( LOWORD (wParam) == 5 ) { int index = SendMessage ( hwndList1, LB_GETCURSEL, 0, 0 ) ; // kysytään valittu rivi listboxista if ( index != LB_ERR ) { SendMessage ( hwndList1, LB_DELETESTRING, index, 0) ; SendMessage ( hwndStatus, SB_SETTEXT, 0, (LPARAM) "Poistit rivin painamalla nappia..." ) ; rivit -= 1 ; itoa(rivit, buffer, 10) ; // tietotyypin muunnos SendMessage ( hwndStatus, SB_SETTEXT, 1, (LPARAM) buffer ) ; } else SendMessage ( hwndStatus, SB_SETTEXT, 0, (LPARAM) "Rivin poisto ei onnistunut..." ) ; } if ( LOWORD (wParam) == 6 ) { SendMessage ( hwndList1, LB_RESETCONTENT, 0, 0 ) ; // tyhjennetään listbox SendMessage ( hwndStatus, SB_SETTEXT, 0, (LPARAM) "Tyhjensit juuri listan..." ) ; rivit = 0 ; itoa(rivit, buffer, 10) ; // tietotyypin muunnos SendMessage ( hwndStatus, SB_SETTEXT, 1, (LPARAM) buffer ) ; } } return 0 ; // pois ikkunaproseduurista case WM_DESTROY : // lopetussanoman käsittely PostQuitMessage (0) ; // lopetusviesti Windowsille return 0 ; // pois ikkunaproseduurista } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; // sanomien oletuskäsittely }