Thread: Yet another button question!

  1. #1
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21

    Yet another button question!

    Hi all,

    I need to wrap text on a button. It is for a question bank, in which the answer is displayed on buttons for the user to select. The problem starts when the length of the string is longer than the button is wide, and needs to start a new line. I have looked in the search, but not pinpointed antything exactly specific.

    I could do this counting characters and inserting hard line breaks "\n", but with all those WIN API messages available, I thought there might be one available already.

    Any help, please? Thanks in advance....

    (some rough code to set the picture of what I am up to)...

    Code:
    //	Window Procedure......
    LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(Msg)
    	{
    	case WM_CREATE:
    		{
    			loadfile(hWnd); // This function call just loads the strings, no big deal...
    			
    			/* 
    			Here is where I make a button. The problem is;
    			I cannot seem to get the text to wrap and start a new line if it
    			is wider than the button.
    			*/
    
    			HWND hwndButton0 = CreateWindowEx(
    			WS_EX_CLIENTEDGE,
    			"BUTTON",    
    			MyFile[0].c_str(),       // Text on the button
    			WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN, //  | BS_PUSHBUTTON , 
    			10,         
    			10,          
    			100,        
    			100,        
    			hWnd,       
    			(HMENU)ID_BUT0,
    			//(HINSTANCE)GetWindowLong(*****),
    			(HINSTANCE)GetModuleHandle(NULL),
    			NULL);      
    
    		}
    		break;
    	case WM_PAINT:
    		if (newtext == true)
    		{
    			showfile(hWnd);
    			newtext = false;
    		}
    		break;
    	case WM_MOVE:
    		newtext = true;
    		break;
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case ID_BUT0:
    			{
    				
    				p += 10;
    				HWND hwndButton0 = GetDlgItem(hWnd, ID_BUT0);
    				//SetWindowText(hwndButton0, "Try now"); // This works...
    					
    				SendMessage(hwndButton0, WM_SETTEXT, 0, (LPARAM)"Another way");
    				//	This SendMessage() is another way to do it, too. 
    				//	Take your pick, I think (?).
    				
    				//HDC hdcBut0 = GetDCEx(hwndButton0, NULL, DCX_WINDOW);
    				//TextOut(hdcBut0, 10,10, "HELLO", strlen("HELLO"));
    				//ReleaseDC(hwndButton0, hdcBut0);
    				
    				/*	This does NOT work. I don't understand the "range" bit in GetDCEx()....
    					However, the thing is NONE of this allows me, apparently, to
    					be able to wrap the text effectively
    				*/
    								
    
    				EndDialog(hwndButton0, NULL);
    				
    				break;
    			}
    			//break;
    		
    		}
    		break;
    	
    	case WM_DESTROY:
    		PostQuitMessage(WM_QUIT);
    		break;
    	default:
    		return DefWindowProc(hWnd, Msg, wParam, lParam);
    	}
    	return 0;
    }

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    SetWindowText or SetDlgItemText will change the buttons caption...but you need to change its style to accept multiline strings.
    Use BS_MULTILINE with SetWindowLong.
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  3. #3
    Registered User KeithS's Avatar
    Join Date
    Jul 2009
    Location
    Colombia
    Posts
    21
    ¡Muchas gracias, Joelito!

    That did the trick, and also implemented as shown below. Nice!

    Code:
    HWND hwndButton0 = CreateWindowEx( 
    	WS_EX_WINDOWEDGE,
    	"BUTTON",    
    	MyFile[0].c_str(),        
    	WS_VISIBLE | WS_BORDER | WS_CHILD | BS_PUSHBUTTON | BS_MULTILINE,  
    	10,          
    	10,          
    	200,        
    	200,        
    	hWnd,       
    	(HMENU)ID_BUT0,
    	(HINSTANCE)GetModuleHandle(NULL),
    	NULL);
    ...and with my various experiments with this last night, I figured out this alternate way to do the same sort of thing, using the DC's and DrawTextEx, and without using BS_MULTILINE. It is a bit long winded, but may have a use sometime. I place it here for the record. As a rain check, does it seem okay?

    Code:
    case WM_COMMAND:
    	switch(LOWORD(wParam))
    	{
    	case ID_BUT0:
    		{
    			DRAWTEXTPARAMS tParams;
    			tParams.cbSize = sizeof(DRAWTEXTPARAMS);
    			tParams.iLeftMargin = 5;
    			tParams.iRightMargin = 5;
    			tParams.iTabLength = 1;
    			tParams.uiLengthDrawn = 10;
    			
    			RECT rcBut;
    			//HWND hwndButton0 = GetWindow(hWnd, ID_BUT0); // No! This gets the desktop!
    			
    			HWND hwndButton0 = GetDlgItem(hWnd, ID_BUT0); // This is right...
    			HDC hDcBut = GetWindowDC(hwndButton0);
    			
    			char *char_buffer = "A Quick Test to see what happens now.";
    			
    			GetWindowRect(hwndButton0, &rcBut);
    			SetRect(&rcBut, 10, 10, 190, 190); // Margins for the rectangle inside the button.
    			
    			SetTextAlign(hDcBut, TA_LEFT | TA_TOP /*| TA_NOUPDATECP*/);
    			DrawTextEx(hDcBut, buffer, strlen(buffer), &rcBut, DT_EDITCONTROL | DT_NOCLIP | DT_WORDBREAK, &tParams);
    			//  The DT_WORDBREAK style is the key with this method...			
    
    			ReleaseDC(hwndButton0, hDcBut);
    			CloseHandle(hwndButton0);
    		}
    	}
    Thanks again, I understand so much more about Rects, Controls, and DC's after these last 24 odd hours!

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    This code has no sense.
    You must not draw outside WM_PAINT !
    Read the Petzold.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Replies: 12
    Last Post: 12-08-2008, 07:13 PM
  3. Button Displaying Image
    By mrafcho001 in forum Windows Programming
    Replies: 4
    Last Post: 12-03-2005, 02:14 PM
  4. Pressing a button works sometimes
    By johny145 in forum Windows Programming
    Replies: 14
    Last Post: 05-18-2005, 11:53 AM
  5. Disabling "X" button on Window
    By Boomba in forum Windows Programming
    Replies: 3
    Last Post: 06-14-2003, 01:53 PM