DOC PREVIEW
U-M CIS 487 - Windows GDI Programming

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Windows GDI ProgrammingValidatingProcessing WM_PAINTSlide 4Invalidating Entire WindowValidating Entire WindowAvoid RepaintDisplay Surface BasicsDisplay ModesColor ObjectsDrawing TextLaMothe ExamplesTextOut ExampleWhat about WM_PAINT?Additional IssuesTextOut Example 2Text MetricsDrawing ShapesPensUsing a PenBrushesSelecting and Destroying BrushesPlotting PointsDrawing LinesDrawing RectanglesDrawing Circles and EllipsesBouncing BallSlide 28Slide 29Slide 30Slide 31Slide 321Windows GDI ProgrammingCIS 487/587Bruce R. MaximUM-Dearborn2Validating•Whenever either you or Windows disturbs a window a WM_PAINT message is sent•The area to be repainted is the invalid rectangle•The coordinates of the invalid rectangle are shipped with the WM_PAINT message as part of the validating process3Processing WM_PAINT•BeginPaint( ) … EndPaint( )–Advantage: all needed validating done automatically–Disadvantage: invalid rectangle is same as clipping rectangle (can’t draw outside invalid rectangle)–This approach fools Windows into believing you fixed the invalid rectangle whether you have drawn anything or not4Processing WM_PAINT•GetDC( ) … ReleaseDC( )–Advantage:you can draw outside the invalid rectangle –Disadvantage: you need to validate every rectangle manually using ValidateRect( )•Note: With either method you need to be sure that windows does not fill in your Background to avoid losing your drawing5Invalidating Entire Windowcase WM_PAINT: { // NULL is pointer to invalid rectangle // meaning entire window ValidateRect(hwnd,NULL);// simply validate the window hdc = BeginPaint(hwnd,&ps); // fixes go here EndPaint(hwnd,&ps); // end painting return(0); // return success} break;6Validating Entire Windowcase WM_PAINT: { // NULL is pointer to invalid rectangle // meaning entire window ValidateRect(hwnd,NULL);// simply validate the window without // doing any drawing return(0); // return success} break;7Avoid Repaintcase WM_PAINT:{ hdc = GetDC(hwnd); // do graphics here ReleaseDC(hwnd,hdc); // validate the window to clear message ValidateRect(hwnd,hdc); return(0); // return success} break;8Display Surface Basics•Resolutions in pixels640x480, 800x600, 1024x768, …•Color depth (# color bits per pixel)6, 16, 24, 32•Color space (actual # available colors)8 bits = 256 colors, 16 bits = 655369Display Modes•RGB mode–True RGB values are encoded for each pixel displayed•Palletized mode–Color indirection scheme–256 colors available–Value 0 to 255 is stored with each pixel–Color value looked up in CLUT10Color Objects•Windows uses to data structures–COLOREF: byte structure allows you to create colors on the fly–PALETTEENTRY: all creation of pallets with control flag components•Complete control of pallets is best done using DirectX11Drawing Text•TextOut( )–No formatting or processing of text string is done before it is displayed•DrawText( )–Allows justification, clipping, etc. prior to displaying text string12LaMothe Examples13TextOut Examplemain_window_handle = hwnd; // save main window handleHDC hdc = GetDC(hwnd); // get the dc and hold it// set the foreground color to randomSetTextColor(hdc, RGB(rand()%256,rand()%256,rand()%256));// set the background color to blackSetBkColor(hdc, RGB(0,0,0));// finally set the transparency mode to transparentSetBkMode(hdc, TRANSPARENT); // draw some text at a random locationTextOut(hdc,rand()%400,rand()%400, "GDI Text Demo!", strlen("GDI Text Demo!")); ReleaseDC(hwnd,hdc); // release the dc14What about WM_PAINT?case WM_PAINT: {// simply validate the windowhdc = BeginPaint(hwnd,&ps); // you would do all your painting here EndPaint(hwnd,&ps); // return successreturn(0);} break;15Additional Issues•You should consider saving the old values for foreground, ground, and transparency so that you can restore them after you set them•Example we might declare the following:COLORREF old_fcolor, // old foreground old_bcolor; // old backgroundint old_tmode; // old transparency16TextOut Example 2main_window_handle = hwnd; // save main window handleHDC hdc = GetDC(hwnd); // get the dc and hold it// save old attributes and set newold_fcolor = SetTextColor(hdc,RGB(255,255,255));old_bcolor = SetBkColor(hdc, RGB(0,0,0));old_tmode= SetBkMode(hdc, TRANSPARENT); // draw some text at a random locationTextOut(hdc, 120, 200, “Hello", strlen(“Hello")); // Restore the old valuesSetTextColor(hdc, old_fcolor);SetBkColor(hdc, old_bcolor);SetBkMode(hdc, old_tmode);ReleaseDC(hwnd,hdc); // release the dc17Text Metrics•The function GetTextMetrics can be used to query the system for things like–Font height–Average width–Max width–First character defined in font–Last character defined in font•Set of values returned in fields of a parameter of type tagTEXTMETRIC18Drawing Shapes•GDI allows you to draw things like–Points–Lines–Circles and ellipses–Rectangles–Curves–Polygons–Bitmaps–Metafiles (instructions that can be “replayed”)19Pens•Pens are GDI objects used to draw lines•Pens have two attributes–RGB color–Styles like•PS_DASH - - - -•PS_DOT . . . .•PS_DASHDOT _ . _ . •PS_NULL20Using a Pen// get the graphics device context hdc = GetDC(hwnd);// create a red colored pen HPEN red_pen = CreatePen(PS_SOLID,1,RGB(255, 0, 0));// selecting pen and saving old value HPEN old_hpen = (HPEN)SelectObject(hdc,red_pen);// draw with pen// deleting pen SelectObject(hdc,old_hpen); DeleteObject(red_ pen);// release the device context ReleaseDC(hwnd,hdc)21Brushes•Brushes are used to fill in line objects•Examples// creating a solid brushHBRUSH blue_brush = CreateSolidBrush(RGB(0,0,255));// creating a patterned brushHBRUSH green_brush = CreateHatchBrush(HS_DIAGCROSS, RGB(0,255,0));// creating a bitmap brush (8x8)HBRUSH bitmap_brush = CreatePatternBrush(hBitMap);22Selecting and Destroying Brushes// select brush and save old brushHBRUSH old_brush = SelectObject(hdc, red_brush);// use the brush// restore old brushSelectObject(hdc, old_brush);DeleteObject(red_brush);23Plotting Pointshdc = GetDC(hwnd); // get the dc for the window// draw 1000 pixelsfor (int index=0; index < 1000; index++) { // get random position int x = rand()%400; int y = rand()%300; COLORREF color = RGB(rand()%255,rand()%255,rand()%255); SetPixel(hdc, x,y, color);} // end for indexReleaseDC(hwnd, hdc); // release the dc24Drawing


View Full Document

U-M CIS 487 - Windows GDI Programming

Documents in this Course
Mad Maxim

Mad Maxim

10 pages

DirectX

DirectX

10 pages

Load more
Download Windows GDI Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Windows GDI Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Windows GDI Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?