/********************************************************************\ * generic.c: Source code for generic * * * * Comments: Generic Win32-based Application * * * * Functions: * * WinMain - Application entry point * * MainWndProc - main window procedure * * AboutDlgProc - dialog procedure for About dialog * * * * * \********************************************************************/ /********************* Header Files *********************/ #include #include "generic.h" #include /********************* Prototypes ***********************/ LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM ); LRESULT WINAPI AboutDlgProc( HWND, UINT, WPARAM, LPARAM ); /******************* Global Variables ********************/ HANDLE ghInstance; /********************************************************************\ * Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int) * * * * Purpose: Initializes Application * * * * Comments: Register window class, create and display the main * * window, and enter message loop. * * * * * \********************************************************************/ int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow ) { WNDCLASS wc; MSG msg; HWND hWnd; srand((unsigned)time(NULL)); if( !hPrevInstance ) { wc.lpszClassName = "GenericAppClass"; wc.lpfnWndProc = MainWndProc; wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.hInstance = hInstance; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 ); wc.lpszMenuName = "GenericAppMenu"; wc.cbClsExtra = 0; wc.cbWndExtra = 0; RegisterClass( &wc ); } ghInstance = hInstance; hWnd = CreateWindow( "GenericAppClass", "Generic Application", WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); ShowWindow( hWnd, nCmdShow ); while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return msg.wParam; } /********************************************************************\ * Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) * * * * Purpose: Processes Application Messages * * * * Comments: The following messages are processed * * * * WM_PAINT * * WM_COMMAND * * WM_DESTROY * * * * * \********************************************************************/ LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps; HDC hDC; char str[255]; int i, j, X=0, Y=0; static int maxX, maxY; TEXTMETRIC tm; HBRUSH hBrush; HPEN hPen, hPenOld; static HDC bitdc, memDC; static HBITMAP hbmp, hbit; switch( msg ) { /**************************************************************\ * WM_CREATE: * \**************************************************************/ case WM_CREATE: maxX = GetSystemMetrics(SM_CXSCREEN); maxY = GetSystemMetrics(SM_CYSCREEN); hDC = GetDC(hWnd); memDC = CreateCompatibleDC(hDC); hbit = CreateCompatibleBitmap(hDC, maxX, maxY); SelectObject(memDC, hbit); hBrush = (HBRUSH) GetStockObject(WHITE_BRUSH); hPen = CreatePen(PS_SOLID, 10, RGB(0, 255, 0)); SelectObject(memDC, hBrush); hPenOld = SelectObject(memDC, hPen); PatBlt(memDC, 0, 0, maxX, maxY, PATCOPY); GetTextMetrics(memDC, &tm); strcpy(str, "This is a test of text output to the window using Win32 API GDI calls!"); for(i=0; i < 24; i++) { SetTextColor(memDC, RGB(rand()%256,rand()%256,rand()%256)); TextOut(memDC, 10, Y+10, str, strlen(str)); Y += tm.tmHeight + tm.tmExternalLeading; } Rectangle(memDC, 50, 50, 150, 150); Ellipse(memDC, 150, 225, 350, 325); RoundRect(memDC, 250, 100, 350, 200, 20, 20); MoveToEx(memDC, 20, 20, (LPPOINT) NULL); LineTo(memDC, 200, 150); LineTo(memDC, 200, 400); hbmp = LoadImage(ghInstance, "test.bmp", IMAGE_BITMAP, 16, 16, LR_LOADFROMFILE); bitdc = CreateCompatibleDC(memDC); SelectObject(bitdc, hbmp); for(i=0; i < 30; i++) for(j=0; j < 30; j++) BitBlt(memDC, 20*i, 20*j, 16, 16, bitdc, 0, 0, SRCCOPY); SelectObject(memDC, hPenOld); DeleteObject(hPen); ReleaseDC(hWnd, hDC); break; /**************************************************************\ * WM_PAINT: * \**************************************************************/ case WM_PAINT: hDC = BeginPaint( hWnd, &ps ); // BitBlt(hDC, 0, 0, maxX, maxY, memDC, 0, 0, SRCCOPY); BitBlt(hDC, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right - ps.rcPaint.left, // width ps.rcPaint.bottom - ps.rcPaint.top, // height memDC, ps.rcPaint.left, ps.rcPaint.top, SRCCOPY); EndPaint( hWnd, &ps ); break; /**************************************************************\ * WM_COMMAND: * \**************************************************************/ case WM_COMMAND: switch( wParam ) { case IDM_ABOUT: DialogBox( ghInstance, "AboutDlg", hWnd, (DLGPROC) AboutDlgProc ); break; } break; /**************************************************************\ * WM_DESTROY: PostQuitMessage() is called * \**************************************************************/ case WM_DESTROY: DeleteDC(memDC); DeleteDC(bitdc); DeleteObject(hbit); DeleteObject(hbmp); PostQuitMessage( 0 ); break; /**************************************************************\ * Let the default window proc handle all other messages * \**************************************************************/ default: return( DefWindowProc( hWnd, msg, wParam, lParam )); } return 0; } /********************************************************************\ * Function: LRESULT CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM)* * * * Purpose: Processes "About" Dialog Box Messages * * * * Comments: The About dialog box is displayed when the user clicks * * About from the Help menu. * * * \********************************************************************/ LRESULT CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch( uMsg ) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch( wParam ) { case IDOK: EndDialog( hDlg, TRUE ); return TRUE; } break; } return FALSE; }