48
Quanta Confidentia QUANTA WBU Study Report 1 昭昭 2008/08/01

Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Embed Size (px)

Citation preview

Page 1: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU

Study Report 1

昭正2008/08/01

Page 2: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -2-

大綱• 簡介WinMain流程• 簡介原始範例結構• 背景填色• 繪製圖片 1• 繪製圖片 2

Page 3: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -3-

#define dim(x) (sizeof(x) / sizeof(x[0]))

const TCHAR szAppName[] = TEXT ("Shapes");

const struct decodeUINT MainMessages[] = {

WM_PAINT, DoPaintMain, WM_DESTROY, DoDestroyMain, 

};

Page 4: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -4-

WinMainint WINAPI WinMain (HINSTANCE hInstance, HINSTANCE

hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {

// Initialize this instance.

hwndMain = InitInstance(hInstance, lpCmdLine, nCmdShow); 

// Application message loop

while (GetMessage (&msg, NULL, 0, 0)) {  TranslateMessage (&msg);  DispatchMessage (&msg);  } // Instance cleanup

return TermInstance (hInstance, msg.wParam); }

Page 5: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -5-

InitInstance// InitInstance - Instance initialization

HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){ WNDCLASS wc;  // Register application main window class. wc.lpfnWndProc = MainWndProc; // Callback function  wc.lpszClassName = szAppName; // Window class name 

// Create main window. hWnd = CreateWindowEx (WS_EX_NODRAG, // Ex Style  szAppName, // Window class

TEXT("Shapes"), // Window title WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL);

// Standard show and update calls ShowWindow (hWnd, nCmdShow);  UpdateWindow (hWnd);  return hWnd;}

Page 6: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -6-

MainWndProc

// MainWndProc - Callback function for application windowLRESULT CALLBACK MainWndProc (HWND hWnd, UINT

wMsg, WPARAM wParam, LPARAM lParam) { // Search message list to see if we need to handle this // message. If in list, call procedure. for (i = 0; i < dim(MainMessages); i++) {

if (wMsg == MainMessages[i].Code)  return (*MainMessages[i].Fxn)(hWnd, wMsg,

wParam, lParam); }

return DefWindowProc (hWnd, wMsg, wParam, lParam); }

WM_QUIT

Page 7: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -7-

DoPaintMain

LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)

{

//主程式}

Page 8: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -8-

BeginPaint

//The BeginPaint function prepares the specified window for painting and fills a PAINTSTRUCT structure with information about the painting.

HDC BeginPaint(

HWND hwnd, // handle to window LPPAINTSTRUCT lpPaint // paint information

);

Page 9: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -9-

EndPaint

//The EndPaint function marks the end of painting in the specified window. This function is required for each call to the BeginPaint function, but only after painting is complete.

BOOL EndPaint(

HWND hWnd, // handle to window

CONST PAINTSTRUCT *lpPaint // paint data

);

Page 10: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -10-

DoPaintMain{ PAINTSTRUCT ps;  RECT rect;  HDC hdc;  HBRUSH hBr;  TCHAR szText[128];  GetClientRect (hWnd, &rect); 

hdc = BeginPaint (hWnd, &ps);  //繪圖程式碼:背景填色、繪圖 1、繪圖 2EndPaint (hWnd, &ps);  

return 0;}

WM_DESTROY

Page 11: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -11-

背景填色hBr = (HBRUSH) GetStockObject

(DKGRAY_BRUSH); FillRect(hdc,&rect,hBr); 

// DeleteObject(hBr);

Page 12: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -12-

繪製圖片 1HBITMAP hBitmap,hOldBitmap;BITMAP bmp;HDC hdcMem;TCHAR sz[]=TEXT("\\bitmap1.bmp");hBitmap=SHLoadDIBitmap((LPCTSTR)sz); hdcMem=CreateCompatibleDC(hdc); hOldBitmap=(HBITMAP) SelectObject(hdcMem,hBitmap); GetObject(hBitmap,sizeof(BITMAP),&bmp);   BitBlt(hdc,rect.left,rect.top,bmp.bmWidth,bmp.bmHeight,

hdcMem,0,0,SRCCOPY); SelectObject(hdcMem,hOldBitmap); DeleteObject (hOldBitmap); DeleteDC(hdcMem); 

Page 13: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -13-

繪製圖片 2

hBitmap=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_ BITMAP1)); hdcMem=CreateCompatibleDC(hdc); hOldBitmap= (HBITMAP) SelectObject(hdcMem,hBitmap); GetObject(hBitmap,sizeof(BITMAP),&bmp);  BitBlt(hdc,rect.left,rect.top,bmp.bmWidth,bmp.bmHeight,hdcMem,0,0,SRCCOPY); SelectObject(hdcMem,hOldBitmap);  

DeleteObject (hOldBitmap); DeleteDC(hdcMem); 

DoPaintMain

Page 14: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -14-

DoDestroyMain

// DoDestroyMain - Process WM_DESTROY message for window

LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {

PostQuitMessage (0);  return 0;

}

WinMain

Page 15: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -15-

TermInstance

// TermInstance - Program cleanup

int TermInstance (HINSTANCE hInstance, int nDefRC) {

return nDefRC;

}

WinMain

Page 16: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -16-

#include

#include <windows.h> // For all that Windows stuff

#include "shapes.h" // Program-specific stuff

#include "resource.h"

Page 17: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -17-

decodeUINT

struct decodeUINT { // Structure associates

UINT Code; // messages

// with a function.

LRESULT (*Fxn)(HWND, UINT, WPARAM,

LPARAM);

};

Page 18: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -18-

LRESULT

typedef LONG_PTR LRESULT;

Page 19: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -19-

WNDCLASS//This structure contains the window class attributes that are registered

by the RegisterClass function. typedef struct _WNDCLASS {

UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName;

} WNDCLASS;

Page 20: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -20-

PAINTSTRUCT

//This structure contains information that an application uses to paint the client area of a window owned by that application.

typedef struct tagPAINTSTRUCT { HDC hdc; BOOL fErase; RECT rcPaint; BOOL fRestore; BOOL fIncUpdate; BYTE rgbReserved[32];

} PAINTSTRUCT;

Page 21: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -21-

RECT

//This structure defines the coordinates of the upper-left and lower-right corners of a rectangle.

typedef struct _RECT {

LONG left;

LONG top;

LONG right;

LONG bottom;

} RECT;

Page 22: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -22-

HDC

Page 23: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -23-

HBRUSH

Page 24: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -24-

TCHAR

Page 25: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -25-

GetClientRect

//The GetClientRect function retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).

BOOL GetClientRect(HWND hWnd,     LPRECT lpRect

);

Page 26: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -26-

GetStockObject

//The GetStockObject function retrieves a handle to one of the stock pens, brushes, fonts, or palettes.

HGDIOBJ GetStockObject(

int fnObject // stock object type

);

Page 27: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -27-

Page 28: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -28-

FillRect

//This function fills a rectangle using the specified brush. This function fills the rectangle's left and top borders, but excludes the right and bottom borders.

int FillRect(

HDC hDC,

CONST RECT* lprc,

HBRUSH hbr

);

Page 29: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -29-

SelectObject

//The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type.

HGDIOBJ SelectObject(

HDC hdc, // handle to DC

HGDIOBJ hgdiobj // handle to object

);

Page 30: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -30-

HBITMAP

Page 31: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -31-

BITMAP

//This structure defines the type, width, height, color format, and bit values of a bitmap.

typedef struct tagBITMAP {

LONG bmType;

LONG bmWidth;

LONG bmHeight;

LONG bmWidthBytes;

WORD bmPlanes;

WORD bmBitsPixel;

LPVOID bmBits;

} BITMAP;

Page 32: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -32-

SHLoadDIBitmap

Page 33: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -33-

CreateCompatibleDC

//This function creates a memory device context (DC) compatible with the specified device.

HDC CreateCompatibleDC(

HDC hdc

);

Page 34: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -34-

GetObject

//This function obtains information about a specified graphics object. Depending on the graphics object, the function places a filled-in BITMAP, DIBSECTION, LOGBRUSH, LOGFONT, or LOGPEN structure into a specified buffer.

int GetObject( HGDIOBJ hgdiobj, int cbBuffer, LPVOID lpvObject

);

Page 35: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -35-

sizeof

//This function returns the size, in bytes, of the specified resource.

DWORD SizeofResource(

HMODULE hModule,

HRSRC hResInfo

);

Page 36: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -36-

BitBlt//This function transfers pixels from a specified source rectangle to a sp

ecified destination rectangle, altering the pixels according to the selected raster operation (ROP) code.

BOOL BitBlt( HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop

);

Page 37: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -37-

DeleteDC

//This function deletes the specified device context (DC).

BOOL DeleteDC(

HDC hdc

);

Page 38: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -38-

DeleteObject

//This function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object. After the object is deleted, the specified handle is no longer valid.

BOOL DeleteObject(

HGDIOBJ hObject

);

Page 39: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -39-

LoadBitmap

//This function loads the specified bitmap resource from the executable file for a module.

HBITMAP LoadBitmap(

HINSTANCE hInstance,

LPCTSTR lpBitmapName

);

Page 40: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -40-

MAKEINTRESOURCE

//This macro converts an integer value to a resource type compatible with Windows resource-management functions. This macro is used in place of a string containing the name of the resource.

LPTSTR MAKEINTRESOURCE(

WORD wInteger;

);

Page 41: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -41-

GetMessage

//This function retrieves a message from the calling thread's message queue and places it in the specified structure.

BOOL GetMessage(

LPMSG lpMsg,

HWND hWnd,

UINT wMsgFilterMin,

UINT wMsgFilterMax

);

Page 42: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -42-

TranslateMessage

//This function translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function.

BOOL TranslateMessage(

const MSG* lpMsg

);

Page 43: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -43-

DispatchMessage

//This function dispatches a message to a window procedure. It is typically used to dispatch a message retrieved by the GetMessage function.

LONG DispatchMessage(

const MSG* lpmsg

);

Page 44: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -44-

CreateWindowEx//This function creates an overlapped, pop-up, or child window with an extende

d style; otherwise, this function is identical to the CreateWindow function.HWND CreateWindowEx(

DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam

);

Page 45: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -45-

ShowWindow

//This function sets the specified window's show state.

BOOL ShowWindow(

HWND hWnd,

int nCmdShow

);

Page 46: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -46-

UpdateWindow

//This function updates the client area of the specified window by sending a WM_PAINT message to the window if the window's update region is not empty.

UpdateWindow sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue.

If the update region is empty, no message is sent.

BOOL UpdateWindow(

HWND hWnd

);

Page 47: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -47-

DefWindowProc

//This function calls the default window procedure to provide default processing for any window messages that an application does not process. This function ensures that every message is processed. DefWindowProc is called with the same parameters received by the window procedure.

LRESULT DefWindowProc(

HWND hWnd,

UINT Msg,

WPARAM wParam,

LPARAM lParam

);

Page 48: Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01

Quanta ConfidentialQUANTAWBU -48-

PostQuitMessage

//This function indicates to Windows that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message.

void PostQuitMessage( int nExitCode

);