Chinaunix首页 | 论坛 | 博客
  • 博客访问: 510125
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-28 01:13:12

《windows程序设计》dll这一章末尾里作者给我们讲了一个纯资源型的DLL,我觉得非常有意思。
看看纯资源型的DLL是怎样加载进内存的?

LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static HBITMAP hBitmap;
    static int cxClient, cyClient;
    static HDC hMem;
    HDC hdc;
    PAINTSTRUCT ps;
    static POINT pt;
    int x, y;
    static HINSTANCE hLibrary;
    BITMAP bp;
    
    switch(message) {
    case WM_CREATE:
        // 这是dll文件的名字

        if(NULL == (hLibrary = LoadLibrary(TEXT("dll.dll")))) {
            MessageBox(hwnd, TEXT("can't load dll.dll"),
                TEXT("error"), MB_ICONERROR);
            return -1;
        }
        hdc = GetDC(hwnd);
        // 这是DLL里的资源

        hBitmap = LoadBitmap(hLibrary, TEXT("DLL"));
        hMem = CreateCompatibleDC(hdc);
        SelectObject(hMem, hBitmap);
        GetObject(hBitmap, sizeof(BITMAP), &bp);
        pt.x = bp.bmWidth;
        pt.y = bp.bmHeight;
        ReleaseDC(hwnd, hdc);
        return 0;
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        return 0;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        for(x = 0; x < cxClient; x += pt.x)
            for(y = 0; y < cyClient; y += pt.y)
                BitBlt(hdc, x, y, pt.x, pt.y, hMem, 0, 0, SRCCOPY);
            EndPaint(hwnd, &ps);
            return 0;
    case WM_DESTROY:
        if(hLibrary)
            FreeLibrary(hLibrary);
        if(hBitmap)
            DeleteObject(hBitmap);
        if(hMem)
            DeleteDC(hMem);
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(1147) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~