Solved

# Twips to pixels...

Posted on 1998-04-23
515 Views
I'm using an ActiveX Control (MSFlexGrid) which returns a location in twips.  Unfortunately, I need to convert this to for graphical purposes.  I've had the hardest time doing what should be really simple. Can someone help me out?
0
Question by:MatthewHertz

LVL 8

Expert Comment

Twip is screen-independent unit used to ensure that placement and proportion of screen elements in your screen application are the same on all display systems. A twip is a unit of screen measurement equal to 1/20 of a printer's point. There are approximately 1440 twips to a logical inch or 567 twips to a logical centimeter (the length of a screen item measuring one inch or one centimeter when printed).

0

LVL 10

Expert Comment

Get a dc (CDC) for your window.

Then do this..

int logpixelsx = dc.GetDeviceCaps(LOGPIXELSX);
int pixelx = MulDiv(x,pogpixels,1440)

This gives the answer in pixels.

You can use DPtoLP to convert to logical units if you are using something other than pixels.

0

LVL 10

Expert Comment

I assume you want to know how to convert, rather than what a twip is.  If so, let me answer with the above.

BTW: pogpixels should have been logpixelsx (darn my chubby fingers).

Also do the same for the y direction.

0

Author Comment

Thanks, I know what twips are, I need to know how to convert them...
0

LVL 1

Accepted Solution

This is the 2nd time I am posting an answer to MSFlexGrid problem. The measurements given by gelbert are perfectly right. I wrote a generalized conversion routine to use for MSFlexGrid. Unfortunately, it doesn't work very fine with MSFlexGrid. There is something that is fishy about MSFlexGrid.

So, I wrote my own conversion routine which works fine with MSFlexGrid. My requirement is to embed an edit control as the user clicks on a cell in the Grid at run time. I get it perfect. Here's the source code for this purpose. If you want to know the location, please let me know of your exact requirement and a sample code of what you have done. If your requirement is similar to what I have done already, you are free to use the following piece of code

// Embed editable CEdit Control

// Construct my own overridden edit control
pEdit = new CStdEqpEdit(&m_StdGrid, row);

Hope this helps .....
/*
* Conversion from Twips to Pixel scale. Divisor values are specific to this
* grid and may not work for others. Generic conversion routine couldn't be
* written for this purpose. Make sure not to change this
*/

long lLeft = (long) (m_StdGrid.GetCellLeft () / 15);
long lTop = (long) (m_StdGrid.GetCellTop () / 15);
long lHeight = (long) (m_StdGrid.GetCellHeight() / 15);
long lWidth = (long) (m_StdGrid.GetCellWidth() / 14.3);

CRect rect (lLeft, lTop, lLeft + lWidth, lTop + lHeight);

// Create the edit control on the fly
pEdit->Create (WS_CHILD | WS_VISIBLE | WS_BORDER, rect, this, IDC_SEQ_NUM_EDIT);

pEdit->SetWindowText (m_StdGrid.GetText());

// Change the font of the edit control to that of the grid cell for consistency
CString sFontName = m_StdGrid.GetCellFontName();
int iFontSize = (int) (m_StdGrid.GetCellFontSize() * FONT_SCALE);
CFont *pFontEdit = new CFont();
pFontEdit->CreatePointFont (iFontSize, sFontName);
pEdit->SetFont (pFontEdit);

// Set the focus to the edit control
pEdit->SetFocus();
0

LVL 10

Expert Comment

I told you how to convert twips to pixels .. obviously that's NOT what you wanted.

0

Author Comment

Actually, RONSLOW that was what I wanted.  Your solution worked fine.  I couldn't figure out how to give you (and not gelbert, to whom the comment was directed) the points though.  Thanks a bunch!
0

## Featured Post

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…