オフィス街(青)

メモパッド ボタンの色替え
通常ウィンド上のボタンはシステムで決められた色でのみしか表示できません。(グレイ)ボタンを色替えするには、自分でそのボタンを描くしかない!。
[手順]
WindowsAPI関数を使用し、画面イメージを取りPrinterオブジェクトを使用し、印刷する。
@ボタンを作成する時にBS_OWNERDRAWを指定し作成する。ダイアログエディターで設定する場合は[オーナー描画]にチェックを付ける
AウィンドプロシージャにWM_DRAWITEMイベントが発生するのでそこにボタンを描くルーチンを入れる。

サンプルコード

HWND hSubWnd;
HWND hWndBtn;

/*------------------------------------------------------
    ウィンド関数:WndProc
--------------------------------------------------------*/
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,UINT wParam,LONG lParam)
{
    HDC                hdc;
    PAINTSTRUCT        ps;
    RECT            rect;
    POINT            pt;
    DRAWITEMSTRUCT *lpdis;
    int wid, hig;

    switch( message ) {
        case WM_CREATE:
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
            break;
        case WM_DRAWITEM:
            lpdis = (LPDRAWITEMSTRUCT)lParam;
            wid = lpdis->rcItem.right - lpdis->rcItem.left;
            hig = lpdis->rcItem.bottom - lpdis->rcItem.top;
            SetBkMode(lpdis->hDC,TRANSPARENT);
            if( lpdis->itemState & ODS_FOCUS) {
                if( lpdis->itemState & ODS_SELECTED) {
                    // pressed button
                    SelectObject(lpdis->hDC, GetStockObject(NULL_PEN));
                    SelectObject(lpdis->hDC, GetStockObject(BLACK_BRUSH));
                    Rectangle( lpdis->hDC, lpdis->rcItem.left , lpdis->rcItem.top,
                                           lpdis->rcItem.right, lpdis->rcItem.bottom );
                    SelectObject(lpdis->hDC, GetStockObject(GRAY_BRUSH));
                    Rectangle( lpdis->hDC, lpdis->rcItem.left +1, lpdis->rcItem.top+1,
                                           lpdis->rcItem.right-1, lpdis->rcItem.bottom-1 );
                    SelectObject(lpdis->hDC, GetStockObject(LTGRAY_BRUSH) );
                    Rectangle( lpdis->hDC, lpdis->rcItem.left +2, lpdis->rcItem.top+2,
                                       lpdis->rcItem.right-2, lpdis->rcItem.bottom-2 );
                    SetTextAlign( lpdis->hDC, TA_CENTER );
                    TextOut(lpdis->hDC, wid/2+1,hig/8*3+1,"label",5);
                }
                else {
                    // no press button
                    SelectObject(lpdis->hDC, GetStockObject(NULL_PEN));
                    SelectObject(lpdis->hDC, GetStockObject(BLACK_BRUSH));
                    Rectangle( lpdis->hDC, lpdis->rcItem.left , lpdis->rcItem.top,
                                           lpdis->rcItem.right, lpdis->rcItem.bottom );
                    SelectObject(lpdis->hDC, GetStockObject(WHITE_BRUSH));
                    Rectangle( lpdis->hDC, lpdis->rcItem.left +1, lpdis->rcItem.top+1,
                                           lpdis->rcItem.right-2, lpdis->rcItem.bottom-2 );
                    SelectObject(lpdis->hDC, GetStockObject(GRAY_BRUSH));
                    Rectangle( lpdis->hDC, lpdis->rcItem.left +2, lpdis->rcItem.top+2,
                                           lpdis->rcItem.right-2, lpdis->rcItem.bottom-2 );
                    SelectObject(lpdis->hDC, GetStockObject(LTGRAY_BRUSH) );
                    Rectangle( lpdis->hDC, lpdis->rcItem.left +2, lpdis->rcItem.top+2,
                                       lpdis->rcItem.right-3, lpdis->rcItem.bottom-3 );
                    SetTextAlign( lpdis->hDC, TA_CENTER );
                    TextOut(lpdis->hDC, wid/2,hig/8*3,"label",5);
                }
            }
            else {
                // normal button
                SelectObject(lpdis->hDC, GetStockObject(NULL_PEN));
                SelectObject(lpdis->hDC, GetStockObject(BLACK_BRUSH));
                Rectangle( lpdis->hDC, lpdis->rcItem.left , lpdis->rcItem.top,
                                       lpdis->rcItem.right, lpdis->rcItem.bottom );
                SelectObject(lpdis->hDC, GetStockObject(WHITE_BRUSH));
                Rectangle( lpdis->hDC, lpdis->rcItem.left   , lpdis->rcItem.top,
                                       lpdis->rcItem.right-1, lpdis->rcItem.bottom-1 );
                SelectObject(lpdis->hDC, GetStockObject(GRAY_BRUSH));
                Rectangle( lpdis->hDC, lpdis->rcItem.left +1, lpdis->rcItem.top+1,
                                       lpdis->rcItem.right-1, lpdis->rcItem.bottom-1 );
                SelectObject(lpdis->hDC, GetStockObject(LTGRAY_BRUSH) );
                Rectangle( lpdis->hDC, lpdis->rcItem.left +1, lpdis->rcItem.top+1,
                                       lpdis->rcItem.right-2, lpdis->rcItem.bottom-2 );
                SetTextAlign( lpdis->hDC, TA_CENTER );
                TextOut(lpdis->hDC, wid/2,hig/8*3,"label",5);
            }
            break;
        case WM_DESTROY:
            return 0;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

void mkMainWnd( HINSTANCE hInstance, HWND hWnd )
{
    static char szAppName[] = "SubWnd";
    WNDCLASS    wndclass;
    int i, j;
    char    tmp[16];

    memset( &wndclass, 0, sizeof(WNDCLASS) );
    wndclass.style            = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.hInstance        = hInstance;
    wndclass.hIcon            = LoadIcon(hInstance, IDI_APPLICATION);
    wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground    = GetStockObject(BLACK_BRUSH);
    wndclass.lpszClassName    = szAppName;
    RegisterClass(&wndclass);

    //    サブウィンドウの作成
    hSubWnd = CreateWindow(szAppName, szAppName,
                        WS_POPUP|WS_VISIBLE,
                        0, 0,MainStr.width, TOOLBAR_HEIGHT,
                        hWnd, NULL, hInstance, NULL);

    for( i=0; i<2; i++ ) {
        for( j=0; j<2; j++ ) {
            sprintf( tmp, "%d", i*2+j+1);
            hWndBtn= CreateWindow( "BUTTON", tmp,
                                WS_CHILD|BS_OWNERDRAW|WS_VISIBLE,
                                0, 0, 80, 30,
                                hSubWnd, NULL, hInstance, NULL);
        }
    }
}


メール アイコン
メール
トップ アイコン
トップ


オフィス街(青)