roland_c,你的方法是通过得到图标而绘制的,不用句柄的话,应该用什么?请说说具体过程

解决方案 »

  1.   

    回去查了一下书,可以直接使用ExtractIcon(hInst,lpszExe,index)
    hInst是程序实例句柄,lpszExe是包含图标的exe或DLL或ICO文件的文件名,index为图标在文件中的索引号。我试了一下,示例如下:
    // icon.cpp : Defines the entry point for the application.
    //#include "stdafx.h"
    #include "windows.h"
    #include "shellapi.h"HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
      // TODO: Place code here.
    static TCHAR szAppName[]=TEXT("Icon Demo");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass; hInst=hInstance; wndclass.style=CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc=WndProc;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hInstance=hInstance;
    wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName=NULL;
    wndclass.lpszClassName=szAppName; if(!RegisterClass(&wndclass))
    {
    MessageBox(NULL,TEXT("This program requires Windows NT!"),szAppName,MB_ICONERROR);
    return 0;
    } hwnd=CreateWindow(szAppName,TEXT("Icon Demostration"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
    CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    PAINTSTRUCT ps;
    static HICON hIcon;
    HDC hdc; switch(message)
    {
    case WM_CREATE:
    hIcon=ExtractIcon(hInst,"e:\\mydoc\\x.ico",0);
    return 0; case WM_PAINT:
    hdc=BeginPaint(hwnd,&ps);
    DrawIcon(hdc,10,10,hIcon);
    EndPaint(hwnd,&ps); return 0; case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
      

  2.   

    这个方法好,可以直接从ico文件获得icon句柄来绘图。对于有很多图标的这个事例正是再适合不过了。