小知识点总结三


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 获得PictureDC

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();

相关文章
相关标签/搜索