?? move pic.cpp
字號:
cxScr = GetSystemMetrics (SM_CXSCREEN) ;
cyScr = GetSystemMetrics (SM_CYSCREEN) ;
// 計算最合適的顯示尺寸
fcxDiv = 5 * (float) (bitmap.bmWidth) / (float) (cxScr) / 2 ;
fcyDiv = 5 * (float) (bitmap.bmHeight) / (float) (cyScr) / 3 ;
// 獲得相對于最合適的顯示區域,圖像比較寬的一邊
if ( (fDiv = (fcxDiv > fcyDiv) ? fcxDiv : fcyDiv) <= 1)
fDiv = 1 ;
// 計算出最合適的大小,并保持圖像長寬比例
cxClient = (int) (bitmap.bmWidth / fDiv) ;
cyClient = (int) (bitmap.bmHeight / fDiv) ;
// 重新計算窗口尺寸和坐標
rcWin.left -= (cxClient - (rcClient.right - rcClient.left)) / 2 ;
rcWin.right += (cxClient - (rcClient.right - rcClient.left)) / 2 ;
rcWin.top -= (cyClient - (rcClient.bottom - rcClient.top)) / 2 ;
rcWin.bottom += (cyClient - (rcClient.bottom - rcClient.top)) / 2 ;
// 經過多次打開圖像后,窗口位置就不再是屏幕中央了。不知道哪里出了問題
MoveWindow (hwnd, rcWin.left, rcWin.top, rcWin.right - rcWin.left,
rcWin.bottom - rcWin.top, TRUE);
/*==================================================
讓Static顯示圖像
==================================================*/
// Static控件應該調整了位置和大小。下面顯示圖像
hdc = GetDC (hwnd) ;
// 把圖像載入內存設備表
hdcMem = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem, hBitmap) ;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
{
// 這里以后應該改進,因為我希望空白地方能夠出現在隨機位置
if (i * 4 + j == 15)
{ idStcBlank = 15;
break ;
}
hdcStc = CreateCompatibleDC (hdc) ;
// 創建新的圖像句柄
hBmpStc = CreateCompatibleBitmap (hdc,
cxClient / 4,
cyClient / 4);
SelectObject (hdcStc, hBmpStc) ;
// 分割圖像并拷貝到hdcStc內存設備表
StretchBlt (hdcStc, 0, 0,
cxClient / 4,
cyClient / 4,
hdcMem,
bitmap.bmWidth * j / 4,
bitmap.bmHeight * i / 4,
bitmap.bmWidth / 4,
bitmap.bmHeight / 4,
SRCCOPY) ;
// 設置對應的Static控件的圖像
SendDlgItemMessage (hwnd,
4 * i + j,
STM_SETIMAGE,
(WPARAM) IMAGE_BITMAP,
(LPARAM) hBmpStc) ;
DeleteObject (hBmpStc) ; // 刪除句柄,這一句可以不要,什么影響?
DeleteDC (hdcStc) ;
}
DeleteDC (hdcMem) ;
ReleaseDC (hwnd, hdc) ;
// 使Static控件可以接受mouse點擊消息
bWinFlag = false ;
// 使reset菜單有效
EnableMenuItem (hMenu, IDM_GAME_RESET, MF_ENABLED) ;
}
return 0 ;
case ID_GAME_EXIT: // 退出游戲
SendMessage (hwnd, WM_CLOSE, 0, 0);
return 0;
case ID_HELP_ABOUT:
DialogBox (hInst, TEXT ("AboutDlg"), hwnd, AboutDlgProc) ;
return 0;
default:
if (bWinFlag == false)
{
/*==================================================
Windows通知Static被鼠標點擊
原本應該這樣,像菜單項處理一樣,使用case id來判斷,
但是我們就要重復16次,為了方便和美觀,我就這樣處理。
因為除了菜單消息就是點擊Static控件消息了。
==================================================*/
// 獲得被點擊的Static控件的id
idStcClk = (int) LOWORD (wParam) ;
// 得到BlankStatic和被點擊的Static控件的位置信息
iStcClkPos = iStcPos[idStcClk / 4][idStcClk % 4] ;
iStcBlankPos = iStcPos[idStcBlank / 4][idStcBlank % 4] ;
// 判斷是否應該對點擊做出反應
if (iStcClkPos == iStcBlankPos) // player click the blank static
return 0 ;
if (iStcClkPos != iStcBlankPos - 4 && // not up static of blank static
iStcClkPos != iStcBlankPos + 4 && // not below static of blank static
iStcClkPos != iStcBlankPos - 1 && // not left static of blank static
iStcClkPos != iStcBlankPos + 1) // not right static of blank static
return 0 ;
// 移動Static控件
MoveWindow (hwndPic_Button[idStcClk / 4][idStcClk % 4],
(iStcBlankPos % 4) * cxClient / 4,
(iStcBlankPos / 4) * cyClient / 4,
cxClient / 4, cyClient / 4, TRUE) ;
MoveWindow (hwndPic_Button[idStcBlank / 4][idStcBlank % 4],
(iStcClkPos % 4) * cxClient / 4,
(iStcClkPos / 4) * cyClient / 4,
cxClient / 4, cyClient / 4, TRUE) ;
// 更新Static控件的位置信息
iStcPos[idStcClk / 4][idStcClk % 4] = iStcBlankPos ;
iStcPos[idStcBlank / 4][idStcBlank / 4] = iStcClkPos ;
// 判斷是否勝利
int iWinFlag = 0 ;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (iStcPos[i][j] == 4 * i + j)
iWinFlag ++ ;
if (iWinFlag == 16) // win the game
{
// 顯示祝賀信息
MessageBox (hwnd, szWinGame, szAppName,
MB_OK | MB_ICONINFORMATION) ;
bWinFlag = true ;
}
}
return 0 ;
}
break;
case WM_DESTROY:
if (hBitmap)
DeleteObject (hBitmap) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
/*==========================================================
從位圖文件創建位圖對象
==========================================================*/
HBITMAP CreateBitmapObjectFromDibFile (HDC hdc, PTSTR szFileName)
{
BITMAPFILEHEADER * pbmfh ;
BOOL bSuccess ;
DWORD dwFileSize, dwHighSize, dwBytesRead ;
HANDLE hFile ;
HBITMAP hBitmap ;
// 打開文件: 只讀,寫禁止
hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;
if (hFile == INVALID_HANDLE_VALUE)
return NULL ;
// 讀入整個文件
dwFileSize = GetFileSize (hFile, &dwHighSize) ;
if (dwHighSize)
{
CloseHandle (hFile) ;
return NULL ;
}
pbmfh = (BITMAPFILEHEADER *) malloc (dwFileSize) ;
if (!pbmfh)
{
CloseHandle (hFile) ;
return NULL ;
}
bSuccess = ReadFile (hFile, pbmfh, dwFileSize, &dwBytesRead, NULL) ;
CloseHandle (hFile) ;
// 驗證文件
if (!bSuccess || (dwBytesRead != dwFileSize)
|| (pbmfh->bfType != * (WORD *) "BM")
|| (pbmfh->bfSize != dwFileSize))
{
free (pbmfh) ;
return NULL ;
}
// 創建DDB
hBitmap = CreateDIBitmap (hdc,
(BITMAPINFOHEADER *) (pbmfh + 1),
CBM_INIT,
(BYTE *) pbmfh + pbmfh->bfOffBits,
(BITMAPINFO *) (pbmfh + 1),
DIB_RGB_COLORS) ;
free (pbmfh) ;
return hBitmap ;
}
/*==========================================================
初始化OPENFILENAME結構
==========================================================*/
void InitOfnStruct (OPENFILENAME& ofn, HWND hWin)
{
// initialize OPENFILENAME struct
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hWin ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = szFileName ;
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrFileTitle = szTitleName ;
ofn.nMaxFileTitle = MAX_PATH ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = 0 ;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = TEXT ("bmp") ;
ofn.lCustData = 0 ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
}
/*==========================================================
對話框窗口函數
==========================================================*/
BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG :
return TRUE ;
case WM_COMMAND :
switch (LOWORD (wParam))
{
case IDOK :
EndDialog (hDlg, 0) ;
return TRUE ;
}
break ;
}
return FALSE ;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -