260 likes | 445 Views
Windows Goodies. CIS 577 Bruce R. Maxim UM-Dearborn. LaMothe Examples. Timers. Low accuracy timers WM_TIMER messages Medium accuracy timers Milliseconds of elapsed CPU time High accuracy timers Uses programmer written callback functions. Creating Timers. // global timer defines
E N D
Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn
Timers • Low accuracy timers • WM_TIMER messages • Medium accuracy timers • Milliseconds of elapsed CPU time • High accuracy timers • Uses programmer written callback functions
Creating Timers // global timer defines #define TIMER_ID1_SEC 1 // id of 1 sec timer #define TIMER_ID3_SEC 2 // id of 3 sec timer // inside callback function switch statement static int counter1=0, // counters counter2=0; case WM_CREATE: { // do initialization stuff here // create a 1 second timer SetTimer(hwnd, TIMER_ID1_SEC, 1000,NULL); // now the 3 second timer SetTimer(hwnd, TIMER_ID3_SEC, 3000,NULL); // return success return(0); } break;
WM_TIMER Messages case WM_TIMER: { switch(wparam) //test what timer fired { case TIMER_ID1_SEC: { // print out a message hdc = GetDC(hwnd); // get the dc SetTextColor(hdc,RGB(0,255,0)); SetBkColor(hdc,RGB(0,0,0)); SetBkMode(hdc,OPAQUE); sprintf(buffer, "The 1 second timer has fired %d times",++counter1); TextOut(hdc,0,0,buffer,strlen(buffer)); ReleaseDC(hwnd,hdc); } break;
WM_TIMER Messages case WM_TIMER: { switch(wparam) //test what timer fired { case TIMER_ID1_SEC: { // print out a message hdc = GetDC(hwnd); // get the dc SetTextColor(hdc,RGB(0,255,0)); SetBkColor(hdc,RGB(0,0,0)); SetBkMode(hdc,OPAQUE); sprintf(buffer, "The 1 second timer has fired %d times",++counter1); TextOut(hdc,0,0,buffer,strlen(buffer)); ReleaseDC(hwnd,hdc); } break;
WM_TIMER Messages case TIMER_ID3_SEC: { // print out a message hdc = GetDC(hwnd); // get the dc SetTextColor(hdc,RGB(0,255,0)); SetBkColor(hdc,RGB(0,0,0)); SetBkMode(hdc,OPAQUE); sprintf(buffer, "The 3 second timer has fired %d times",++counter2); TextOut(hdc,0,0,buffer,strlen(buffer)); ReleaseDC(hwnd,hdc); } break; // .. test for other id's default:break; } // end switch
Tick Counters // get time referrence DWORD start_time = GetTickCount(); // lock time to 30 fps (approx. 33 milliseconds) while((GetTickCount() - start_time) < 33); • Remember TickCount cannot be set to 0
Resources • What can you add to your application under Windows? • Strings • Menus • Dialog boxes • Bitmaps • Icons • Sounds
Resources • Resources are stored in files with the extension .RC • Resources are compiled into resource libraries with the extension .RES
Creating Menus • Create windows resource script for menu definition file (.RC) • Create header file (.h) that contains menu constant definitions • Add resource script (.RC) to project • Include header file (.h) in C++ code • Write code to load menu into application • Write code to process WM_COMMAND messages • Compile everything to an .EXE file
Demo3_4.RC #include "DEMO3_4RES.H" // the icon and cursor resource ICON_T3DX ICON t3dx.ico CURSOR_CROSSHAIR CURSOR crosshair.cur // the sound resources SOUND_ID_ENERGIZE WAVE energize.wav SOUND_ID_BEAM WAVE beam.wav SOUND_ID_TELEPORT WAVE teleport.wav SOUND_ID_WARP WAVE warp.wav // the menu resource SoundMenu MENU DISCARDABLE { POPUP "&File" { MENUITEM "E&xit", MENU_FILE_ID_EXIT } // end popup
Demo3_4.RC POPUP "&PlaySound" { MENUITEM "Energize!", MENU_PLAY_ID_ENERGIZE MENUITEM "Beam Me Up", MENU_PLAY_ID_BEAM MENUITEM "Engage Teleporter", MENU_PLAY_ID_TELEPORT MENUITEM "Quantum Warp Teleport", MENU_PLAY_ID_WARP } // end popup POPUP "Help" { MENUITEM "About", MENU_HELP_ABOUT } // end popup } // end top level menu
Demo3.4res.h // defines for sounds resources #define SOUND_ID_ENERGIZE 1 #define SOUND_ID_BEAM 2 #define SOUND_ID_TELEPORT 3 #define SOUND_ID_WARP 4 // defines for icon and cursor #define ICON_T3DX 100 #define CURSOR_CROSSHAIR 200
Demo3.4res.h // defines for the top level menu FILE #define MENU_FILE_ID_EXIT 1000 // defines for play sound top level menu #define MENU_PLAY_ID_ENERGIZE 2000 #define MENU_PLAY_ID_BEAM 2001 #define MENU_PLAY_ID_TELEPORT 2002 #define MENU_PLAY_ID_WARP 2003 // defines for the top level menu HELP #define MENU_HELP_ABOUT 3000
Attaching the Menu // Inside WinMain // save main window handle main_window_handle = hwnd; // load the menu resource HMENU hmenuhandle = LoadMenu(hinstance, “SoundMenu"); // attach the menu to the window SetMenu(hwnd, hmenuhandle);
Attaching the Menu Directly // first fill in the window class stucture winclass.hInstance = hinstance; winclass.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(ICON_T3DX)); winclass.hCursor = LoadCursor(hinstance, MAKEINTRESOURCE(CURSOR_CROSSHAIR)); winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winclass.lpszMenuName = "SoundMenu"; winclass.lpszClassName = WINDOW_CLASS_NAME; winclass.hIconSm = LoadIcon(hinstance, MAKEINTRESOURCE(ICON_T3DX)); // save hinstance in global hinstance_app = hinstance; // register the window class if (!RegisterClassEx(&winclass)) return(0);
WM_COMMAND case WM_COMMAND: { switch(LOWORD(wparam)) { // handle the FILE menu case MENU_FILE_ID_EXIT: { PostQuitMessage(0); // terminate window } break; // handle the HELP menu case MENU_HELP_ABOUT: { // pop up a message box MessageBox(hwnd, "Menu Sound Demo", "About Sound Menu", MB_OK | MB_ICONEXCLAMATION); } break;
WM_COMMAND // handle each of sounds case MENU_PLAY_ID_ENERGIZE: { // play first sound PlaySound(MAKEINTRESOURCE(SOUND_ID_ENERGIZE), hinstance_app, SND_RESOURCE | SND_ASYNC); } break; case MENU_PLAY_ID_WARP: { // play the sound PlaySound(MAKEINTRESOURCE(SOUND_ID_WARP), hinstance_app, SND_RESOURCE | SND_ASYNC); } break; default: break; } // end switch wparam } break; // end WM_COMMAND
Controls • There are lots of “button” styles you can use in a user interface • Push button • Radio button • State box • Remember that buttons are windows too
// create the buttons #define BUTTON_BASE_ID 100 #define NUM_BUTTONS 8 char *button_names[NUM_BUTTONS]= { "PUSHBUTTON", "RADIOBUTTON", "CHECKBOX", "3STATE", "AUTO3STATE", "AUTOCHECKBOX", "AUTORADIOBUTTON", "OWNERDRAW"}; long button_types[NUM_BUTTONS] = { BS_PUSHBUTTON, BS_RADIOBUTTON, BS_CHECKBOX, BS_3STATE, BS_AUTO3STATE, BS_AUTOCHECKBOX, BS_AUTORADIOBUTTON, BS_OWNERDRAW }; Creating Buttons
Creating Buttons for (int button = 0; button < NUM_BUTTONS; button++) { CreateWindowEx(NULL, // extended style "button", // class button_names[button], // title WS_CHILD | WS_VISIBLE | button_types[button], 10,10+button*36, // initial x,y strlen(button_names[button])*16,24, // width, height main_window_handle, // handle to parent (HMENU)(BUTTON_BASE_ID + button), // handle to menu hinstance, // instance of this application NULL); // extra creation parms } // end for button
WM_COMMAND case WM_COMMAND: // all buttons come thru here { hdc = GetDC(hwnd); // get the dc // print out the wparam an lparam SetBkMode(hdc,OPAQUE); SetTextColor(hdc,RGB(0,255,0)); SetBkColor(hdc,RGB(128,128,128)); sprintf(buffer,"LOWORD(wparam)= %d,HIWORD(wparam)= %d", LOWORD(wparam), HIWORD(wparam)); TextOut(hdc,220,100,buffer,strlen(buffer)); sprintf(buffer,"LOWORD(lparam)= 0X%X,HIWORD(lparam)= 0X%X", LOWORD(lparam), HIWORD(lparam)); TextOut(hdc,220,140,buffer,strlen(buffer)); ReleaseDC(hwnd, hdc); // release the dc } break;
Processing WM_COMMAND • LOWORD(wparam) = child window ID • HIWORD(wparam) = notification code BN_CLCKED = 0 BN_PAINT = 1 BN_HIGHLIGHT = 2 BN_UNHILIGHT = 3 BN_DISABLE = 4 BN_DOUBLECLICKED = 5 • lparam = child window handle
Checking Specific Button case WM_COMMAND: { if (LOWORD(wparam) == 100) { // process button with ID 100 } return(0); } break;
Sending Messages to Controls • To simulate pushing a button SendMessage(hwndbutton, BM_CLICK, 0, 0); • To simulate checking a box SendMessage(hwndbutton,BM_SETCHECK, BST_CHECKED,0); • To retrieve current state and mark it checked if (SendMessage(hwndbutton,BM_GETCHECK,0,0) == BST_CHECKED; { // action }