|
Hi all!
I meet a problem when I derive a class from CEdit. I want to use a font specficed by myself in it. Now I use the font in the message function of WM_CTLCOLOR in parent window of CEdit. The new font can be shown in CEdit. But when I double click the left-button or drag the mouse to select more than one character, the position of caret is wrong. I don't know how to fix it. Plz help me! Thx!
whiteclouds
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
Have you already tried
to set a font at the WM_CREATE reaction ?
virtual void BeHappy() = 0;
|
|
|
|
|
In WM_CREATE of CEdit? What difference between this and the action I take?
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
I think,
the difference is the settings purpose:
- your choice: the drawing (I guess, not in the edit mode)
- my choice: system assignment (I guess, for drawing and editing as well)
When you will want to use the class for a dialog item,
you could place the setting in your OnInitDialog() function :
...
m_cYourEdit.SetFont(..);
...
virtual void BeHappy() = 0;
|
|
|
|
|
I had test your method. I add some codes into OnInitDialog() of parent window as below:
CColorEdit *pEdit = (CColorEdit*)GetDlgItem(IDC_ED_COLOR);
if(pEdit)
{
CDC *pDC = pEdit->GetDC();
if(pDC)
{
pDC->SelectObject(m_fnt);
}
}
Then m_fnt can't be shown. And when I select some characters, the caret isn't in correct position.
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
Try it :
CWnd* pcWnd = GetDlgItem(IDC_ED_COLOR);
if (pcWnd->GetSafeHwnd()) {
pcWnd->SetFont(&m_cFont);
}
virtual void BeHappy() = 0;
|
|
|
|
|
Thx for your reply!
The position of caret is right now. But the font isn't be used in general. Only when I select the characters, the font can work.
That means, when not select any character, the OnPaint() respond function in the derived class hadn't work. Because I draw the characters using specific font in it. Maybe I make a mistake, isn't it?
Pls help me more! Thx!
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
It works in my OnInitDialog()
(I have not performed any font manipulations for WM_CTLCOLOR ) :
static CFont cFont;
cFont.CreatePointFont(120, _T("Arial"));
CWnd* pcWnd = GetDlgItem(IDC_EDIT1);
if (pcWnd->GetSafeHwnd()) {
pcWnd->SetFont(&cFont);
}
virtual void BeHappy() = 0;
|
|
|
|
|
I had test the code you provided. That can work correct. And I know that the problem was generated by my own class derived from CEdit. Thank you, Eugen Podsypalnikov!
There is some white cloud floating on the blue sky. That's the landscape I like.
|
|
|
|
|
Fellow teachers, my English is poor it may be more vague expression, but also you are forgiven!(各位老师,我英语很差所以表达上可能比较模糊,还请您们见谅!)
I compiled CELayoutEditor this program when met a very difficult issue, the program has been in one place error, the error is as follows:
(我在编译 CELayoutEditor 这个程序的时候,碰到了一个很棘手的问题,该程序一直在一个地方报错,错误如下:)
error C3861: 'wxStrdupA': identifier not found
Code fragment:
(代码片段
Buffer.h header files:
#ifndef _WX_BUFFER_H
#define _WX_BUFFER_H
#include "wxchar.h"
#include <stdlib.h> // malloc() and free()
DEFINE_BUFFER(wxCharBuffer, char, wxStrdupA);
#if wxUSE_WCHAR_T
DEFINE_BUFFER(wxWCharBuffer, wchar_t, wxStrdupW);
#endif // wxUSE_WCHAR_T
wxchar.h header files:
#include <stdarg.h>
#include <string.h>
/* Almost all compiler have strdup(), but not quite all: CodeWarrior under Mac */
/* and VC++ for Windows CE don't provide it */
#if !(defined(__MWERKS__) && defined(__WXMAC__)) && !defined(__WXWINCE__)
/* use #define, not inline wrapper, as it is tested with #ifndef below */
#define wxStrdupA strdup
#endif
Can tell about what is wrong somewhere? Is there any way to solve this problem? Thank you!
(可以告诉一下是什么地方出现问题?有没有什么办法解决这个问题?谢谢!)
|
|
|
|
|
Have no idea where you got these codes, just try changing wxchar.h to be like this:
#include <stdarg.h>
#include <string.h>
#ifndef wxStrdupA
#define wxStrdupA strdup
#endif
|
|
|
|
|
I tried to modify your approach, or can not eliminate this error, I now have Mozhe the
(我试着用你的方法修改,还是不能消除这个错误,我现在已经没辙了)
Code is found in the online, open source
(代码是在网上找到的,开源的)
|
|
|
|
|
Try it (this code has good chances under CE and under PC as well) :
{
...
TCHAR* tszResult = _tcsdup(_T("先生"));
...
free(tszResult);
...
}
virtual void BeHappy() = 0;
|
|
|
|
|
What do you? Did not understand ah!
|
|
|
|
|
class A
{
int i;
public:
void func1() {cout << "func1" << endl;};
static void func2() {cout << "func2" << endl;};
void func3() {i =1; cout << "func3" << endl;};
};
template <class T> class Smart
{
private :
T *m_ptr;
public :
explicit Smart(T *p = 0) : m_ptr(p) {}
T& operator*() {return *m_ptr;}
T* operator->() {return m_ptr;}
~Smart() {delete m_ptr;}
};
int main()
{
Smart<A> s(0);
(*s).func1();
system("PAUSE");
}
On running it I saw that it prints "func1" but how it it possible. I initialize it as NULL.
|
|
|
|
|
Basically your problem is resumed to something like this:
A* a = NULL;
a->func1();
In fact, the code will not crash in such scenario because you do not access any member variable of the class. Class methods are similar to global function except that they take an additional implicit parameter: the 'this' pointer (in your case it will be NULL). So, as long as you don't access any class variable, the code will not crash. Try now to print the value of 'i' in func1 and you'll that the code will crash.
|
|
|
|
|
Weird as it might look, a instance of the class is not really needed when calling such method, try the following simple code:
#include <iostream>
using namespace std;
class A
{
public:
void show(){ cout << "The show must go on." << endl; }
};
int main()
{
A * pA = NULL;
pA->show();
}
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]
|
|
|
|
|
I tried this and it worked find. But I am still confusing why it is working. What are the scenarios in which this thing will work.
Does it mean that U can access methods from NULL pointer but not attribute?
|
|
|
|
|
akashag wrote: Does it mean that U can access methods from NULL pointer but not attribute?
Yes, try:
#include <iostream>
using namespace std;
class A
{
public:
int a;
A():a(1){}
void show(){ cout << "The show must go on." << endl; }
void dump(){ cout << "member value is << " << a << endl;}
};
int main()
{
A * pA = NULL;
pA->show();
pA->dump();
}
[added]
From a OOP point of view (IMHO) this is a point where object abstraction clashes with technical implementation.
[/added]
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]
modified on Wednesday, April 28, 2010 3:36 AM
|
|
|
|
|
Yes, as I said earlier a class method is similar as a global method (so, there is one single function even if there are mutliple instances of the class or even no instance at all). When the method is called, the address of the instance on which the function is called is passed to the function (the 'this' pointer). This means that as long as you don't access any variables of the class, you can perfectly call the function even if the instance is NULL.
|
|
|
|
|
There is one exception: if the function call is indierect because of a v-table (the method is virtual), even if the function itself don't refer the this pointer, a valid this is required to perform the runtime redirection.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Yes, I forgot that.
Mmmh, let's say I didn't want to confuse the OP with too much information
|
|
|
|
|
Cedric Moonen wrote: I didn't want to confuse the OP with too much information
But what about me? I am still trying to absorb this.
It's time for a new signature.
|
|
|
|
|
Err, what do you mean exactly ? I don't get it...
|
|
|
|
|
I mean I was confused by this thread, but now I'm beginning to understand, thanks to your comments.
It's time for a new signature.
|
|
|
|