DrawTextW( hdc, strText, -1, rectB, DT_SINGLELINE);

Hi,
I have a peice of a code for loading a icon and draw the text below
the icon on the same button...
i was sucessful in loading an icon but when tried with the DrawText()
to insert a text, its not working and the problem is with the hdc
parameter of DrawText function.

void CNewButtonDlg::CallButton()
{
	CString strText("");
                //To place the text on the button
	RECT rectA = {180, 20, 270, 90};  //left, top, right, bottom

                //To nullify the error that parameter must be a LPRECT
and not RECT type i have done so
	LPRECT rectB = &rectA;
	CDC my_newButton;
	HDC hdc;

	CWnd *pParent = pParent->GetSafeOwner();
	RECT rect = {180, 10, 270, 80};

	my_Button.Create(_T("&Button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
| BS_ICON /*| BS_TOP | BS_TEXT*/,
		rect, this, IDC_BUTTON_NEW);


	if(my_Button.GetIcon() == NULL)
	
my_Button.SetIcon(::LoadIcon(AfxGetInstanceHandle( ),MAKEINTRESOURCE(IDI_ICON_NEW)));


	//CDC *pMyCDC  = pButtonWnd->GetDC();
	my_Button.GetWindowText(strText);

                //This function will remove the ampersand present in
the button text.
	DelAmpersand(strText);

	hdc = ::GetDC(m_hWnd);
                //problem with the parameters, even i tried with the
                //CDC::DrawText(4 - parameters, its not working);
	DrawTextW( hdc, strText, -1, rectB, DT_SINGLELINE); /*| TA_UPDATECP*/
}

Help me out to draw a text on the button after loading a icon on a
button.

0
sbchetan (8)
4/4/2007 5:09:29 AM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
1397 Views

Similar Articles

[PageSpeed] 14

This code is completely wrong.  The only way to do this is to subclass the CButton, make
in owner-draw, and do this in the DrawItem handler of the button.  THe code here is
effectively totally useless, and must be discarded.  You cannot paint on the surface of
any window from outside the OnPaint [or OnDraw, or DrawItem, both of which are triggered
from OnPaint] of the window and expect anything reasonable to happen.  The only exception
is the case of active dragging done in OnMouseMove handlers.

THis code will, in effect, be ignored.  It does not belong here, and any errors that you
experience are because this code is in the wrong place.  
						joe
On 3 Apr 2007 22:09:29 -0700, "Chetan" <sbchetan@gmail.com> wrote:

>Hi,
>I have a peice of a code for loading a icon and draw the text below
>the icon on the same button...
>i was sucessful in loading an icon but when tried with the DrawText()
>to insert a text, its not working and the problem is with the hdc
>parameter of DrawText function.
>
>void CNewButtonDlg::CallButton()
>{
>	CString strText("");
>                //To place the text on the button
>	RECT rectA = {180, 20, 270, 90};  //left, top, right, bottom
>
>                //To nullify the error that parameter must be a LPRECT
>and not RECT type i have done so
>	LPRECT rectB = &rectA;
>	CDC my_newButton;
>	HDC hdc;
>
>	CWnd *pParent = pParent->GetSafeOwner();
>	RECT rect = {180, 10, 270, 80};
>
>	my_Button.Create(_T("&Button"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
>| BS_ICON /*| BS_TOP | BS_TEXT*/,
>		rect, this, IDC_BUTTON_NEW);
>
>
>	if(my_Button.GetIcon() == NULL)
>	
>my_Button.SetIcon(::LoadIcon(AfxGetInstanceHandle( ),MAKEINTRESOURCE(IDI_ICON_NEW)));
>
>
>	//CDC *pMyCDC  = pButtonWnd->GetDC();
>	my_Button.GetWindowText(strText);
>
>                //This function will remove the ampersand present in
>the button text.
>	DelAmpersand(strText);
>
>	hdc = ::GetDC(m_hWnd);
>                //problem with the parameters, even i tried with the
>                //CDC::DrawText(4 - parameters, its not working);
>	DrawTextW( hdc, strText, -1, rectB, DT_SINGLELINE); /*| TA_UPDATECP*/
>}
>
>Help me out to draw a text on the button after loading a icon on a
>button.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15979)
4/4/2007 3:36:36 PM
thanks for the replay, i will try with another subclass.

0
sbchetan (8)
4/5/2007 4:48:03 AM
Also pass the length of the string to DrawTextW instead of -1. 
_tcslen(strText);

AliR.

"Chetan" <sbchetan@gmail.com> wrote in message 
news:1175748483.729808.222390@n76g2000hsh.googlegroups.com...
> thanks for the replay, i will try with another subclass.
> 


0
AliR3470 (3234)
4/5/2007 2:58:19 PM
You can take a look at my Better Bitmap Button class on my MVP TIps site.  It can be a
starting point for what you want to do.
				joe

On 4 Apr 2007 21:48:03 -0700, "Chetan" <sbchetan@gmail.com> wrote:

>thanks for the replay, i will try with another subclass.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15979)
4/5/2007 3:58:36 PM
Reply:

Similar Artilces:

how to make a cdc from a hdc ?
I have a HDC, is it possible to make a CDC from it? thanks, bn You can do this: CDC cDC; cDC.Attach(hdc); tankc "bn" <b@b.com> wrote in message news:%23tSzyNJ$EHA.3372@TK2MSFTNGP10.phx.gbl... > I have a HDC, is it possible to make a CDC from it? > > thanks, > bn > > tthere aretwo ways CDC dc; dc.Attach(hdc); //do ur stuff dc.Detach(); or CDC dc=CDC::FromHandle(hdc); //no need to detach. "bn" wrote: > I have a HDC, is it possible to make a CDC from it? > > thanks, > bn > > > The second one sho...

how to get hwnd from hdc
Hi, I know how to get a HDC from HWND, but I am given a HDC from window media player and I want to correlate the mouse position with objects on my HDC. In order to use ScreenToClient I need the HDC's hwnd. How is that possible? If I use GetActiveWindow, I have an offset equal to the media player's menu and button panes. -- ------------------------------------------------------------------------- Music, Recipes, Photos, and more: http://www.sequoiagrove.dk "You don�t frighten us, English pig-dogs! Go and boil your bottoms, sons of a silly person. I blow my nose at you, so...

HDC
Env: WindowsXP, VC++6.0 Who can kindly tell me the different about using hDC1 or hDC2 inside OnDraw() below. CView::OnDraw(CDC* pDC) { HDC hDC1 = pDC->m_hDC; HDC hDC2 = ::GetDC(m_hWnd); } TIA William William wrote: > Env: WindowsXP, VC++6.0 > > Who can kindly tell me the different about using hDC1 or hDC2 inside > OnDraw() below. > > CView::OnDraw(CDC* pDC) > { > HDC hDC1 = pDC->m_hDC; > HDC hDC2 = ::GetDC(m_hWnd); > } > > TIA > William The DC passed to OnDraw is special in several ways. It is normally a CPaintDC, which i...

HDC to HDIB
I need to convert hdc to hdib data of 256 colors of size 180*120 But i didn't get output as required plz help me { BITMAPINFO Binf; BITMAPINFOHEADER bi;int ki; HDC hMemDC = CreateCompatibleDC(hDC); int DCWidth = GetDeviceCaps( hDC, HORZRES ); int DCHeight = GetDeviceCaps( hDC, VERTRES ); StretchBlt(hMemDC,0,0,180,120,hDC,0,0,DCWidth,DCHeight,SRCCOPY); bi.biSize = sizeof(BITMAPINFOHEADER);//40; bi.biWidth = 180;//Let it be constant width for our saving bitmaps bi.biHeight = 120;//Let it be constant height for our saving bitmaps bi.biPlanes = 1; bi.biBitCount = 8; bi.b...

HDC and CDC
I am attempting for the first time to use CreateDibSection and am puzzled as to how to use HDC within the MFC framework. I can't see that CDC returns a handle, so I am wondering if these two are mutually exclusive. Is there a bridge between the handle and the class? "Steve Russell" <srussell@removethisinnernet.net> wrote in message news:uQoa24INEHA.624@TK2MSFTNGP11.phx.gbl... > I am attempting for the first time to use CreateDibSection and am puzzled as > to how to use HDC within the MFC framework. I can't see that CDC returns a > handle, so I am wonderin...

HDC Clipping issues
Hi, I am drawing some things in my view. I wish to draw stuff all over the client window, then set a clip rect, such as draw a line from one corner of the screen to another, but make sure that what I draw is only visible on a portion of the view. CMyView::OnDraw(CDC* pDC) { CMyDrawRoutines d; d.BeginDraw(pDC->m_hDC); d.DrawSomeStuffInEntireClientWindow(); CRect rc; GetClientRect(&rc); rc.DelfateRect(100,100,100,100); CRgn rgn; rgn.CreateRectRgnIndirect(&rc); d.SelectClipRgn(&rc); d.MoveTo(0,0); d.LineTo(rcClient.right, rcClient...

CDC/HDC????
I am trying to develop a print screen utility for a windows CE program. I am writing it in Embedded VC++ and running into some weirdness. My plan of action is to use GetClientRect() to find the location of the window, then loop through each pixel in that region and perform a GetPixel() to get the RGB value. My problem is that I need a HDC in order to perform GetPixel(). I used GetActiveWindow() to get an HWND, then used FromWindow() to get a CWnd pointer. I then tried the following line to get an HDC. HDC hdc=pWnd->GetWindowDC(); When I compile though I get the following error. erro...

get font from HDC
Hi, I would like to modify a function used to render some HTML and use the font I have choosen : // C functions int __stdcall DrawHTML( HDC hdc, // handle of device context LPCTSTR lpString, // address of string to draw int nCount, // string length, in characters LPRECT lpRect, // address of structure with formatting dimensions UINT uFormat // text-drawing flags ) { .... /* get the "default" font from the DC */ SavedD...

Get BItmap From HDC
I'm stuck with getting CBitmap object from HDC. I'm using ecw SDK to view ecw files, and I need my soft to return CBitmap object of this .ecw file. in SDK there is a method: DrawImage( HDC DeviceContext, LPRECT pClipRect, IEEE8 dWorldTLX, IEEE8 dWorldTLY, IEEE8 dWorldBRX, IEEE8 dWorldBRY ); wich works just fine, it draws picture into givet device context. DrawImage(hdc, &m_Rect, m_dTLX, m_dTLY, m_dBRX, m_dBRY); CBitmap bitmap; CBitmap * bmp=bitmap.FromHandle((HBITMAP)hdc); is this right?? if DrawImage method drawed picture into HDC already?. If you have an HDC, and it has a bi...

Rookie Q: Release HDC?
Hi, Imagine a little class like this: class CMyClass { public: CMyClass(HDC hDC) { m_hDC = hDC; } HDC GetHDC() { return m_hDC; } private: HDC m_hDC; } Now, in my CMyApp::OnView I do this: void CMyApp::OnView(CDC* pDC) { CMyClass mc(pDC->m_hDC); } Now, HDC is just an UINT, and all I am passing is this number. I shouldn't have to do any relasing of anything, do I? Or does my CMyClass class destructor have to do a DeleteObject(m_hDC) or something? Lisa The HDC is part of the CDC class. Be sure not to delete it, or even store it (except i...

How do I tell what HDC has a HBITMAP selected?
How do I tell which HDC has a specific HBITMAP selected? I am using the ImageList_GetImageInfo() function to get direct access to the HBITMAP in the imagelist. The problem is the system keeps this bitmap selected into a memory DC, so I can't select it into my DC to perform a BitBlt from it. There is a KB article that tells me to use the CopyImage function. CopyImage is terribly slow. Any ideas? ...

Load Tif file in memory hDC
How do I load a TIF file in memory hDC and save it back to another file after some processing using DrawText. I want to use the memory hDC because I dont want to display the image. Is there an example for doing this. Asfar wrote: > How do I load a TIF file in memory hDC and save it back to another file > after some processing using DrawText. > > I want to use the memory hDC because I dont want to display the image. > > Is there an example for doing this. > > I am using Cximage to do this. See here http://www.codeproject.com/bitmap/cximage.asp ...

slower BitBlt from unmanaged mem into C# hDC than old VB6
BitBlt'ing from unmanaged memory into an hDC obtained in a managed (C#) program takes 50-100% longer than the same BitBlt into an hDC obtained in an unmanaged (old VB6) program. My ShowPic() function resides in a DLL written in straight C. It reads a JPEG into memory allocated within the unmanaged DLL and then StretchDIBits()s it to the hDC passed. Some ancient VB6 code called it in a Paint event: Private Sub Picture1_Paint() ShowPic(Picture1.hdc, ...) End Sub Here is the C# code that is almost 100% slower: private void picObj1_Paint(object sender, System.Windows....

Panasonic HDC-SD5 AVCHD 3CCD Flash Memory High Definition Camcorder with 10x Optical Image Stabilization w/DVD Burner
Price:Too low to display Image: http://thediscountguru.info/image.php?id=B000YT5R28 Best deal: http://thediscountguru.info/index.php?id=B000YT5R28 Works great but in low light conditions the video isn't that great. Well lit and sunny areas the HandyCam captures great HD video. I have owned the camera for 2 weeks now and am returning it. I currently own 3 other video cameras, but this is my forst HD camera. Everything about the camera is wonderful except for one MAJOR flaw, there is a high pitched whine when I play back the vodeo through the camera. It is quite noticeable to say th...