Click here to Skip to main content
15,936,395 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Reg : Key Down message not receiving in dialog procedure Pin
Hamid_RT7-Nov-08 20:23
Hamid_RT7-Nov-08 20:23 
GeneralRe: Reg : Key Down message not receiving in dialog procedure Pin
MANISH RASTOGI7-Nov-08 20:32
MANISH RASTOGI7-Nov-08 20:32 
Question[please help]why i cannot render my pic?thks! Pin
kaviniswell7-Nov-08 15:51
kaviniswell7-Nov-08 15:51 
AnswerRe: [please help]why i cannot render my pic?thks! Pin
Hamid_RT7-Nov-08 20:47
Hamid_RT7-Nov-08 20:47 
GeneralRe: [please help]why i cannot render my pic?thks! Pin
kaviniswell7-Nov-08 20:53
kaviniswell7-Nov-08 20:53 
GeneralRe: [please help]why i cannot render my pic?thks! Pin
Hamid_RT8-Nov-08 4:40
Hamid_RT8-Nov-08 4:40 
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 
Hi experts,

I have a VB form that calls a "C" function (implemented in a DLL) to draw the text on the VB form. The VB form sends the window hDC, text position, font name, font size and the chinese text

I have taken analysis from an article in Code Project and created the following "C" function to take the inputs from VB and draws the text on the VB windows. Please see my "C" function below

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

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, &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(&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;
}


It appears to draw something but the content is not correct. It looks like a wrong chinese is getting displayed.


The following is my VB sample (You can change the font name in form load and enter some text in the textbox and then click on enter, which should draw the text)

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 = &H800
Private Const GCS_RESULTREADSTR = &H200
Private Const GCS_COMPSTR = &H8
Private Const GCS_COMPREADSTR = &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 > 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 "'" & GetCompositionResult & "'"

Call CodeProjectDrawText(Me.hdc, strFontName, lngFontSize, GetCompositionResult, lngMouseDownX, lngMouseDownY)
End Sub


Thanks,
Tian
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 
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 

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.