Setting the Color Table

Your application sets the color table of VGAPAL.DRV by using the SETCOLORTABLE escape code and the Escape function. The following fragrment shows the structure of the Escape function for VGAPAL.DRV:

Escape(hdc, SETCOLORTABLE, sizeof(COLORTABLE), lpInData, lpOutData)

If you specify NULL for lpInData, VGAPAL.DRV uses the default colors. If you want to change colors, set lpInData to point to a data structure defining your color table. This structure, which is defined by your application, has the following fields:

        struct {
                 BYTE         ctStart;                 //Starting palette index to set
                 BYTE         ctLen;                        //Number of entries to set
                COLORREF ctColors[];        //Color values
        } ct;

The ctStart field specifies the first color table index that will be set. The ctLen field specifies the number of entries that will be set. Set this value to the number of colors in the array used for the ctColors field. If ctLEN is set to zero, it will be treated as if it were set to one. The ctColors field specifies the colors that will be used. For changing single index entries, your application can use the COLORTABLE_STRUCT data structure.

The lpOutData variable points to a COLORREF structure. VGAPAL.DRV returns the color set corresponding to the first color in the array specified by lpInData. If you the values of the current color table, use the GETCOLORTABLE escape to interrogate VGAPAL.DRV. If you do not want return data, specify NULL for lpOutData.

The following example uses SETCOLORTABLE to set the color table:

BOOL SetDevicePalette(HDC hdc,int iStart,int iLen,LPPALETTEENTRY ppe)
{         int i;
        struct {
                BYTE         ctStart;
                BYTE         ctLen;
                COLORREF ctColors[16];
        } ct;
        i = SETCOLORTABLE; //Check if SETCOLORTABLE is supported.
        if (!Escape (hdc, QUERYESCSUPPORT, sizeof(i), (LPVOID)&i, NULL))
                 return FALSE;


        if (ppe)
         {        if (iLen  16) //Limit colors to 16.
                         iLen = 16;
                ct.ctStart = iStart; //Set colors.
                 ct.ctLen   = iLen;
                for (i=0; i ; i++)
                         ct.ctColors[i] = RGB(ppe[i].peRed, ppe[i].peGreen,p pe[i].peBlue);
                 Escape (hdc,SETCOLORTABLE, sizeof(ct), (LPVOID)&ct,NULL); //Set palette
         } else
         {        Escape (hdc, SETCOLORTABLE, 0, NULL, NULL); //Restore palette
         }
        return TRUE;
}

Setting the Color Table for Other Applications

Your application should switch to the custom color table whenever it becomes active. Conversely, your application should restore the previous colors when another application becomes active. Your application's window processor receives the WM_ACTIVATEAPP message when either of these events occur and can use this message to switch modes.

Your application can use the following example to switch color tables when it receives the WM_ACTIVATEAPP message. The argument for this example is the wParam value of WM_ACTIVATEAPP. (A nonzero value in wParam for the message indicates activation of the window.)

#define NumSysColors 19
//Define system palette
int SysPalIndex[NumSysColors] = {
                COLOR_ACTIVEBORDER,
.
.
.
                COLOR_WINDOWTEXT
};

//Define monochrome palette
#define rgbBlack RGB(0,0,0)
#define rgbWhite RGB(255,255,255)
COLORREF MonoSysColors[NumSysColors] = {
                rgbBlack,                  // ACTIVEBORDER
.
.
.
                rgbBlack                   // WINDOWTEXT
};
COLORREF OldSysColors[NumSysColors];

void VGAPalActivate(BOOL f)
        {HDC hdc;
        int i;
        static  BOOL fPalActive = False;
        hdc = GetDC(NULL);
        if (f && !fPalActive)
                {if (fVgaPal) SetDevicePalette(hdc, 0, 16, AppPalette);
                for (i=0; i<NumSysColors; i++) //Save UI colors and map to black/white
                        OldSysColors[i] = GetSysColor(SysPalIndex[i]);
                SetSysColors (NumSysColors, SysPalIndex, MonoSysColors);
                }
        if (!f && fPalActive)
                {if (fVgaPal)
                        SetDevicePalette(hdc, 0, 16, NULL);
                SetSysColors (NumSysColors, SysPalIndex, OldSysColors); //Restore UI colors.
                }
        fPalActive = f;
        UnrealizeObject(hpalApp);
        ReleaseDC(NULL,hdc);
        }