|
Have a look at QueryPerformanceCounter and QueryPerformanceFrequency. It however displays the amount of "real" time used, not only CPU time for that thread. For the latter you can try GetThreadTimes.
|
|
|
|
|
I highly recommend Programming Applications for Microsoft Windows Fourth Edition by Jeffrey Richter. He demonstrates powerful Win32 API tools including the use of GetThreadTimes().
Kuphryn
|
|
|
|
|
Hi,
In a follow up to my post below, there is going to be quite a bit of text displayed in the edit control. Insteaed of using multiple .Replacesel statement, can I store the text as a string resource and then display it using a single .ReplaceSel statement.
Also is the resource file the preffered method or this ?
Many thanks
Mike
Sunday coding with a huge hangover
|
|
|
|
|
You can use CString::LoadString to load the string from string resource and then use the loaded strind in ReplaceSel.
It is of course better to construct the text in memory and then call ReplaceSel once.
MJ32 wrote:
is the resource file the preffered method or this ?
If the text is static then yes, it's good for localization.
Pavel
Sonork 100.15206
|
|
|
|
|
Try the following:
CString str;
str.LoadString(<string resource ID>);
m_ctrEditLocation.ReplaceSel(str);
Gary R. Wheeler
|
|
|
|
|
Hi guys,
I am trying to output some information to a user in a dialog based app.
I would like to use an edit control (multiline) to dislay several lines (30-50).
Is an edit ctrl the best way to do this ?
Also when using the
m_ctrEditLocation.ReplaceSel(_T("LINE 1 \n\n\r"));everything appears on the same line.
Could anyone suggest any ideas on how to split text up on multiple lines / improvements to enable me to do this.
Many thanks
Mike
|
|
|
|
|
MJ32 wrote:
("LINE 1 \n\n\r"
You need to insert "\r\n" as line delimitter.
You can use this[^] modified edit control.
Pavel
Sonork 100.15206
|
|
|
|
|
Many thanks Pavel,
I can't beleieve i didn't think of just reversing the delimmitters I used
Call me Bit-Fik of the week
Thanks again
Mike
|
|
|
|
|
Dear All,
1) Am in real trouble i have a text file i need to open this file and convert the contents of it into another charecter set i need to read the contets into hexadecimal format. how can i do this.
2) how can i change the contents of a file from an IBM machine into windows format i.e i need to change the charecter set from IBM charecter set into arabic or ascii charecter set.
thank you very much
Best regards,
Mohammad Zakarni
|
|
|
|
|
ummmmm all file data is just numbers remember so open it in binary mode ... read it into memory (if its not too big else read it chunk at a time) ... use a lookup table to convert between ebdic (ibm stuff) and ascii (the rest of the world) ... you should be able to find a conversion table somewhere by searching for ebdic on google for example
"... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away" sonork: 100.18128 8028finder.com
|
|
|
|
|
Thank you very much for your help.
MZakarni
|
|
|
|
|
I've been injured my eyes trying to find the logical bug (since it is
not syntax or any other error that could VC report), so I'm asking you
if you could help me to find the bug.
Program is retyped from book and unfortunately and don't have the
original source code to compare it with.
Program should create main window and in it the other four child
windows and do some display in each of them except the main window.
But when I try to run the program, it only display the main window and
the client area, without child windows in it!
Just to say one more time, VC does not report any error or warning, so
it must be the logical bug in code.
Here's the source (I apologize for text length in mail):
#include <windows.h><br />
#include <math.h><br />
<br />
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);<br />
<br />
int cyChar;<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, <br />
PSTR szCmdLine, int iCmdShow)<br />
{<br />
static TCHAR szAppName[] = TEXT("Multi1");<br />
HWND hwnd;<br />
MSG msg;<br />
WNDCLASS wc;<br />
<br />
wc.style = CS_HREDRAW | CS_VREDRAW;<br />
wc.lpfnWndProc = WndProc;<br />
wc.cbClsExtra = 0;<br />
wc.cbWndExtra = 0;<br />
wc.hInstance = hInstance;<br />
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);<br />
wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br />
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
wc.lpszMenuName = NULL;<br />
wc.lpszClassName = szAppName;<br />
<br />
if (!RegisterClass(&wc))<br />
{<br />
MessageBox(NULL, TEXT("This Program Requires Windows NT!"),<br />
szAppName, MB_ICONERROR);<br />
return 0;<br />
}<br />
<br />
hwnd = CreateWindow(szAppName, TEXT("Multitasking Demo"),<br />
WS_OVERLAPPEDWINDOW,<br />
CW_USEDEFAULT, CW_USEDEFAULT,<br />
CW_USEDEFAULT, CW_USEDEFAULT,<br />
NULL, NULL, hInstance, NULL);<br />
<br />
ShowWindow(hwnd, iCmdShow);<br />
UpdateWindow(hwnd);<br />
<br />
while (GetMessage(&msg, NULL, 0, 0))<br />
{<br />
TranslateMessage(&msg);<br />
DispatchMessage(&msg);<br />
}<br />
return msg.wParam;<br />
}<br />
<br />
int CheckBottom(HWND hwnd, int cyClient, int iLine)<br />
{<br />
if (iLine * cyChar + cyChar > cyClient)<br />
{<br />
InvalidateRect(hwnd, NULL, TRUE);<br />
UpdateWindow(hwnd);<br />
iLine = 0;<br />
}<br />
return iLine;<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int iNum, iLine, cyClient;<br />
HDC hdc;<br />
TCHAR szBuffer[16];<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
if (iNum < 0)<br />
iNum = 0;<br />
<br />
iLine = CheckBottom(hwnd, cyClient, iLine);<br />
hdc = GetDC(hwnd);<br />
<br />
TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
wsprintf(szBuffer, TEXT("%d"), iNum++));<br />
<br />
ReleaseDC(hwnd, hdc);<br />
iLine++;<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int iNum = 1, iLine, cyClient;<br />
HDC hdc;<br />
int i, iSqrt;<br />
TCHAR szBuffer[16];<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
do<br />
{<br />
if (++iNum < 0)<br />
iNum = 0;<br />
<br />
iSqrt = (int) sqrt(iNum);<br />
for (i = 2; i <= iSqrt; i++)<br />
if (iNum % i == 0)<br />
break;<br />
}<br />
while (i <= iSqrt);<br />
<br />
iLine = CheckBottom(hwnd, cyClient, iLine);<br />
hdc = GetDC(hwnd);<br />
<br />
TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
wsprintf(szBuffer, TEXT("%d"), iNum));<br />
<br />
ReleaseDC(hwnd, hdc);<br />
iLine++;<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int iNum = 0, iNext = 1, iLine, cyClient;<br />
HDC hdc;<br />
int iTemp;<br />
TCHAR szBuffer[16];<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
if (iNum < 0)<br />
{<br />
iNum = 0;<br />
iNext = 1;<br />
}<br />
<br />
iLine = CheckBottom(hwnd, cyClient, iLine);<br />
hdc = GetDC(hwnd);<br />
<br />
TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
wsprintf(szBuffer, TEXT("%d"), iNum));<br />
<br />
ReleaseDC(hwnd, hdc);<br />
iTemp = iNum;<br />
iNum = iNext;<br />
iNext += iTemp;<br />
iLine++;<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int cxClient, cyClient;<br />
HDC hdc;<br />
int iDiameter;<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cxClient = LOWORD(lParam);<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
InvalidateRect(hwnd, NULL, TRUE);<br />
UpdateWindow(hwnd);<br />
<br />
iDiameter = rand() % (max(1, min(cxClient, cyClient)));<br />
hdc = GetDC(hwnd);<br />
<br />
Ellipse(hdc, (cxClient - iDiameter) / 2,<br />
(cyClient - iDiameter) / 2,<br />
(cxClient + iDiameter) / 2,<br />
(cyClient + iDiameter) / 2);<br />
<br />
ReleaseDC(hwnd, hdc);<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static HWND hwndChild[4];<br />
static TCHAR* szChildClass[] = { TEXT("Child1"), TEXT("Child2"),<br />
<br />
TEXT("Child3"),<br />
TEXT("Child4") };<br />
static WNDPROC ChildProc[] = { WndProc1, WndProc2, WndProc3, WndProc4 };<br />
HINSTANCE hInstance;<br />
int i, cxClient, cyClient;<br />
WNDCLASS wc;<br />
<br />
switch (message)<br />
{<br />
case WM_CREATE:<br />
hInstance = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);<br />
<br />
wc.style = CS_HREDRAW | CS_VREDRAW;<br />
wc.cbClsExtra = 0;<br />
wc.cbWndExtra = 0;<br />
wc.hInstance = hInstance;<br />
wc.hIcon = NULL;<br />
wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br />
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
wc.lpszMenuName = NULL;<br />
<br />
for (i = 0; i < 4; i++)<br />
{<br />
wc.lpfnWndProc = ChildProc[i];<br />
wc.lpszClassName = szChildClass[i];<br />
<br />
RegisterClass(&wc);<br />
<br />
hwndChild[i] = CreateWindow(szChildClass[i], NULL,<br />
<br />
WS_CHILDWINDOW |<br />
WS_BORDER | WS_VISIBLE,<br />
<br />
0, 0, 0, 0,<br />
<br />
hwnd, (HMENU) i,<br />
hInstance, NULL);<br />
}<br />
<br />
cyChar = HIWORD(GetDialogBaseUnits());<br />
SetTimer(hwnd, 1, 10, NULL);<br />
return 0;<br />
<br />
case WM_SIZE:<br />
cxClient = LOWORD(lParam);<br />
cyClient = HIWORD(lParam);<br />
<br />
for (i = 0; i < 4; i++);<br />
MoveWindow(hwndChild[i], (i % 2) * cxClient / 2,<br />
<br />
(i > 1) * cyClient / 2,<br />
<br />
cxClient / 2, cyClient / 2,<br />
TRUE);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
for (i = 0; i < 4; i++)<br />
SendMessage(hwndChild[i], WM_TIMER, wParam, lParam);<br />
return 0;<br />
<br />
case WM_CHAR:<br />
if (wParam == '\x1B')<br />
DestroyWindow(hwnd);<br />
return 0;<br />
<br />
case WM_DESTROY:<br />
KillTimer(hwnd, 1);<br />
PostQuitMessage(0);<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}
Help in any form is welcome
Thanks
|
|
|
|
|
In WndProc 's body you must:
<br />
case WM_SIZE:<br />
cxClient = LOWORD(lParam);<br />
cyClient = HIWORD(lParam);<br />
<br />
for (i = 0; i < 4; i++)<font color="blue">
MoveWindow(hwndChild[i], (i % 2) * cxClient / 2,<br />
<br />
(i > 1) * cyClient / 2,<br />
<br />
cxClient / 2, cyClient / 2,<br />
<br />
<br />
Should be enough...
rechi
|
|
|
|
|
I CAN NOT BELIEVE I MADE SUCH MISTAKE !!!
Thanks Bogdan.
|
|
|
|
|
Hello, all
Can anyone tell is a message cannot be peeked in a thread? for example:
Program 1:
LRESULT OnOK(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
MSG msg;
unsigned int nTimerID = SetTimer(hWnd, 1, 2000, 0);
while(1)
{
if( PeekMessage(&msg, hWnd, WM_TIMER, WM_TIMER, PM_REMOVE) )
{
MessageBox(hWnd, "Timer out", NULL, 0);
break;
}
}
KillTimer(hWnd, nTimerID);
}
Program 1 has no problem, the WM_TIMER message can be successfully peeked by PeekMessage. but please take a look at program 2.
Program 2:
LRESULT OnOK(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
unsigned long nThreadID = 0;
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, (void*)&hWnd, 0, &nThreadID);
WaitForSingleObject(hThread, INFINITE);
return 0;
}
unsigned long WINAPI ThreadProc(LPVOID pvParam)
{
HWND* phWnd = (HWND*)pvParam;
HWND hWnd = *phWnd;
MSG msg;
unsigned int nTimerID = SetTimer(hWnd, 1, 2000, 0);
while(1)
{
if( PeekMessage(&msg, hWnd, WM_TIMER, WM_TIMER, PM_REMOVE) )
{
MessageBox(hWnd, "Time out", NULL, 0);
break;
}
}
KillTimer(hWnd, nTimerID);
return 0;
}
I run program 2 in debug mode, i found the timer is successfully created, but there just no WM_TIMER message. Later i changed the "WaitForSingleObject"(in OnOK function) into "MsgWaitForMultipleObjects", i found the WM_TIMER again, but it is returned by "MsgWaitForMultiObjects", not in the thread function.
I'm confused now, can anyone tell me how to peek a message in a thread?
|
|
|
|
|
From the PeekMessage docs:
<quote>
PeekMessage retrieves only messages associated with the window identified by the hWnd parameter or any of its children as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters. If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread making the call. (PeekMessage does not retrieve messages for windows that belong to other threads.) If hWnd is –1, PeekMessage only returns messages with a hWnd value of NULL, as posted by the PostThreadMessage function. If wMsgFilterMin and wMsgFilterMax are both zero, PeekMessage returns all available messages (that is, no range filtering is performed).
They key point here is: "(PeekMessage does not retrieve messages for windows that belong to other threads.)" which I assume is the case here.
Why do you want to use a Timer in a thread anyway. You should either put the thread to sleep waiting for an event to wake it up, or if you have to then use Sleep() to temporarilly suspend it.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
Thanks Franks.
The main purpose of doing this by this way is i want to wait another message comes in, just like:
unsigned long ThreadProc(LPVOID pvParam)
{
MSG msg;
SetTimer(...);
while(1)
{
if( PeekMessage(&msg, hWnd, WM_SOCKET_MESSAGE, WM_SOCKET_MESSAGE, PM_REMOVE)
{
// Receive the data from the socket
// ...
// if (it's the last part of the data)
// break;
// else
// Reset the timer
// bSuccess = TRUE;
}
if( PeekMessage(&msg, hWnd, WM_TIMER, WM_TIMER, PM_REMOVE)
{
// Time is up
bSuccess = FALSE;
break;
}
}
KillTimer(...);
if( bSuccess )
return nLengthOfTheDataReceive;
else
return -1; //Error
}
Do you have a better way of doing this?
|
|
|
|
|
You could use clock().
eg. Before the while loop: clock_t StartTime = clock();
Then in the loop: if ( ( clock() - StartTime ) > MaxWait ) ...
It would be better still if the worker thread didn't have to waist time and CPU cycles in this loop though. Typically worker threads use WaitForxxx for some event to occur, do whatever processing is required, and then either exit or return to the WaitForxxx function.
You might want to look at some of the socket articles here at CP and see how they handle whatever you are trying to do.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
i considered this way, but i feel it's very un-effecient(nor the TIMER-Way), cos the CPU use all its time to check whether the time is over, but it can't determin which thread is more useful, every thread is treated equally.
do you think sleep() will be better? or another way?
|
|
|
|
|
Hello,
My program allows users print photos. How do I handle if they want to
print the same photo more than one copies? Do I really have to do StartPage
and EndPage for 100 times if they want 100 copies?
Thanks,
Hiu Sing Ngai
|
|
|
|
|
Hiusing wrote:
Do I really have to do StartPage
and EndPage for 100 times if they want 100 copies?
AFAIK, no. Have a look at the DEVMODE struct. It has a dmCopies member.
You can fill such a struct with a call to DocumentProperties, modify that single member, and set it back using another call to DocumentProperties. Please make sure the printer driver indeed handles multiple copies by checking the DM_COPIES flag in dmFields.
|
|
|
|
|
How do you get the red, green, and blue out of a COLORREF variable?
There's always one more bug.
|
|
|
|
|
Use GetRValue, GetBValue and GetGValue macros.
regards
|
|
|
|
|
Thanks a lot
There's always one more bug.
|
|
|
|
|
I got a lil problem here. I have several projects built with Visual Studio 6. Recently switched to Visual Studio.NET and found that it compiles/linkages all projects using MFC7 dlls. Of course it is not working on OS that doesn't have .NET framework installed. And this is not a good point. So my question is: is it still possible to use MFC6 in Visual Studio .NET and how to do that?
I even created a simple Test application with MFC, and tried it on Windows 98... "Get me MFC7 now!" it said
I hope there is some way to do that, otherwise I'll have to bring the VS.NET back to store, no use of it then
Philip Patrick
Web-site: www.stpworks.com
"Two beer or not two beer?" Shakesbeer
|
|
|
|
|