CPB Mailing List

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Which button am I?



Here's the senerio:
The user selects "Setup" from the main program, to assign a personal launch
command to a predesigned BitBtn, for his own use. The setup dialog opens,
he enters (in sequence) strings for items, such as commandline, parameters,
and the buttons caption. Then selects which button to assign it to from a
duplicate toolbar (just like the one he'll use in the program). I can only
let him run ExtractAssociatedIcon() once or it screws up, so first I define
int stopit, then test stopit, then at the end of the function, I increment
stopit so it doesn't work twice. The user has to finish or cancel and
re-run setup to repeat it for the next button.

I have two problems:
Problem 1 : The compiler warns me that stopit is declared but not used.
(???)
Problem 2 : I need to run this function from 20 buttons that the user can
select from. So far I haven't been able to determine which button was
selected, so for now I have 20 identical functions like the one below. Oh,
it works, but it's bulky, and one of the star features of CBuilder is
REUSE. (I also get 20 warnings about stopit.)

NOTE: When the dialog is created, any existing assigned buttons are loaded
so the user can see whats available or change an existing assignment. The
finish button loads the programs BitBtn and strings and saves the data to
the registry.
<<Code views best at full screen>>

void __fastcall TCustomSetup::SUCust1Click(TObject *Sender)
{
int stopit = 1;
if (stopit == 1)
	{
	try
		{
		if(AppPath->Text != "")
			{
			SUPath = AppPath->Text;			//TEdit
			SUParams = AppParams->Text;		//TEdit
			SUCaption = SetCaption->Text;	//TEdit
			/* disable all buttons */
				{
				AnsiString nameString("TBitBtn");
				TBitBtn * btn;
				for(int i=0; i < ComponentCount; i++)
					{
					/*Check to see if the component is a TBitBtn*/
					if (Components[i]->ClassNameIs(nameString))
						{
						//cast the component to a TBitBtn *
						btn = (TBitBtn *)Components[i];
						if (btn->Tag == 5)/* the SUCustBitBtns are tagged as 5 */ 
							{
							btn->Enabled = false;/*re-enable just the one selected*/
							}
						}
					}
				}
			/* continue to process button setup */
			WORD ico = 0;//first icon found
			LPSTR PathName = AppPath->Text.c_str();/* assigned from editbox */
			HICON GrabbedIcon;/* void pointer to ico */
			GrabbedIcon = ExtractAssociatedIcon(Application->Handle, PathName, &ico);
			if (GrabbedIcon > 0)
				{
				Image1->Picture->Icon->Handle = GrabbedIcon;
				
				/* I save it and load it + all the strings when the program starts */
				Image1->Picture->SaveToFile("custom1.ico");

				try
					{
					Timer1->Enabled = true;
					SUCust1->Enabled = true;
					SUCust1->Glyph->Canvas->Draw(0, 0, Image1->Picture->Icon);
					SUCust1->Caption = SetCaption->Text;
					} /* end of try to draw on button */
				catch (...)
					{
					MessageBox(0, "No icon available.", "NOTICE", MB_OK);
					SUCust1->Enabled = true;
					SUCust1->Glyph = "default.bmp";
					SUCust1->Caption = SetCaption->Text;
					}
				} /*end of if(GrabbedIcon) */
			} /*end of if(AppPath != "") for entire statement */
		else
			{
			MessageBox(0,"First select a program to assign.", "ERROR", MB_OK);
			}
		}/* end of try for entire statement */
	catch (...)
		{
		MessageBox(0, "Doesnt this work?", "ERROR", MB_OK);
		}
	}
++ stopit;
}


COMMENT:  If you're going to test this code you also need to increase the BitBtn's canvas so icons will fit on them, and the buttons are sized at 39x125 with the glyph at the left, so you can have up to 20 chars in the caption:

void __fastcall TCustomSetup::FormCreate(TObject *Sender)
{
/*set SCCust (TBitBtns) glyph canvas size	*/			{
	AnsiString nameString("TBitBtn");
	TBitBtn * btn;
	for(int i=0; i < ComponentCount; i++)
		{
		/*Check to see if the component is a TBitBtn	*/
		if (Components[i]->ClassNameIs(nameString))
			{
			/*cast the component to a TBitBtn *	*/
			btn = (TBitBtn *)Components[i];
			if (btn->Tag == 5)/*check TBitBtn Tag property	*/
				{
				btn->Glyph->Height = 32;
				btn->Glyph->Width = 32;
				btn->Glyph->Canvas->Rectangle(0,0,32,32);
				}
			}
		}
	}
}


Rick Malik
rmfci@pacbell.net
"Visit the C++ Builder Hobby Pages"
http://home.pacbell.net/rmfci/bcbhp.htm


W Komornicki's Home Page | Main Index | Thread Index