WM_PAINT message gets sent on a lot of different occasions so having a create within that message would be bad. the textout function takes textout(<window handle>, <X location to draw>, <Y location to draw>, <String to be drawn>, <number of characters in string>). That code up above is all you need to center text on a given window. The only thing that needs to change is the handle to the window. The problem you are running into is the dynamic window creation in win32. After you create the dynamic window you will have a handle to that window.
Dynamic GUI
Collapse
X
-
I wanted to try out the textout() function, so I created the following program:Originally posted by StudlyamiWM_PAINT message gets sent on a lot of different occasions so having a create within that message would be bad. the textout function takes textout(<window handle>, <X location to draw>, <Y location to draw>, <String to be drawn>, <number of characters in string>). That code up above is all you need to center text on a given window. The only thing that needs to change is the handle to the window. The problem you are running into is the dynamic window creation in win32. After you create the dynamic window you will have a handle to that window.
[code=cpp]#include <windows.h>
#include <string>
#include <vector>
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp ";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClass Name = szClassName;
wincl.lpfnWndPr oc = WindowProcedure ; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* 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.lpszMenuN ame = NULL; /* No menu */
wincl.cbClsExtr a = 0; /* No extra bytes after the window class */
wincl.cbWndExtr a = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackgr ound = (HBRUSH) COLOR_BACKGROUN D;
/* Register the window class, and if it fails quit the program */
if (!RegisterClass Ex (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWI NDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessag e(&messages);
/* Send message to WindowProcedure */
DispatchMessage (&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage () gave */
return messages.wParam ;
}
/* This function is called by the Windows function DispatchMessage () */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_PAINT:
PAINTSTRUCT paintStruct;
HDC hdc;
hdc = GetDC (hwnd);
BeginPaint (hwnd, &paintStruct );
textout(hdc, 500, 500, "yo", 2);
EndPaint(hwnd, &paintStruct );
ReleaseDC (hwnd, hdc);
return 0;
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}[/code]
When I try to compile it, I get the following error:
87 C:\Documents and Settings\DBE\De sktop\main.cpp `textout' undeclared (first use this function)
That is what I was asking about in my last post. The dynamic text thing is a different matter, but still needs attending to.Comment
-
That fixes that error.Originally posted by StudlyamiYou textout needs to be TextOut(...)
capital T capital O
Now I get tge error message:
[Linker error] undefined reference to `TextOutA@20'
ld returned 1 exit statusComment
-
Did you try creating a new project and pasting the code in or was this a code file downloaded from somewhere? If it was a code file downloaded from somewhere try creating a new win32 project and copy that code and paste it in the new project. I copied your code made the change and it works fine. I'm using Visual studios 2003 to build it.Comment
-
Neat, it works.Originally posted by StudlyamiDid you try creating a new project and pasting the code in or was this a code file downloaded from somewhere? If it was a code file downloaded from somewhere try creating a new win32 project and copy that code and paste it in the new project. I copied your code made the change and it works fine. I'm using Visual studios 2003 to build it.
Well, now all I need is how to get it to be dynamic whenever I pass an intiger into the function that creates the window, and then it displays that.Comment
-
I would just like to remind everyone involved with this post that the issue has not been resolved.
It seems like everyone has forgotten since the last post was 29 days agoComment
-
The code sample doesn't compile.Originally posted by compman9902I would just like to remind everyone involved with this post that the issue has not been resolved.
It seems like everyone has forgotten since the last post was 29 days ago
There are various errors. Mainly attempting to use a char array as an LPWSTR. For that I added the include of tchar.h and used the standard TCHAR mappings you are supposed to use in Windows code.
All I did was take you code, create a Win32 Project in Visual Studio.NET 2005 and fix errors. I did rework the WM_PAINT message handling.
The code below works and displays the yo in the center of the window. Have a look and you will see where my changes are.
I had no idea that the code was never compilng.
[code=c]
#include <windows.h>
#include <string>
#include <vector>
using namespace std;
#include <tchar.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = TEXT("WindowsAp p");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClass Name = szClassName;
wincl.lpfnWndPr oc = WindowProcedure ; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* 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.lpszMenuN ame = NULL; /* No menu */
wincl.cbClsExtr a = 0; /* No extra bytes after the window class */
wincl.cbWndExtr a = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackgr ound = (HBRUSH) COLOR_BACKGROUN D;
/* Register the window class, and if it fails quit the program */
if (!RegisterClass Ex (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
TEXT("Windows App"), /* Title Text */
WS_OVERLAPPEDWI NDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
CW_USEDEFAULT, /* The programs width */
CW_USEDEFAULT, /* and height in pixels */
//544, /* The programs width */
//375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessag e(&messages);
/* Send message to WindowProcedure */
DispatchMessage (&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage () gave */
return messages.wParam ;
}
/* This function is called by the Windows function DispatchMessage () */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message) /* handle the messages */
{
case WM_PAINT:
hdc = BeginPaint(hwnd , &ps);
GetClientRect(h wnd, &rect);
DrawText(hdc, TEXT("yo"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
return 0;
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
[/code]Comment
Comment