4
\$\begingroup\$

This portion is for a button style. I set the style depending on if it is pressed or not. I was wondering if I could write this better or more efficient. Mainly the way I use new.

bool Draw::CustomButton(LPDRAWITEMSTRUCT pDIS) {

using namespace Gdiplus;

// Control info
int ctlResID = pDIS->CtlID;            
Status lastCall = (Status)0;

// The bounding box of the button
Rect rect(0.0f,0.0f,pDIS->rcItem.right, pDIS->rcItem.bottom);

// Prepare the text
const int buffer_size_ = 16;
char textCharArray[buffer_size_];
GetWindowTextA(pDIS->hwndItem, textCharArray, buffer_size_);
std::string buttonText = std::string(textCharArray);

// Button state, down or up
bool button_down = false;
if (pDIS->itemState & ODS_SELECTED) button_down = true;


/* Pens, Brushes, Fonts, etc, depending on state *********/
// Center Align
StringFormat format;
format.SetLineAlignment(StringAlignmentCenter);
format.SetAlignment(StringAlignmentCenter);
format.SetFormatFlags(0);

/* This only works with pointers to removed redundent code and making it eaiser to update */
LinearGradientBrush* lgBrushBackgroundPtr = new LinearGradientBrush(  rect, Color(255, 0, 120, 165), Color(255, 0, 90, 190), LinearGradientModeVertical);  
int borderSize = 0;
static Pen* penBorderPtr = NULL;
static Font* fontPtr = NULL;
static SolidBrush* fontColorPtr = NULL;


// Button Down
if (button_down) { 
    borderSize = 4;
    penBorderPtr       = new Pen ( Color ( 125, 0, 0, 0 ), borderSize );
    fontPtr            = new Font ( L"Arial", 18, FontStyleRegular, UnitPixel );
    fontColorPtr       = new SolidBrush ( Color ( 255, 255, 255, 255 ) );
}
// Button Up
else {
    borderSize = 4;
    penBorderPtr       = new Pen ( Color ( 125, 20, 20, 20 ), borderSize );
    fontPtr            = new Font ( L"Arial", 20, FontStyleRegular, UnitPixel );
    fontColorPtr       = new SolidBrush ( Color ( 255, 255, 255, 255 ) );
}    

 // Draw
Graphics g (pDIS->hDC);
lastCall = g.FillRectangle(lgBrushBackgroundPtr, rect);          
lastCall = g.DrawRectangle(penBorderPtr, rect);


StringEx(&g, buttonText, fontPtr, rect, ALIGN_CENTER, ALIGN_CENTER, fontColorPtr);

// Clean
g.Flush();   
delete penBorderPtr;
delete fontPtr;
delete fontColorPtr;
delete lgBrushBackgroundPtr;

return true;

}

The main thing I am looking at is how I draw the button with new/ delete

\$\endgroup\$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.