📄 pictureviewer.cpp
字号:
// PictureViewer.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "PictureViewer.h"
#include <commctrl.h>
#include "Picture.h"
#include "ddraw.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE, LPTSTR);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
HWND main_window_handle; //used for recorde the main window handle(hwnd)
LPDIRECTDRAW4 lpDD4;
LPDIRECTDRAW lpDD;
LPDIRECTDRAWSURFACE lpDDSPrimary;
LPDIRECTDRAWSURFACE lpDDSBack;
LPDIRECTDRAWSURFACE lpDDSBuffer;
DDCAPS caps;
DDSURFACEDESC ddsd;
TCHAR szBitmap[50] = _T("\\storage card\\All.bmp");
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
DWORD SX;
DWORD SY;
BOOL InitDirectDraw(void);
void DestroyDirectDrawObjects();
BOOL LoadPicture( HDC hdc )
{
HDC memdc = ::CreateCompatibleDC( NULL);
HBITMAP bitmap = SHLoadDIBitmap( szBitmap );
if( bitmap == NULL )
{
MessageBox( main_window_handle, _T("bitmap handle is NULL !"), szTitle, MB_OK );
return NULL;
}
::SelectObject( memdc, bitmap );
if( FAILED( lpDDSBuffer->GetDC( &hdc ) ) )
return FALSE;
BITMAP bmp;
GetObject(bitmap,sizeof(BITMAP),&bmp);
StretchBlt( hdc, 0, 0, SX, SY, memdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
lpDDSBuffer->ReleaseDC( hdc);
return TRUE;
}
BOOL DirectDraw_Flip()
{
RECT rect;
rect.left = rect.top = 0 ;
rect.bottom = 600 ;
rect.right = 800;
if( FAILED( lpDDSBack->BltFast( 0, 0, lpDDSBuffer, NULL, DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY ) ) )
{
MessageBox( main_window_handle, _T("BltFast from lpDDSBuffer to lpDDSBack FAILED !"), szTitle, MB_OK );
return FALSE;
}
//将离屏页面(lpDDSBuffer)拷贝到后台页面(lpDDSBack)
//lpDDSBack->BltFast( 0, 0, lpDDSBuffer, NULL, DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY );
while(1)
{
lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
}
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_PICTUREVIEWER);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// It is important to call this function so that the application
// will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PICTUREVIEWER));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, 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]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name
hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
LoadString(hInstance, IDC_PICTUREVIEWER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance, szWindowClass);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (hwndCB)
CommandBar_Show(hwndCB, TRUE);
if( !InitDirectDraw( ) )
return FALSE;
LoadPicture( GetDC( hWnd ) );
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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)
{
HDC hdc;
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
main_window_handle = hWnd;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
hwndCB = CommandBar_Create(hInst, hWnd, 1);
CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
CommandBar_AddAdornments(hwndCB, 0, 0);
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
//DrawText(hdc, szHello, _tcslen(szHello), &rt,
// DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(hwndCB);
PostQuitMessage(0);
break;
case WM_TIMER:
LoadPicture( hdc );
DirectDraw_Flip();
break;
case WM_KEYDOWN:
switch( wParam )
{
case VK_LEFT:
DirectDraw_Flip();
break;
case VK_RIGHT:
DirectDraw_Flip();
break;
case VK_ESCAPE:
PostMessage( hWnd, WM_CLOSE, 0, 0 );
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hDlg, &rt1)) {
GetClientRect(GetParent(hDlg), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
BOOL InitDirectDraw(void)
{
DDSCAPS ddscaps;
HRESULT ddrval;
ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
SX = GetSystemMetrics( SM_CXSCREEN );
SY = GetSystemMetrics( SM_CYSCREEN );
if( ddrval != DD_OK)
{
DestroyDirectDrawObjects();
MessageBox( main_window_handle, _T("DirectDraw DirectDrawCreate FAILED"), szTitle, MB_OK );
DestroyWindow( main_window_handle );
return FALSE;
}
/* ddrval = lpDD->QueryInterface( IID_IDirectDraw4, (LPVOID*)&lpDD4 );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( main_window_handle, _T("DirectDraw DirectDrawCreate FAILED"), szTitle, MB_OK );
DestroyWindow( main_window_handle );
return FALSE;
}
*/
// Get exclusive mode
ddrval = lpDD->SetCooperativeLevel( main_window_handle, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( main_window_handle, _T("DirectDraw SetCooperativeLevel FAILED"), szTitle, MB_OK );
DestroyWindow( main_window_handle );
return FALSE;
}
// Set the video mode to 640x480x8
ddrval = lpDD->SetDisplayMode( SX, SY, 16 );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( main_window_handle, _T("DirectDraw SetDisplayMode FAILED"), szTitle, MB_OK );
DestroyWindow( main_window_handle );
return FALSE;
}
// Create the primary surface with 1 back buffer
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( main_window_handle, _T("DirectDraw CreateSurface FAILED"), szTitle, MB_OK );
DestroyWindow( main_window_handle );
return FALSE;
}
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface( &ddscaps, &lpDDSBack );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( main_window_handle, _T("DirectDraw GetAttachedSurface FAILED"), szTitle, MB_OK );
DestroyWindow( main_window_handle );
return FALSE;
}
//创建离屏页面
memset(&ddsd,0,sizeof(ddsd)); //清空结构内容
ddsd.dwSize=sizeof(ddsd); //设置大小
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;//指定页面类型
ddsd.dwWidth=600;
ddsd.dwHeight=800; //设置离屏页面大小
ddrval = lpDD->CreateSurface( &ddsd,&lpDDSBuffer,NULL );
if( ddrval != DD_OK )
{
DestroyDirectDrawObjects();
MessageBox( main_window_handle, _T("DirectDraw CreateSurface for offscreen Failed"), szTitle, MB_OK);
DestroyWindow( main_window_handle );
return FALSE;
}
//设置定时器
SetTimer(main_window_handle,1,50,NULL);
return TRUE;
}
void DestroyDirectDrawObjects()
{
if( lpDD != NULL)
{
if( lpDD4 != NULL )
{
if( lpDDSPrimary != NULL)
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
}
lpDD->Release();
lpDD = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -