MFC 小知识总结三

时间:2021-08-02 09:41:13

1 载入资源位图



1 获取程序实例

HINSTANCE   m_hInst;

m_hInst=AfxGetInstanceHandle();                         //获取程序实例

 

2 载入图像

HBITMAP m_bmpTank;

HBITMAP m_bmpBomb;

m_bmpTank=LoadBitmap(m_hInst,MAKEINTRESOURCE(IDB_TANK));

m_bmpBomb=LoadBitmap(m_hInst,MAKEINTRESOURCE(IDB_BOMB));

3 将位图选入DC中

HDC m_TankDC;

HDC m_BombDC;

m_TankDC=CreateCompatibleDC(pDC->m_hDC);

m_BombDC=CreateCompatibleDC(pDC->m_hDC);

//选择图像

SelectObject(m_TankDC,m_bmpTank);

SelectObject(m_BombDC,m_bmpBomb);

 

4 绘图

BitBlt(pTankDC->m_hDC,0,0,TANK_WIDTH,TANK_WIDTH,m_TankDC,0,0,SRCCOPY);

TransparentBlt2(pDC->m_hDC,0,0,nWidth,nHeight,

m_TankDC,   0,0,nWidth,nHeight,RGB(255,0,0));



2 移动Pictutr控件

1 获得Picture的DC

CDC* m_pMapDC=GetDlgItem(IDC_MAP)->GetDC();

CDC* m_pTankDC=GetDlgItem(IDC_TANK)->GetDC();

2 将Picture控件移到指定矩形区域

//设置客户区矩形

m_ClientRect[0]=CRect(20,10,7*WIDTH+20,WIDTH+10);           //IDC_MAP

m_ClientRect[1]=CRect(20,50,NUM*WIDTH+20,NUM*WIDTH+50);     //IDC_FIELD

m_ClientRect[2]=CRect(260,12,TANK_WIDTH+260,TANK_WIDTH+12); //IDC_TNAK

//移动窗体

GetDlgItem(IDC_MAP)->MoveWindow(m_ClientRect[0]);           //IDC_MAP

GetDlgItem(IDC_FIELD)->MoveWindow(m_ClientRect[1]);         //IDC_FIELD

GetDlgItem(IDC_TANK)->MoveWindow(m_ClientRect[2]);

 

3 载入光标

1 载入光标

 

1)从文件载入光标

HCURSOR m_hCursor;

//Load the Cursor

m_hCursor=LoadCursorFromFile("res\\cursor.ani");  //.cur  or .ani

 

2)从程序自己定义的资源光标载入

m_hCursor=AfxGetApp()->LoadCursor(IDR_CURSOR);

3)从默认资源载入

m_hCursor=LoadCursor(NULL,IDC_CROSS);

2 设置光标

 响应WM_SETCURSOR消息,显示适当的光标形状

if(m_hCursor)

{

SetCursor(m_hCursor);

return TRUE;

}

else

return CDialog::OnSetCursor(pWnd, nHitTest, message);

4 播放背景音乐



构造函数:

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

初始化:

HICON icon=LoadIcon(AfxGetInstanceHandle(),(LPCTSTR)IDR_MAINFRAME);

SetIcon(icon,TRUE);

SetIcon(icon,FALSE);

MCI_OPEN_PARMS mciOpen;

//背景音乐

mciOpen.lpstrDeviceType=0;

mciOpen.lpstrElementName="./res/music.mp3";

mciSendCommand(NULL,MCI_OPEN, MCI_OPEN_ELEMENT,(DWORD)&mciOpen);

MCI_PLAY_PARMS mciPlay;

mciPlay.dwFrom = 0;

mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,(DWORD)&mciPlay);

SetTimer(1,210000,NULL);        //用来循环播放背景音乐

 

OnTimer函数:

OnTimer(UINT nIDEvent)

{

// TODO: 在此加入消息处理程序代码和/或调用默认值

if(nIDEvent==1)

{

MCI_PLAY_PARMS mciPlay;

mciPlay.dwFrom = 0;

mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,(DWORD)&mciPlay);

}

CDialog::OnTimer(nIDEvent);

}

控制背景音乐的播放:

void CMazeDlg::OnBnClickedBkmusic()

{

// TODO: 在此加入控件通知处理程序代码

UpdateData(TRUE);

if(m_bBkMusic)

{

MCI_PLAY_PARMS mciPlay;

mciPlay.dwFrom = 0;

mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,(DWORD)&mciPlay);

SetTimer(1,210000,NULL);

}

else

{

KillTimer(1);

mciSendCommand(mciOpen.wDeviceID,MCI_STOP,MCI_WAIT,NULL);

}

}

 

5 线程操作

CWinThread *m_Thread;

UINT Start(LPVOID pPar);               //開始

m_Thread=AfxBeginThread(Start,GetSafeHwnd());

m_Thread->SuspendThread();

m_Thread->SuspendThread();

转自:http://blog.csdn.net/shuilan0066/article/details/7804231