GetTextExtent vs. DrawText (with DT_CALCRECT)

Working on an MFC app that has just been converted to Unicode (finally!), I noticed that one button (which is created dynamically) is too small to fit the text in Korean (and Russian and a few other languages).
The code was calling something like:

CSize sz = m_btAdjustColors.GetDC()->GetTextExtent(sCaption);

It seems correct, but these script languages are throwing it [...]

Popularity: 2% [?]

Serial Port Code Library

Need to interact with some hardware via serial port? I’m having to do some serial port communications in a project at work, and it is officially Not Fun. There was some original code that was doing only reads from the port, and I needed to some significant functionality that would not be possible with the [...]

Popularity: 2% [?]

UnitTest++

I’ve got a great process using NUnit in all my .Net projects. However, at work we have a large MFC/C++ application that has NO automated testing. I’ve been planing on getting unit testing into it for quite a while, but finally decided this is the week to do it.
It turns out that C++ unit testing [...]

Popularity: 2% [?]

Setting the minimum Windows version supported (C++)

Recently, I was trying to use the Shell function SHGetFolderPath. Despite including the correct header file (shlobj.h), the compiler wasn’t recognizing it. My code looked like this:

CString strPath;
HRESULT hResult = SHGetFolderPath(
NULL,
CSIDL_APPDATA,
[...]

Popularity: 1% [?]

Don’t use CArchive with native C++ type bool

I recently ran into an issue where I was trying to serialize a bool variable from a CArchive object.
Archiving it out, with this code, works fine:
//CArchive ar;bool bFill;ar << bFill;

But this code:
ar >> bFill

has problems. It compiles fine under Visual Studio 2003, but errors out under Visual C++ 6 with this error:
C2679: binary ‘>>’ : no operator defined [...]

Popularity: 1% [?]

Getting the real view under a CPreviewView (MFC)

I had an interesting problem at work the other day. In this MFC application, there are a number of views that can be printed and we support the Print Preview function.
However, in one of these views we rely on getting the view from the frame window in order to handle command updates. This is accomplished [...]

Popularity: 1% [?]

Unsupported Frameworks…

Programming with a framework that you’ve developed can be annoying, when you compare it to the ease of IDE-supported frameworks. MFC is a nice framework primarily because Visual C++ has so much built-in support for it.
My little framework has no such support (and I have no ambitions to build in IDE support for it) and [...]

Popularity: 1% [?]

What’s Wrong with this code? - 2 - Answer

In the last post, I showed some code that had a very simple problem.
The problem is that when you call HIWORD, it converts the 16 bits into an unsigned short, which then gets passed as an int padded with zeros–not sign extended (it’s not signed). It will NEVER be less than zero. The solution is [...]

Popularity: 1% [?]

What’s Wrong with this code? - 2

What’s wrong with this code?
UINT a;
.
.
.
DoSomething(HIWORD(a));
void DoSomething(int x)
{
 if (x < 0)
 {
  //do something
 }
 
}
I recently ran into this in some code I was working on.

Popularity: 1% [?]
Tags: Code, mfc, programming, quiz, windows

Popularity: 1% [?]

Macros are evil

I’m innocently developing a device context class for my LFC framework and I want a method called SelectPen. All of a sudden I’m getting very weird linker errors about how SelectPen is not defined.
It turns out that SelectPen is a macro defined in windowsx.h as an alias for SelectObject.
#define SelectPen(hdc, hpen) ((HPEN)SelectObject((hdc), (HGDIOBJ)(HPEN)(hpen))) Very annoying.Very [...]

Popularity: 1% [?]