// basicPPC.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "basicPPC.h" #include #include #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE g_hInst; // current instance HWND g_hWndMenuBar; // menu bar handle HWND MainWindow; //global handle to main window HDC screenDC; HDC memoryDC; HDC pSPLASH; // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE, LPTSTR); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); //caller must prepare DC with CreateCompatibleDC bool loadBitmapIntoDC(HINSTANCE hIns, int resourceID, HDC dc); //Creates a memory buffer DC with the size/bitdepth of the Bitmap bool createMemoryBufferDCFromBitmap(HINSTANCE hIns, int resourceID, HDC dc); //Creates a memory buffer DC with the given size/bitdepth bool createMemoryBufferDC(int width, int height, WORD planes, WORD bitsPixel, HDC dc); void loadGFX(); void unloadGFX(); void doDraw(); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; // Perform application initialization: if (!InitInstance(hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable; hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BASICPPC)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass) { WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BASICPPC)); wc.hCursor = 0; wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wc.lpszMenuName = 0; wc.lpszClassName = szWindowClass; return RegisterClass(&wc); } // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; TCHAR szTitle[MAX_LOADSTRING]; // title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name g_hInst = hInstance; // Store instance handle in our global variable // SHInitExtraControls should be called once during your application's initialization to initialize any // of the device specific controls such as CAPEDIT and SIPPREF. SHInitExtraControls(); LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_BASICPPC, szWindowClass, MAX_LOADSTRING); //If it is already running, then focus on the window, and exit hWnd = FindWindow(szWindowClass, szTitle); if (hWnd) { // set focus to foremost child window // The "| 0x00000001" is used to bring any owned windows to the foreground and // activate them. SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001)); return 0; } if (!MyRegisterClass(hInstance, szWindowClass)) { return FALSE; } hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } // When the main window is created using CW_USEDEFAULT the height of the menubar (if one // is created is not taken into account). So we resize the window after creating it // if a menubar is present if (g_hWndMenuBar) { RECT rc; RECT rcMenuBar; GetWindowRect(hWnd, &rc); GetWindowRect(g_hWndMenuBar, &rcMenuBar); rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top); MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE); } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; static SHACTIVATEINFO s_sai; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_HELP_ABOUT: DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About); break; case IDM_OK: SendMessage (hWnd, WM_DESTROY, 0, 0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_CREATE: SHMENUBARINFO mbi; memset(&mbi, 0, sizeof(SHMENUBARINFO)); mbi.cbSize = sizeof(SHMENUBARINFO); mbi.hwndParent = hWnd; mbi.nToolBarId = IDR_MENU; mbi.hInstRes = g_hInst; if (!SHCreateMenuBar(&mbi)) { g_hWndMenuBar = NULL; } else { g_hWndMenuBar = mbi.hwndMB; } // Initialize the shell activate info structure memset(&s_sai, 0, sizeof (s_sai)); s_sai.cbSize = sizeof (s_sai); MainWindow = hWnd; loadGFX(); // Tell our app to repaint SendMessage(MainWindow,WM_PAINT, NULL,NULL); break; case WM_PAINT: HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hWnd, &ps); EndPaint (hWnd, &ps); doDraw(); break; case WM_DESTROY: unloadGFX(); CommandBar_Destroy(g_hWndMenuBar); PostQuitMessage(0); break; case WM_ACTIVATE: // Notify shell of our activate message SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE); break; case WM_SETFOCUS: //show ShowWindow(MainWindow, SW_SHOWNORMAL); break; case WM_KILLFOCUS: //hide ShowWindow(MainWindow, SW_HIDE); break; case WM_SETTINGCHANGE: SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: { // Create a Done button and size it. SHINITDLGINFO shidi; shidi.dwMask = SHIDIM_FLAGS; shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU; shidi.hDlg = hDlg; SHInitDialog(&shidi); } return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; case WM_CLOSE: EndDialog(hDlg, message); return TRUE; #ifdef _DEVICE_RESOLUTION_AWARE case WM_SIZE: { DRA::RelayoutDialog( g_hInst, hDlg, DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX)); } break; #endif } return (INT_PTR)FALSE; } //caller must prepare DC with CreateCompatibleDC bool loadBitmapIntoDC(HINSTANCE hIns, int resourceID, HDC dc) { HBITMAP hBitmap; HBITMAP hOldBitmap; hBitmap = ::LoadBitmap(hIns, MAKEINTRESOURCE(resourceID)); hOldBitmap = (HBITMAP) SelectObject(dc, hBitmap); DeleteObject(hOldBitmap); DeleteObject(hBitmap); return true; } //Creates a memory buffer DC with the size/bitdepth of the Bitmap bool createMemoryBufferDCFromBitmap(HINSTANCE hIns, int resourceID, HDC dc) { BITMAP bm; HBITMAP hBitmap; HBITMAP hOldBitmap; hBitmap = ::LoadBitmap(hIns, MAKEINTRESOURCE(resourceID)); GetObject(hBitmap, sizeof(bm), &bm); hBitmap = CreateBitmap(bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel,NULL); hOldBitmap = (HBITMAP) SelectObject(dc, hBitmap); DeleteObject(hOldBitmap); DeleteObject(hBitmap); return true; } //Creates a memory buffer DC with the given size/bitdepth bool createMemoryBufferDC(int width, int height, WORD planes, WORD bitsPixel, HDC dc) { HBITMAP hBitmap; HBITMAP hOldBitmap; hBitmap = CreateBitmap(width, height, planes, bitsPixel,NULL); hOldBitmap = (HBITMAP) SelectObject(dc, hBitmap); DeleteObject(hOldBitmap); DeleteObject(hBitmap); return true; } void loadGFX() { int screenX = 0; int screenY = 0; int screenBitDepth = 0; int screenPlanes = 0; screenDC = GetDC(MainWindow); memoryDC = CreateCompatibleDC(screenDC); screenX = GetDeviceCaps(screenDC, HORZRES); screenY = GetDeviceCaps(screenDC, VERTRES); screenBitDepth = GetDeviceCaps(screenDC, BITSPIXEL); screenPlanes = GetDeviceCaps(screenDC, PLANES); createMemoryBufferDC(screenX, screenY, screenPlanes, screenBitDepth, memoryDC); SetBkColor(memoryDC, RGB(255, 255, 255)); SetTextColor(memoryDC, RGB(0, 0, 0)); SetBkMode(memoryDC, TRANSPARENT); pSPLASH= CreateCompatibleDC(screenDC); loadBitmapIntoDC(g_hInst,IDB_SPLASH,pSPLASH); } void unloadGFX() { DeleteDC(pSPLASH); DeleteDC(memoryDC); } void doDraw() { RECT rect; //area we're going to draw text in rect.top=0; rect.left=0; rect.right=80; rect.bottom=20; //copy image to memory buffer (backbuffer) BitBlt(memoryDC, 0, 0, 150, 150, pSPLASH, 0, 0, SRCCOPY); //write some text on top of the memory buffer DrawText(memoryDC, L"RAWR...", -1, &rect, DT_WORDBREAK); //copy memory buffer to screen buffer BitBlt(screenDC, 0, 0, 150, 150, memoryDC, 0, 0, SRCCOPY); }