NO

Author Topic: DLU to pixel  (Read 4483 times)

czerny

  • Guest
DLU to pixel
« on: August 08, 2014, 02:52:35 PM »
Hallo!

I have to calculate the hight of a custom control in pixels. I have tried to do this with the following code:

Code: [Select]
void DluToPixel(HWND hwnd)
{
UINT h;
RECT rect={0,0,4,8};
MapDialogRect(hwnd, &rect);
h = (3*rect.bottom)/8;
//h = rect.bottom;
GetClientRect(hwnd, &rect);

char s[200] = "";
sprintf(s,"%d %d",rect.bottom, h);
MessageBox(0, s, "info", MB_OK);
}
If the height in DLU is 3, this function outputs '5 3' so it seems as if MapDialogRect() leaves rect as it was.

I have two ideas why this could be the case:

- The control has no font asigned to.
- the hwnd is not a dialog handle.

What could be a solution?

Sam Steele

  • Guest
Re: DLU to pixel
« Reply #1 on: August 08, 2014, 03:53:30 PM »
This is what I use:

Code: [Select]
// GetDialogBaseUnits only works for the system font.
// MapDialogRect only works one way.

// ssDuToPixelsX by Sam Steele. 2013-08-18.
// Returns the number of pixels corresponding to the number of
//   horizontal dialog units for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssDuToPixelsX(HWND Dialog, int DuX){

    int PixelsX = 0;

    if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){

        // If we go to 40 du then rounding in MapDialogRect is eliminated.
        RECT r = {40, 0, 0, 0};

        MapDialogRect(Dialog, &r);
        // Add half of the divisor to the dividend for rounding to nearest.
        PixelsX = (DuX * r.left + 20) / 40;

    }

    return PixelsX;

} // ssDuToPixelsX





// ssDuToPixelsY by Sam Steele. 2013-08-18.
// Returns the number of pixels corresponding to the number of
//   vertical dialog units for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssDuToPixelsY(HWND Dialog, int DuY){

    int PixelsY = 0;

    if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){

        // If we go to 80 du then rounding in MapDialogRect is eliminated.
        RECT r = {0, 80, 0, 0};

        MapDialogRect(Dialog, &r);
        // Add half of the divisor to the dividend for rounding to nearest.
        PixelsY = (DuY * r.top + 40) / 80;

    }

    return PixelsY;

} // ssDuToPixelsY





// ssPixelsToDuX by Sam Steele. 2013-08-18.
// Returns the number of horizontal dialog units corresponding
//   to the number of pixels for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssPixelsToDuX(HWND Dialog, int PixelsX){

    int DuX = 0;

    if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){

        // If we go to 20 or 40 du then rounding in MapDialogRect is eliminated.
        RECT r = {20, 0, 40, 0};

        MapDialogRect(Dialog, &r);
        // Add half of the divisor to the dividend for rounding to nearest.
        DuX = (PixelsX * 40 + r.left) / r.right;

    }

    return DuX;

} // ssPixelsToDuX





// ssPixelsToDuY by Sam Steele. 2013-08-18.
// Returns the number of vertical dialog units corresponding
//   to the number of pixels for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssPixelsToDuY(HWND Dialog, int PixelsY){

    int DuY = 0;

    if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){

        // If we go to 40 or 80 du then rounding in MapDialogRect is eliminated.
        RECT r = {0, 40, 0, 80};

        MapDialogRect(Dialog, &r);
        // Add half of the divisor to the dividend for rounding to nearest.
        DuY = (PixelsY * 80 + r.top) / r.bottom;

    }

    return DuY;

} // ssPixelsToDuY

czerny

  • Guest
Re: DLU to pixel
« Reply #2 on: August 09, 2014, 09:46:53 AM »
Thank you Sam! That is essentially the same what I did. My error was, that hwnd is not a dialog handle.