Well, I finally found a win32 api way to rotate text, but it's scrambly-looking

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello, sorry to bother you out there. This is a rather long post. The following post is some Windows code I found from MSDN and used from code::blocks but pasted and compiled on visual studio just to see if it would work better. But it doesnt There are
two problems.
1)The beginning lines of each string overlap each other
2) When text is rendered diagonally, the characters baselines dont align perfectly, and sort of looked like they were jiggled back and forth.
If you could help me with the code, that would be great. I know this sounds silly, but this is very important to me. I have been working on and off with Windows programming for months and seem to be running into a brick wall with each line of code I enter,
:P


<div style="color:Black;background-color:White; <pre>
#include <Windows.h>
#include <string>
<span style="color:Blue; using std::wstring;

<span style="color:Green; /* Declare Windows procedure */
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

<span style="color:Green; /* Make the class name into a global variable */
TCHAR szClassName[] = L<span style="color:#A31515; "CodeBlocksWindowsApp";

<span style="color:Blue; int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
<span style="color:Blue; int nCmdShow)
{
HWND hwnd; <span style="color:Green; /* This is the handle for our window */
MSG messages; <span style="color:Green; /* Here messages to the application are saved */
WNDCLASSEX wincl; <span style="color:Green; /* Data structure for the windowclass */

<span style="color:Green; /* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WndProc; <span style="color:Green; /* This function is called by windows */
wincl.style = CS_DBLCLKS; <span style="color:Green; /* Catch double-clicks */
wincl.cbSize = <span style="color:Blue; sizeof (WNDCLASSEX);

<span style="color:Green; /* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; <span style="color:Green; /* No menu */
wincl.cbClsExtra = 0; <span style="color:Green; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; <span style="color:Green; /* structure or the window instance */
<span style="color:Green; /* Use Windowss default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);

<span style="color:Green; /* Register the window class, and if it fails quit the program */
<span style="color:Blue; if (!RegisterClassEx (&wincl))
<span style="color:Blue; return 0;

<span style="color:Green; /* The class is registered, lets create the program*/
hwnd = CreateWindowEx (
0, <span style="color:Green; /* Extended possibilites for variation */
szClassName, <span style="color:Green; /* Classname */
L<span style="color:#A31515; "Code::Blocks Template Windows App", <span style="color:Green; /* Title Text */
WS_OVERLAPPEDWINDOW, <span style="color:Green; /* default window */
CW_USEDEFAULT, <span style="color:Green; /* Windows decides the position */
CW_USEDEFAULT, <span style="color:Green; /* where the window ends up on the screen */
544, <span style="color:Green; /* The programs width */
375, <span style="color:Green; /* and height in pixels */
HWND_DESKTOP, <span style="color:Green; /* The window is a child-window to desktop */
NULL, <span style="color:Green; /* No menu */
hThisInstance, <span style="color:Green; /* Program Instance handler */
NULL <span style="color:Green; /* No Window Creation data */
);

<span style="color:Green; /* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);

<span style="color:Green; /* Run the message loop. It will run until GetMessage() returns 0 */
<span style="color:Blue; while (GetMessage (&messages, NULL, 0, 0))
{
<span style="color:Green; /* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
<span style="color:Green; /* Send message to WindowProcedure */
DispatchMessage(&messages);
}

<span style="color:Green; /* The program return-value is 0 - The value that PostQuitMessage() gave */
<span style="color:Blue; return messages.wParam;
}


<span style="color:Green; /* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

<span style="color:Blue; switch (message)
{

<span style="color:Blue; case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
RECT rc;
<span style="color:Blue; int angle;
HGDIOBJ hfnt, hfntPrev;
WCHAR lpszRotate[50] = TEXT(<span style="color:#A31515; "String to be rotated.");
HRESULT hr;
size_t pcch = 50;

<span style="color:Green; // Allocate memory for a LOGFONT structure.

PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, <span style="color:Blue; sizeof(LOGFONT));


<span style="color:Green; // Specify a font typeface name and weight.
WCHAR lfFaceName[20] = L<span style="color:#A31515; "Arial";
lstrcpy(plf->lfFaceName, lfFaceName);

<span style="color:Green; //if (FAILED(hr))
{
<span style="color:Green; // TODO: write error handler
}

plf->lfWeight = FW_NORMAL;

<span style="color:Green; // Retrieve the client-rectangle dimensions.

GetClientRect(hWnd, &rc);

<span style="color:Green; // Set the background mode to transparent for the
<span style="color:Green; // text-output operation.

SetBkMode(hdc, TRANSPARENT);

<span style="color:Green; // Draw the string 36 times, rotating 10 degrees
<span style="color:Green; // counter-clockwise each time.

<span style="color:Blue; for (angle = 0; angle < 3600; angle += 400)
{
plf->lfEscapement = angle;
plf->lfOrientation = angle;
SetTextAlign(hdc, TA_BASELINE);
hfnt = CreateFontIndirect(plf);
hfntPrev = SelectObject(hdc, hfnt);


TextOut(hdc, rc.right / 2, rc.bottom / 2,
lpszRotate, pcch);
SelectObject(hdc, hfntPrev);
DeleteObject(hfnt);
}

<span style="color:Green; // Reset the background mode to its default.

SetBkMode(hdc, OPAQUE);

<span style="color:Green; // Free the memory allocated for the LOGFONT structure.

LocalFree((LOCALHANDLE) plf);
EndPaint(hWnd, &ps);
<span style="color:Blue; break;
}
<span style="color:Blue; case WM_DESTROY:
PostQuitMessage(0);
<span style="color:Blue; break;
<span style="color:Blue; default:
<span style="color:Blue; return DefWindowProc(hWnd, message, wParam, lParam);
}
<span style="color:Blue; return 0;
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top