|
Hi,All
I want to convert rft format data to txt data with CRichEditCtrl. the code liks belows:
///
void CRichEditorDlg::OnBnClickedButton1()
{
EDITSTREAM es;
es.dwCookie = 1;
es.dwError = 0;
es.pfnCallback = StreamIn;
m_editor.StreamIn(SF_RTF, es);
es.dwCookie = 1;
es.dwError = 0;
es.pfnCallback = StreamOut;
m_editor.StreamOut(SF_TEXT, es);
}
///
DWORD CALLBACK CRichEditorDlg::StreamIn( DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb )
{
return 1;
}
///
DWORD CALLBACK CRichEditorDlg::StreamOut( DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb )
{
return 1;
}
I find that the streamIn can be called back, but the streamOut can no be called back. when I use SF_RTF param in the StreamOut, the StreamOut do call back.
///
m_editor.StreamOut(SF_TEXT, es);
My question is that i hope convert rtf format data to txt format!
Can you help me?
How can I do?
Thanks
SunHaiMin
sunhaiminbnu@tom.com
|
|
|
|
|
The callbacks should be normal functions, not class methods (unless they're static methods, of course). I've just found this code in my archives that writes text out of a richedit control called rich_ .
DWORD __stdcall EditStreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
LPCWSTR a = LPCWSTR(pbBuff);
if (dwCookie == 100)
{
}
return 0;
}
void CMyDlg::OnBnStreamOut()
{
EDITSTREAM es = { 100, 0, &EditStreamCallback };
rich_.StreamOut(SF_TEXT, es);
}
Note that where I've specified '100', you can specify any pointer value like (say) the pointer to your CRichEditorDlg object, so you could call back into the object from the callback if you needed.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
I need to carry out actions when an individual character is entered into a WIN32 C Edit control. These actions will take place each time a key is pressed and each time a key is released.
Characters will be entered into an Edit control, but the edit control does not know when a keydown/up message is needed, or an onchar.
I would appreciate some help in being able to 'trap' a keydown and keyup event when text is entered in an edit control.
Thanks
|
|
|
|
|
You can subclass[^] the edit control, to process the Windows messages you want to, before passing them on to the edit control.
If you're using MFC, derive from the edit control class[^] to do this.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks Stuart, but I'm using windows 32 API functions and not MFC. Any ideas?
|
|
|
|
|
Yes - the first link[^] in my message showed how to subclass a window just using Win32 calls.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I'm not using a dialog box, I'm using CreateWindow to draw my controls. It appears that the code that you pointed me to is more appropriate for windows interaction with a dialog ctrl: "...The following example shows how to subclass an instance of an edit control in a dialog box..."
If I'm not understanding it correctly, then where in the code would I make changes in order for it to work with my application? I have implemented
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
and
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
|
|
|
|
|
EvScott wrote: I'm not using a dialog box, I'm using CreateWindow to draw my controls.
That's fine - it works just the same.
Here's a code fragment for a small Win32 app I just created - it's just a standard VS2008 Windows app with an edit box created in the standard window. I've sub-classed the edit control to trap the WM_CHAR message and increment the character code (so if you press 'a', the edit control sees 'b'):
WNDPROC wpOrigEditProc;
LRESULT APIENTRY EditSubclassProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
if (uMsg==WM_CHAR)
++wParam;
return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
editWindow = CreateWindow(_T("EDIT"), _T("edit"), WS_CHILD|WS_BORDER, 10, 10, 300, 300, hWnd, 0, hInst, 0);
wpOrigEditProc = (WNDPROC) SetWindowLong(editWindow, GWL_WNDPROC, (LONG) EditSubclassProc);
ShowWindow(editWindow, SW_SHOWNORMAL);
break;
Simple, eh?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks for your time and patience Stuart. I'm having problems with the
editWindow = CreateWindow("EDIT", "edit", WS_CHILD|WS_BORDER, 10, 10, 300, 300, hWnd, 0, hInstance, 0);
It doesn't like the hInstance parameter as I have created my Edit and command button windows in the
WINAPI WinMain and I use its hInstance parameter
and not the
LRESULT CALLBACK WndProc
Is there any way round that? Am I doing something obviously wrong?
|
|
|
|
|
Just tried that (creating the edit window outside the WndProc - just after the main window's CreateWindow, but before a ShowWindow call) - I had to add WS_VISIBLE to the style parameter when I moved the edit window creation out of the WndProc. So, change WS_CHILD|WS_BORDER to WS_CHILD|WS_BORDER|WS_VISIBLE.
EvScott wrote: It doesn't like the hInstance parameter as I have created my Edit and command button windows in the WinMain
Can you define "doesn't like" a little more exactly?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks a million Stuart - you're the man. It worked a treat.
|
|
|
|
|
Actually, I've just run it with my code, and the system crashes with an error showing up at
return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
When I debug the code, I notice that hwnd has a valid handle but claims to be unused. Details as follows:
hwnd 0x000f070e {unused = ???}
|
|
|
|
|
is this the write way to add percentage
case 5 :
dReg = aReg /dreg x 100;
aReg = dReg;
break;
or do I need to add more
|
|
|
|
|
What do you mean, to add percentage?
case 5 :
dReg = (aReg /dreg) x 100;
aReg = dReg;
break;
Seems ok in calculating a percentage but what do you mean by 'add'?
|
|
|
|
|
it dosn't mean add (=) just in the sense of is this the write way to add the code to the rest of the code?
|
|
|
|
|
if 'x ' is actually '* ', then it looks correct, provided dReg and aReg are what they appear to be.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi,
I have an object which is purely a data structure which is shared between several threads. At present, I control access using a mutex which I grab when I need to use the data object. The problem is I have already found places where I have missed the WaitForSingleObject() to lock the data so I am considering making all data members private and using Get/Set members which will automatically grab and release the mutex.
My concern is what will be the impact on performance? At present, I only need to lock the mutex once before accessing all data members, then release the mutex.
To put some figures on things, the program runs a timer thread at a rate of 10Hz, three or four other threads need to access the data object at rates between 10Hz and 1Hz and there are 8 doubles in the data class.
I am really looking for opinions on whether there will be an appreciable performance hit from making it totally threadsafe.
Thanks
Tony
|
|
|
|
|
softwaremonkey wrote: shared between several threads
softwaremonkey wrote: using a mutex
I think you'll find critical section[^]s have better performance, as they're not kernel objects (they're no good between processes, unlike mutexes). Alternatively, if you're targetting Vista or newer, a slim reader/writer lock[^]?
softwaremonkey wrote: At present, I only need to lock the mutex once before accessing all data members, then release the mutex
I believe that if a thread has ownership of a critical section, it can request ownership again and it gets it without delay.
So, you could have something like:
struct S
{
S() { InitializeCriticalSection(&myDataLock); }
~S() { DeleteCriticalSection(&myDataLock); }
void Lock() { EnterCriticalSection(&myDataLock); }
void Unlock() { LeaveCriticalSection(&myDataLock); }
int GetData() { Lock(); int i(data_); Unlock(); return i }
void SetData(int i) { Lock(); data_ = i; Unlock(); }
private:
S(S const&);
S& operator=(S const&);
CRITICAL_SECTION myDataLock;
int data_;
};
I'd be tempted to use RAII[^] to lock and unlock, but that's a detail...
Anyway - you could then use the accessor OR surround accesses with Lock/Unlock calls?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
Thanks for the reply. Thats a neat solution.
Stuart Dootson wrote: I believe that if a thread has ownership of a critical section, it can request ownership again and it gets it without delay
This is true, but if we call lock() and unlock() within the Get/Set methods then we reliquish ownership so if I need to call Get/Set for each member, I think we still have the overhead of entering and leaving the critical section.
I am no expert on this subject but dont critical section only prevent multiple threads calling the same pielce of code at the same time? If thats the case, wont we still have problems if one thread is calling Get() while another thread is calling Set()?
Tony
|
|
|
|
|
softwaremonkey wrote: This is true, but if we call lock() and unlock() within the Get/Set methods then we reliquish ownership
No - the critical section retains an ownership count for the current thread, so you need to unlock as many times as you locked. So, if you did:
S s;
s.Lock();
s.SetData(1234);
s.Unlock();
s is locked throughout.
[edit]PS - I should emphasise that I'm pretty sure that's how critical sections work - I'd validate that belief before relying on it!!![/edit]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks,
I have implemented your suggestion and it all appears to be working great.
What a great feeling it was to delete all those WaitForSingleObject() calls in my code
I will now leave it running for a few weeks to make sure everything is OK.
Thanks for your help
|
|
|
|
|
Stuart Dootson wrote: I should emphasise that I'm pretty sure that's how critical sections work
Yes that is how they work.
Stuart Dootson wrote: I'd validate that belief before relying on it
Absofreakinlutly!! Of course fraking microsoft keeps hiding that information in the documentation[^]
When a thread owns a critical section, it can make additional calls to EnterCriticalSection or TryEnterCriticalSection without blocking its execution. This prevents a thread from deadlocking itself while waiting for a critical section that it already owns. To release its ownership, the thread must call LeaveCriticalSection one time for each time that it entered the critical section. There is no guarantee about the order in which waiting threads will acquire ownership of the critical section.
|
|
|
|
|
led mike wrote: Absofreakinlutly!! Of course fraking microsoft keeps hiding that information in the documentation[^]
I only point to the documentation - I don't bother reading it
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Even if that were true it would still put you one step ahead of most of these twittering monkeys.
|
|
|
|
|
I am not convinced it makes sense to protect data at the field level.
assuming not all combinations of valid values are valid for the overall object, having protection at the member level can still yield an invalid object state (one thread changing one member, then another thread changing another member)...
|
|
|
|
|