如题,要纯API写成的,不要MFC。请给出实例,谢谢。

解决方案 »

  1.   

    #include <windows.h>#define ID_MYOWN   1000 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    BOOL InitApp(HINSTANCE);
    BOOL InitInstance(HINSTANCE, int);void DrawPic(HDC, HINSTANCE, int); char szClassName[] = "owndr01";       HWND hMyButton;
    LPCTSTR buttoncaption="owner button";
    int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst,
                       LPSTR lpsCmdLine, int nCmdShow)
    {
        MSG msg;
        
        if (!hPrevInst) {
            if (!InitApp(hCurInst))
                return FALSE;
        }
        if (!InitInstance(hCurInst, nCmdShow)) {
            return FALSE;
        }
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    BOOL InitApp(HINSTANCE hInst)
    {
        WNDCLASS wc;
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;    
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInst;        
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName = NULL;    
        wc.lpszClassName = (LPCSTR)szClassName;
        return (RegisterClass(&wc));
    }
    BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
    {
        HWND hWnd;    hWnd = CreateWindow(szClassName,
                "owner draw button",
                WS_OVERLAPPEDWINDOW,    
                CW_USEDEFAULT,    //&pound;&Oslash;×ù&#732;&Euml;
                CW_USEDEFAULT,    //&pound;&Ugrave;×ù&#732;&Euml;
                CW_USEDEFAULT,    
                CW_USEDEFAULT,    
                NULL,           
                NULL,           
                hInst,           
                NULL);
        if (!hWnd)
            return FALSE;
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
        return TRUE;
    }
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
    {
        int id;
        HDC hdc;
        static int sw = 1; 
        switch (msg) {
            case WM_CREATE: 
                hMyButton = CreateWindow("BUTTON",
                    buttoncaption,  
                    WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
                    30, 30, //&Icirc;&raquo;&Ouml;&Atilde;
                    100, 100, 
                    hWnd, 
                    (HMENU)ID_MYOWN, 
                    (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), 
                    NULL);
                break;
            case WM_DRAWITEM: 
    {
    //draw bmp here
    LPDRAWITEMSTRUCT pdis=(LPDRAWITEMSTRUCT)lp;
    hdc = pdis->hDC;
    HBRUSH hbr=CreateSolidBrush(RGB(0,244,22));
    HBRUSH hbold=(HBRUSH)SelectObject(hdc,hbr);
    Rectangle(hdc,pdis->rcItem.left,pdis->rcItem.top,pdis->rcItem.right,pdis->rcItem.bottom);
    SelectObject(hdc,hbold);
    DeleteObject(hbr);
    SetTextColor(hdc,RGB(255,0,0));
    SetBkMode(hdc,TRANSPARENT);
    TextOut(hdc,pdis->rcItem.left,pdis->rcItem.top,buttoncaption,strlen(buttoncaption));
    }
                break;
            case WM_COMMAND:
                switch (LOWORD(wp)) {
                    case ID_MYOWN:
                        if (sw == 1) {
                            sw = 0;
                            return 0L;
                        }
                        if (sw == 0) {
                            sw = 1;
                            return 0L;
                        }
                        break;
                    default:
                        return (DefWindowProc(hWnd, msg, wp, lp));
                }
                break;
            case WM_CLOSE:
                id = MessageBox(hWnd,
                    (LPCSTR)"end application",
                    (LPCSTR)"confirm",
                    MB_YESNO | MB_ICONQUESTION);
                if (id == IDYES) {
                    DestroyWindow(hWnd);
                }
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            default:
                return (DefWindowProc(hWnd, msg, wp, lp));
        }
        return 0L;
    }