Click here to Skip to main content
15,943,325 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionIs there a way to call a method with a CStringT Variable ? Pin
SNArruda7-Nov-08 11:48
SNArruda7-Nov-08 11:48 
QuestionHow to draw a chinese text on a window Pin
Tianan7-Nov-08 10:50
Tianan7-Nov-08 10:50 
JokeRe: How to draw a chinese text on a window Pin
sashoalm7-Nov-08 23:52
sashoalm7-Nov-08 23:52 
GeneralRe: How to draw a chinese text on a window Pin
Hamid_RT8-Nov-08 3:31
Hamid_RT8-Nov-08 3:31 
GeneralRe: How to draw a chinese text on a window Pin
Tianan8-Nov-08 13:01
Tianan8-Nov-08 13:01 
GeneralRe: How to draw a chinese text on a window Pin
Hamid_RT8-Nov-08 20:52
Hamid_RT8-Nov-08 20:52 
AnswerRe: How to draw a chinese text on a window Pin
Iain Clarke, Warrior Programmer8-Nov-08 13:10
Iain Clarke, Warrior Programmer8-Nov-08 13:10 
GeneralRe: How to draw a chinese text on a window Pin
Tianan8-Nov-08 16:27
Tianan8-Nov-08 16:27 
Hello again, I am sorry for not placing the code in the <pre></pre> tags, so the following are the code again in tags Smile | :) first tag section are the C code again. Second tag section are the VB code again.

<pre>
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;tchar.h&gt;

HFONT m_hFont;
LOGFONT m_LogFont;

_TCHAR m_szSection[256];
_TCHAR m_szEntry[256];


void DefaultFontInit();
HFONT CreateFontLocal();

long
__stdcall
CodeProjectDrawText(
long lWindowDC,
_TCHAR *lpszFontName,
long lFontSize,
_TCHAR *lpszMultiByteText,
long X,
long Y)
{
long nFontHeight = 0;
long nLen = 0;
long nStatus = 0;

_tcscpy(m_szSection, _T("Settings"));
_tcscpy(m_szEntry, _T("Font"));

nFontHeight = - MulDiv (lFontSize, GetDeviceCaps (GetDC (0), LOGPIXELSY), 72);

DefaultFontInit(lpszFontName, nFontHeight);

SelectObject(lWindowDC,m_hFont);

nLen = _tcslen(lpszMultiByteText);

nStatus = TextOut(lWindowDC, X, Y, &amp;lpszMultiByteText, nLen);

return (0);
}

void DefaultFontInit(_TCHAR *lpszFontName, long nFontHeight)
{
HFONT hFont;

// define the logical parameters for the default font
//m_LogFont.lfHeight = -11; // size 8
m_LogFont.lfHeight = nFontHeight;
m_LogFont.lfWidth = 0;
m_LogFont.lfEscapement = 0;
m_LogFont.lfOrientation = 0;
m_LogFont.lfWeight = FW_NORMAL;
m_LogFont.lfItalic = 0;
m_LogFont.lfUnderline = 0;
m_LogFont.lfStrikeOut = 0;
m_LogFont.lfCharSet = 0;
m_LogFont.lfOutPrecision = OUT_STRING_PRECIS;
m_LogFont.lfClipPrecision = CLIP_STROKE_PRECIS;
m_LogFont.lfQuality = DEFAULT_QUALITY;
m_LogFont.lfPitchAndFamily = FF_SWISS | VARIABLE_PITCH;
//_tcscpy(m_LogFont.lfFaceName, _T("Lucida Sans Unicode"));
_tcscpy(m_LogFont.lfFaceName, lpszFontName);

// create the associated font
hFont = CreateFontLocal();
}

HFONT CreateFontLocal()
{
HFONT hFont = CreateFontIndirect(&amp;m_LogFont);

if (hFont == NULL)
{
// GetLastError(); can be used to understand why the font was not created
MessageBox(0, _T("Impossible to create font\n"), _T("My Sample"), 0);
}

// don't forget to delete the current font
if (m_hFont != NULL)
{
DeleteObject(m_hFont);
}

// store the font (event if the creation has failed)
m_hFont = hFont;

return hFont;
}
</pre>

<pre>
Private Const FW_NORMAL = 400
Private Const DEFAULT_CHARSET = 1
Private Const OUT_DEFAULT_PRECIS = 0
Private Const CLIP_DEFAULT_PRECIS = 0
Private Const DEFAULT_QUALITY = 0
Private Const VARIABLE_PITCH = 2
Private Const FF_SWISS = 32 ' Variable stroke width, sans-serifed.

Private Const GCS_RESULTSTR = &amp;H800
Private Const GCS_RESULTREADSTR = &amp;H200
Private Const GCS_COMPSTR = &amp;H8
Private Const GCS_COMPREADSTR = &amp;H1
Private Const SCS_SETSTR = (GCS_COMPREADSTR Or GCS_COMPSTR)

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function CodeProjectDrawText Lib "CodeProjectSample.dll" ( _
ByVal lWindowDc As Long, _
ByVal szFontName As String, _
ByVal szFontSize As Long, _
ByVal lstrText As String, _
ByVal x As Long, _
ByVal y As Long) _
As Long


Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long

Private Declare Function ImmGetContext Lib "imm32.dll" (ByVal hwnd As Long) As Long
Private Declare Function ImmGetOpenStatus Lib "imm32.dll" (ByVal himc As Long) As Long
Private Declare Function ImmGetCompositionString Lib "imm32.dll" Alias "ImmGetCompositionStringA" (ByVal himc As Long, ByVal dw As Long, lpv As Any, ByVal dw2 As Long) As Long
Private Declare Function ImmReleaseContext Lib "imm32.dll" (ByVal hwnd As Long, ByVal himc As Long) As Long


Private Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W As Long, ByVal I As Long, ByVal u As Long, ByVal S As Long, ByVal C As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, ByVal F As String) As Long
Private Declare Function TextOutW Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As Long, ByVal nCount As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long


Private lngMouseDownX As Long
Private lngMouseDownY As Long

Private strFontName As String
Private lngFontSize As Long

Private mstrMarkupText As String


Private Sub Form_Load()
lngFontSize = 20
strFontName = "ËÎÌå"

lngMouseDownX = 50
lngMouseDownY = 150
End Sub


Private Sub Text1_Change()

Dim hct As Long
Dim ll As Long
Dim len5 As Long
Dim str5() As Byte
Dim hwnd As Long
Dim GetCompositionResult As String

hwnd = Screen.ActiveForm.hwnd

GetCompositionResult = ""
hct = ImmGetContext(hwnd)

If hct = 0 Then
Exit Sub
End If


'Get the size of the result string
ll = ImmGetCompositionString(hct, GCS_RESULTSTR, vbNullString, 0)

If ll = 0 Then
'Call DrawMarkupText(Me.hDC, strFontName, lngFontSize, KeyCode, lngMouseDownX, lngMouseDownY)
ElseIf ll &gt; 0 Then

'lstrText = ""
ReDim str5(0 To ll - 1)
len5 = ImmGetCompositionString(hct, GCS_RESULTSTR, str5(0), ll)

GetCompositionResult = StrConv(str5, vbUnicode)
End If
ImmReleaseContext hwnd, hct

Debug.Print "'" &amp; GetCompositionResult &amp; "'"

Call CodeProjectDrawText(Me.hdc, strFontName, lngFontSize, GetCompositionResult, lngMouseDownX, lngMouseDownY)
End Sub
</pre>

So my logic is that VB is basically an exe application that just contains a textbox on a form. It then calls "CodeProjectDrawText()" function which is written in C code (an C dll) to draw Chinese character on the VB form. So the key is that my TextOut() api in C code is actually drawing the Chinese character out. Just a note, please don't get confused with the TextOut() api being just ASCII, it is really unicode TextOutW(). In my C project settings, the unicode flag is already set, so during TextOut() it is actually calling the TextOutW() instead of just TextOut().

I have tried by calling the same TextOutW()function in VB (writting this whole small app in VB instead of C)and it works fine, but because this drawing feature has to be in C code, I can't do this in VB Frown | :(

Thank you very much Iain for your suggestions, Your suggestions for "m_hFont is not inialised" makes sense, so I changed the order of the code just a little by moving the "_tcscpy(m_LogFont.lfFaceName, lpszFontName);" after "hFont = CreateFontLocal();" call... but my result is still the same.

I actually stepped through the code couple time already Smile | :) I monitored all the variables such as "lpszMultiByteText", "lpszFontName" and so on... all seems to be correct right before the TextOut()api. Well at least in memory they seem to be correct. When I watched the 2 variables in memory stack they all appear to be correct. I mean I see my selected font name, Chinese text that I entered. But after TextOut() I see other wrong Chinese character or garbage being displayed. So somewhere fonts or code page is still not sinked up. Anyway thanks for all the good imput guys!

Tian
QuestionChecking for a registered DLL Pin
kpiciulo7-Nov-08 9:57
kpiciulo7-Nov-08 9:57 
AnswerRe: Checking for a registered DLL Pin
CPallini7-Nov-08 10:42
mveCPallini7-Nov-08 10:42 
QuestionStrange Behaviour on LookupPrivilegeValue Pin
bsaksida7-Nov-08 8:42
bsaksida7-Nov-08 8:42 
QuestionVisual C++ 2003 MDI file associations fail out of the box [solved] Pin
bob169727-Nov-08 4:49
bob169727-Nov-08 4:49 
QuestionDisable a Button ina SDI application Pin
m_mun7-Nov-08 2:50
m_mun7-Nov-08 2:50 
AnswerRe: Disable a Button ina SDI application Pin
Nishad S7-Nov-08 2:53
Nishad S7-Nov-08 2:53 
AnswerRe: Disable a Button ina SDI application Pin
Saurabh.Garg7-Nov-08 2:55
Saurabh.Garg7-Nov-08 2:55 
AnswerRe: Disable a Button ina SDI application Pin
Hamid_RT7-Nov-08 20:26
Hamid_RT7-Nov-08 20:26 
QuestionHow to get a string according to a "language identifier"? [modified] Pin
Joseph Marzbani7-Nov-08 1:08
Joseph Marzbani7-Nov-08 1:08 
AnswerRe: How to get a string according to a "language identifier"? Pin
CPallini7-Nov-08 1:54
mveCPallini7-Nov-08 1:54 
GeneralRe: How to get a string according to a "language identifier"? Pin
Joseph Marzbani7-Nov-08 4:19
Joseph Marzbani7-Nov-08 4:19 
Questionhow to show enable/disable check mark for menu items in a dialog based application? Pin
puppya7-Nov-08 0:51
puppya7-Nov-08 0:51 
AnswerRe: how to show enable/disable check mark for menu items in a dialog based application? Pin
Saurabh.Garg7-Nov-08 3:01
Saurabh.Garg7-Nov-08 3:01 
AnswerRe: how to show enable/disable check mark for menu items in a dialog based application? Pin
Mark Salsbery7-Nov-08 3:11
Mark Salsbery7-Nov-08 3:11 
GeneralRe: how to show enable/disable check mark for menu items in a dialog based application? Pin
Nishad S7-Nov-08 3:21
Nishad S7-Nov-08 3:21 
GeneralRe: how to show enable/disable check mark for menu items in a dialog based application? Pin
Mark Salsbery7-Nov-08 3:29
Mark Salsbery7-Nov-08 3:29 
GeneralRe: how to show enable/disable check mark for menu items in a dialog based application? Pin
Nishad S7-Nov-08 3:35
Nishad S7-Nov-08 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.