bool dll_init(IBox * p_in/*IN*/, IController ** p_out/*OUT*/)
{
HINSTANCE hExe=AfxGetResourceHandle();
HINSTANCE hDll=GetModuleHandle(_T("exchange.dll"));
AfxSetResourceHandle(hDll); * p_out = new MyDll;
(* p_out) -> draw(p_in -> getWND(), p_in -> getDrawRect()); AfxSetResourceHandle(hExe);
return true;
}MyDll是IController的子类,其中有个函数,需要bmp.LoadBitmap(IDB_BITMAP_TEST);可是不成功。AFX_MANAGE_STATE( AfxGetStaticModuleState());也不行,该怎么办啊?dllvc++
{
HINSTANCE hExe=AfxGetResourceHandle();
HINSTANCE hDll=GetModuleHandle(_T("exchange.dll"));
AfxSetResourceHandle(hDll); * p_out = new MyDll;
(* p_out) -> draw(p_in -> getWND(), p_in -> getDrawRect()); AfxSetResourceHandle(hExe);
return true;
}MyDll是IController的子类,其中有个函数,需要bmp.LoadBitmap(IDB_BITMAP_TEST);可是不成功。AFX_MANAGE_STATE( AfxGetStaticModuleState());也不行,该怎么办啊?dllvc++
不应该啊, 无论是win32 dll, 还是 mfc 规则dll ,
这个法子都应该 行啊,加载dll里的资源
#ifndef INTERFACE2_H
#define INTERFACE2_H
#pragma once#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
//#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
//#include <afxcmn.h> // MFC support for Windows Common Controls
#include <windows.h>
#include "resource.h"#define interface struct
//#define PURE 0interface IController
{
virtual void draw(HWND wnd, RECT & rc) PURE;
virtual void on_btn_up_pressed() PURE;
virtual void on_btn_left_pressed() PURE;
virtual void on_btn_right_pressed() PURE;
virtual void on_btn_down_pressed() PURE;
};//IBox接口由我们提供的exe实现,在调用你导出函数时,把IBox指针传给你
//你需要实现IController
interface IBox
{
virtual HWND getWND() PURE;
virtual RECT getDrawRect() PURE;
};
//dll提供的导出函数原型
typedef bool (*dll_entry_type)(IBox * p_in/*IN*/, IController ** p_out/*OUT*/) ;extern "C" __declspec(dllexport) bool dll_init(IBox * p_in/*IN*/, IController ** p_out/*OUT*/);#endifinterface MyDll : public IController
{
public:
MyDll();
virtual ~MyDll();
private:
CPoint m_pt; //图片左上角起点坐标
int m_dis; //每次移动距离
CSize m_sizeWnd; //矩形长宽
CSize m_sizePic; //图片长宽
CBitmap m_bitmap; //位图 BITMAP m_BitmapInfo;
HWND m_wnd; //对话框指针
CRect m_rc; //矩形窗口大小
CRect m_rect; //对话框大小
public:
void draw(HWND wnd, RECT & rc); //对话框句柄,绘画矩形
void drawBk(); //画图
void on_btn_up_pressed(); //矩形窗口向上移动_对应图片向相反方向移动
void on_btn_left_pressed(); //矩形窗口向左移动_对应图片向相反方向移动
void on_btn_right_pressed(); //矩形窗口向右移动_对应图片向相反方向移动
void on_btn_down_pressed(); //矩形窗口向下移动_对应图片向相反方向移动
};bool dll_init(IBox * p_in/*IN*/, IController ** p_out/*OUT*/)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
//HINSTANCE hExe=AfxGetResourceHandle();
// HINSTANCE hDll=GetModuleHandle("MyDll_Lijie_15215141776.dll");
// AfxSetResourceHandle(hDll); * p_out = new MyDll;
(* p_out) -> draw(p_in -> getWND(), p_in -> getDrawRect());// AfxSetResourceHandle(hExe); return true;
}interface2.cpp/////////////////////////////////////#include "interface2.h"MyDll::MyDll()
{
m_pt.x = 0; //图片左上角起点坐标
m_pt.y = 0;
m_dis = 10; //每次移动距离(像素)
}MyDll::~MyDll()
{
;
}//////////////////////////////////////////////////////
void MyDll::draw(HWND wnd, RECT & rc)
{
m_wnd = wnd; //对话框指针
m_rc = rc; //矩形窗口大小
::GetClientRect(m_wnd,&m_rect);}void MyDll::drawBk()
{ if(m_wnd==NULL)
return; HDC hdc=GetDC(m_wnd);
CDC dc;
dc.Attach(hdc);
CDC dcMem;
CDC dcTem;
CBitmap bmp; //内存中承载临时图象的位图
CBitmap bmpTem; dcMem.CreateCompatibleDC(&dc);
bmpTem.CreateCompatibleBitmap(&dc,m_rc.Width(),m_rc.Height());
dcMem.SelectObject(&bmpTem); dcTem.CreateCompatibleDC(&dc);
// HBITMAP hBitmap = LoadBitmap(GetModuleHandle(_T("MyDll_Lijie_15215141776.dll")),MAKEINTERSOURCE(IDB_BITMAP_TEST));
// bmp.Attach(hBitmap);
bmp.LoadBitmap(IDB_BITMAP_TEST);
// bmp.CreateCompatibleBitmap(&dc,m_rc.Width(),m_rc.Height());
dcTem.SelectObject(&bmp); dcMem.FillSolidRect(CRect(0 ,0 ,m_rc.Width(),m_rc.Height()),RGB(178,178,178));
dcMem.BitBlt( 0 ,0 ,m_rc.Width(),m_rc.Height(),&dcTem ,m_pt.x,m_pt.y,SRCCOPY);
dc.BitBlt(0,0,m_rc.Width(),m_rc.Height(),&dcMem,0,0,SRCCOPY); dcMem.DeleteDC(); //删除DC
bmp.DeleteObject(); //删除位图
}void MyDll::on_btn_down_pressed()
{ if(m_pt.y < m_rect.Height())
{
m_pt.y += m_dis;
drawBk();
}
}void MyDll::on_btn_right_pressed()
{
if(m_pt.x < m_rect.Width())
{
m_pt.x += m_dis;
drawBk();
}
}void MyDll::on_btn_left_pressed()
{
if(m_pt.x > -500)
{
m_pt.x -= m_dis;
drawBk();
}
}void MyDll::on_btn_up_pressed()
{ if(m_pt.y > -500)
{
m_pt.y -= m_dis;
drawBk();
}
}
resource.h,文件/////////////////////////////////////////////////{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by MyDll_Lijie_15215141776.rc
//
#define IDB_BITMAP_TEST 101// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif