Skip to main content
GameDev.net gamedev.net
🔒 Locked

TextOut() Problems

Started by Jimi Apr 23, 2004 at 2:08 PM 5 replies 650+ views
Original Post
Jimi
Jimi
I''ve just started with win32 programming. so far I''ve got a little program with a menu, that when you click on one of the items on a popup menu, it comes up with a messagebox displaying the current time. But I''ve run into a problem. I managed to get it to display "Hello World" using TextOut(), however for some reaon, if you move the cursor over the text, the cursor dissapears until it is no longer on the text, if you click on a menu item, the little popup menu''s don''t appear until you move the mouse over where they should be, and the message box doesn''t appear at all.
Crouching Hippo - Hidden Suprise
Colin Jeanne
Colin Jeanne
Can you post your program''s code?


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
Jimi
Jimi
The code is below. Please note that there is a resource file with the menu in, and the DisplayTime function writes the time to a file first, because it wouldn't 'convert int to const char' (or whatever), and I've only just made the discovery of the sprintf() funtion.


#include <windows.h>
#include <iostream>
#include <time.h>
#include <fstream>
#include "resource.h"
using namespace std;

const char g_szClassName[] = "myWindowClass";

void DisplayTime(HWND hwnd)
{
time_t Seconds; //Calender Time

struct tm *tTime; //Pointer to broken down time struct

char szTime[5]; //Final output

ofstream fout; //Variable for writing to file

ifstream fin; //Variable for reading from file


Seconds = time(NULL); //Get calender time

tTime = localtime(&Seconds); //Convert to broken down time


//Write Time to File

fout.open("Time.tmp");
if(tTime->tm_hour < 10)
fout << "0";
fout << tTime->tm_hour << ":";
if(tTime->tm_min < 10)
fout << "0";
fout << tTime->tm_min;
fout.close();

//Read Time from File

fin.open("Time.tmp");
fin >> szTime;
fin.close();

//Display Time

MessageBox(hwnd, szTime, "Time",MB_OK);
}

void DisplayText(int x_pos,int y_pos, char *text, HWND hwnd)
{
if(!text)
return;

HDC hdc = GetDC(hwnd);
if(!hdc)
return;

TextOut(hdc,x_pos,y_pos,text,strlen(text));

ReleaseDC(hwnd,hdc);
}

//WINDOW PROCEDURE

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
DisplayText(100,100,"Hello World",hwnd);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case ID_VEIW_TIME:
DisplayTime(hwnd);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//REGISTERING THE WINDOW CLASS

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON), IMAGE_ICON, 16, 16, 0);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

//CREATING THE WINDOW

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Time",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

//MESSAGE LOOP

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}



[edited by - Jimi on April 24, 2004 10:37:03 AM]

[edited by - Jimi on April 24, 2004 10:45:40 AM]
Crouching Hippo - Hidden Suprise
Aprosenf
Aprosenf
You need to use BeginPaint() and EndPaint() during the WM_PAINT message. Change your DisplayText() function to the following:
void DisplayText(int x_pos, int y_pos, char *text, HWND hwnd)
{
if(!text)
return;

PAINTSTRUCT ps; // Dummy structure, we can ignore what gets put in this


HDC hdc = BeginPaint(hwnd, &ps);

if(!hdc)
return;

TextOut(hdc, x_pos, y_pos, text, strlen(text));

EndPaint(hwnd, &ps);
}


[edited by - aprosenf on April 24, 2004 11:05:00 AM]
Jimi
Jimi
thanks!
but what actually is PAINTSTRUCT

[edited by - Jimi on April 24, 2004 12:12:15 PM]
Crouching Hippo - Hidden Suprise
nagromo
nagromo
It is a structure that Windows uses to keep track of stuff. You are not supposed to know what is in it. Only Windows should access it. That''s how lots of stuff in Windows works.
Colin Jeanne
Colin Jeanne
Actually, only some of the members of PAINTSTRUCT are solely for internal use. There are three that are useful:

hdc - specifies the HDC used for drawing (although this is returned by BeginPaint())

fErase - specifies whether or not the background must be erased

rcPaint - the rect that needs to be painted.


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.