1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

26
1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn

Transcript of 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

Page 1: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

1

Windows Goodies

CIS 577

Bruce R. Maxim

UM-Dearborn

Page 2: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

2

LaMothe Examples

Page 3: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

3

Timers

• Low accuracy timers– WM_TIMER messages

• Medium accuracy timers– Milliseconds of elapsed CPU time

• High accuracy timers– Uses programmer written callback

functions

Page 4: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

4

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 statementstatic 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;

Page 5: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

5

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;

Page 6: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

6

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;

Page 7: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

7

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

Page 8: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

8

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

Page 9: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

9

Resources

• What can you add to your application under Windows?– Strings– Menus– Dialog boxes– Bitmaps– Icons– Sounds

Page 10: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

10

Resources

• Resources are stored in files with the extension .RC

• Resources are compiled into resource libraries with the extension .RES

Page 11: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

11

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

Page 12: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

12

Demo3_4.RC#include "DEMO3_4RES.H"// the icon and cursor resourceICON_T3DX ICON t3dx.icoCURSOR_CROSSHAIR CURSOR crosshair.cur// the sound resourcesSOUND_ID_ENERGIZE WAVE energize.wavSOUND_ID_BEAM WAVE beam.wavSOUND_ID_TELEPORT WAVE teleport.wavSOUND_ID_WARP WAVE warp.wav// the menu resourceSoundMenu MENU DISCARDABLE{POPUP "&File"

{MENUITEM "E&xit", MENU_FILE_ID_EXIT} // end popup

Page 13: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

13

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

Page 14: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

14

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

Page 15: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

15

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

Page 16: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

16

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);

Page 17: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

17

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);

Page 18: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

18

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;

Page 19: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

19

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

Page 20: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

20

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

Page 21: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

21

Creating Buttons

// 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

};

Page 22: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

22

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

Page 23: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

23

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;

Page 24: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

24

Processing WM_COMMAND

• LOWORD(wparam) = child window ID• HIWORD(wparam) = notification code

BN_CLCKED = 0BN_PAINT = 1BN_HIGHLIGHT = 2BN_UNHILIGHT = 3BN_DISABLE = 4BN_DOUBLECLICKED = 5

• lparam = child window handle

Page 25: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

25

Checking Specific Buttoncase WM_COMMAND:

{

if (LOWORD(wparam) == 100)

{

// process button with ID 100

}

return(0);

} break;

Page 26: 1 Windows Goodies CIS 577 Bruce R. Maxim UM-Dearborn.

26

Sending Messages to Controls

• To simulate pushing a buttonSendMessage(hwndbutton, BM_CLICK, 0, 0);

• To simulate checking a boxSendMessage(hwndbutton,BM_SETCHECK, BST_CHECKED,0);

• To retrieve current state and mark it checkedif (SendMessage(hwndbutton,BM_GETCHECK,0,0) == BST_CHECKED;{ // action}