求教:TRadioGroup不单击时会触发OnClick事件

anhuishengshuchengxi 2017-11-21 08:00:18
现象是这样:
设置了TRadioGroup的OnClick事件,在单击改变选项时会触发,在单击不改变时不触发。另外在其他函数中改变TRadioGroup的ItemIndex时也会触发OnClick事件。

想要的效果是:
只有单击时触发此函数,这个单击不管有没有改变选项。

有这个事件吗?求指导,谢谢。
...全文
693 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
好的,谢谢指导,通用的确很重要。
ooolinux 2017-11-26
  • 打赏
  • 举报
回复
楼主如果想用TRadioGroup,可以查看一下TRadioGroup和TCustomRadioGroup的VCL源代码,说不定有办法。不过感觉依赖于VCL实现,不够通用(不是通过函数或方法)。
hongss 2017-11-25
  • 打赏
  • 举报
回复
ooolinux 2017-11-24
  • 打赏
  • 举报
回复
消息截获是这个意思,本来鼠标点击RadioGroup会选中一项,打个比方,总经理(操作系统)发消息(鼠标在客户区点击)给小王(RadioGroup),我这里简化了,实际上消息要从线程消息队列由消息循环获得以后分派,小王(RadioGroup)就会按照消息有相应的动作,但是部门经理提前把消息截获了,Handled=true; //该鼠标消息已处理 自己把消息处理了不再下发,小王(RadioGroup)根本没有收到消息,自己就不会有动作(这就是消息驱动),部门经理根据消息自己做了一些事,比如我代码中的:obj->Checked=true; //选中该选项 ,并且指挥下属做事,比如:RadioButton1Click(obj); //调用选项点击函数
ooolinux 2017-11-23
  • 打赏
  • 举报
回复
TRadioGroup的ClientRect应该是有的,你可以单独用代码测试一下。
ooolinux 2017-11-23
  • 打赏
  • 举报
回复
如果用RadioGroup,没办法用ControlAtPos这个方法了,我不知道有没有哪个方法可以获取RadioGroup内鼠标点击位置所在的是哪一个项目,手动写代码计算也许有可能,因为RadioGroup的Height、Items->Count都是可以得到,通过一些除法计算可以得到每一个项目的位置。
  • 打赏
  • 举报
回复
谢谢,单步调的时候就是发现point的值有问题,忽略了调用对象,修改后都能进入,这就回到了12#您所说的问题了。 我想简单了,以为单击后会修改ItemIndex,之后直接调用OnClick函数判断下就好了。 看样子是不行,谢谢指导。
ooolinux 2017-11-23
  • 打赏
  • 举报
回复
你可能ClientRect和BoundsRect都没分清楚, ClientRect是(0,0)—(x,y) BoundsRect是相对于窗体左上角而言的,就是(Left,Top)—(x+Left,y+Top) 使用哪个主要看对哪个控件或窗体调用ScreenToClient得到的鼠标坐标。 你用RadioGroup,如何判断鼠标点击的位置,是在原来选中的项目上,还是在其它项目上? 我那个代码: RadioButton1Click(obj); 这个 obj 已经代表是(指向)哪一个RadioButton了,就是鼠标位置所在的那个RadioButton,作为参数传给Sender: void __fastcall TForm1::RadioButton1Click(TObject *Sender) { TRadioButton *obj=(TRadioButton *)Sender; Memo1->Lines->Add(IntToStr(obj->Tag)+"."+obj->Caption); } 该函数中得到这个参数,能够区分是哪一个RadioButton。
  • 打赏
  • 举报
回复
谢谢指导,本来以为判断鼠标区域后直接调用OnClick函数,判断ItemIndex就行的,测试后: 调用的两个方法都无法判断区域; 直接调用OnClick函数,ItemIndex并没有修改为鼠标所在位置。 if(PtInRect(RadioGroup1->BoundsRect, point)) //鼠标在RadioGroup客户区内 { ………………测试后无法进入 Memo1->Lines->Add("PointOnClick bounds"); Handled = true; } if(PtInRect(RadioGroup1->ClientRect, point)) //鼠标在RadioGroup客户区内 { ………………测试后无法进入 将判断语句注释掉,直接进入,调用OnClick,发现ItemIndex没有变化 //获取鼠标位置所在的控件 RadioGroup1Click(RadioGroup1); //调用选项点击函数 Memo1->Lines->Add("PointOnClick client"); //int i = RadioGroup1->ItemIndex; //RadioGroup1->ItemIndex = i; Handled=true; //该鼠标消息已处理 }
ooolinux 2017-11-23
  • 打赏
  • 举报
回复
//几个RadioButton复用此事件处理函数 void __fastcall TForm1::RadioButton1Click(TObject *Sender) 这个代码里应该不必要。
  • 打赏
  • 举报
回复
谢谢,好多内容,好好学习下。 关于代码中, if(PtInRect(GroupBox1->ClientRect,point)) //鼠标在GroupBox客户区内 我想用于TRadioGroup,发现调用ClientRect或BoundsRect,都不行,可以解决吗? 实际应用中,一个个放RadioButton,如果数目多起来,没有RadioGroup方便哈……
ooolinux 2017-11-22
  • 打赏
  • 举报
回复
(TRadioButton *) 改成dynamic_cast<TRadioButton *>( )比较好。 简单点的方法看看别的前辈有没有。 如果对截获消息不熟悉,可以看看我的博客,转载了好几篇,应该比较完整了:http://blog.163.com/tab_98/ C++ Builder区 《C++ Builder高级应用开发指南》这本书也可以参考。
ooolinux 2017-11-22
  • 打赏
  • 举报
回复
截获消息也不算麻烦,我开始用中游的 void __fastcall TForm1::WndProc(TMessage &Message) 发现无法截获鼠标消息,改用上游的TApplication::OnMessage 事件处理函数可以截获,窗体上放一个TApplicationEvents组件就可以了, 主要代码(使用GroupBox里面放置单独的RadioButton,另外一个Memo1):
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
//几个RadioButton复用此事件处理函数
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
    TRadioButton *obj=(TRadioButton *)Sender;
    Memo1->Lines->Add(IntToStr(obj->Tag)+"."+obj->Caption);    
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,
      bool &Handled)
{
    if(Msg.message==WM_LBUTTONUP) //鼠标左键UP消息
    {
        TPoint point=GroupBox1->ScreenToClient(Msg.pt); //鼠标相对于GroupBox客户区坐标
        if(PtInRect(GroupBox1->ClientRect,point)) //鼠标在GroupBox客户区内
        {
            //获取鼠标位置所在的控件
            TRadioButton *obj=(TRadioButton *)GroupBox1->ControlAtPos(point,true,true);
            if(obj)
            {
                obj->OnClick=NULL;
                obj->Checked=true; //选中该选项
            //    obj->OnClick=RadioButton1Click;
                RadioButton1Click(obj); //调用选项点击函数
            }
            Handled=true; //该鼠标消息已处理
        }
    }
}
//---------------------------------------------------------------------------

  • 打赏
  • 举报
回复
在其他函数内修改属性: 是想避免触发OnClick事件,试过设为NULL,再恢复的方法,能够实现。 选中后再次单击: (报文通讯时,想重复发送该报文) 截获鼠标坐标就是麻烦了点,理论上是可以实现。 谢谢u010165006的解答,想等等看看有没有简单点的方法。
ooolinux 2017-11-22
  • 打赏
  • 举报
回复
如果用RadioGroup,替换它的WindowProc应该也可以。 不过截获消息以后,可能需要用代码来选中一个选项,如果用GroupBox加RadioButton的话,可以用ControlAtPos方法找到鼠标所在的子控件(某个RadioButton),把它Check。 如果用代码把Checked属性设置为true时不想触发OnClick,可以这样: RadioButton1->OnClick=NULL; RadioButton1->Checked=true; RadioButton1->OnClick=RadioButton1Click;
ooolinux 2017-11-22
  • 打赏
  • 举报
回复
1)在其他函数内使某个RadioButton的Checked属性值为true时,会触发OnClick函数。 —————— 这个是你要的,还是不要的?
ooolinux 2017-11-22
  • 打赏
  • 举报
回复
不知道你为什么要选中以后再次单击触发OnClick事件? 非要这么做应该也可以,麻烦一点,就是窗体截获鼠标事件消息,判断鼠标位置如果在RadioGroup1->BoundsRect内,调用RadioGroup1Click函数,否则,传递消息获得默认处理。
  • 打赏
  • 举报
回复
这样的效果和TRadioGroup貌似一样哎。 1)在其他函数内使某个RadioButton的Checked属性值为true时,会触发OnClick函数。 2)RadioButton被选中,再次单击,不会触发OnClick函数。
ooolinux 2017-11-21
  • 打赏
  • 举报
回复
如果用2楼这样做,只要设置一个RadioButton1的OnClick事件处理函数:
void __fastcall TForm1::RadioButton1Click(TObject *Sender)
{
    int tag;
    tag=((TRadioButton *)Sender)->Tag;
    switch(tag)
    {
    }
}
RadioButton2、RadioButton3等的OnClick都选为下拉的RadioButton1Click,就是复用RadioButton1Click事件处理函数, 几个RadioButton的Tag属性事先设置为不同的值就可以了。 Sender代表实际触发事件(比如点击事件)的那个控件。
ooolinux 2017-11-21
  • 打赏
  • 举报
回复
或者你用GroupBox里面放置单独的RadioButton来代替RadioGroup,后面多放一个没用的RadioButton,Checked属性设置为true,Visible属性设置为false。
加载更多回复(1)
ListView 排序 Stringgrid内使用回车键代替Tab键 TListBox内显示分栏 TListBox每一行显示交互的颜色 TMemo内光标位置根据鼠标移动 TMemo自动卷动 TRichEdit卷到特定位置 TRxRichEdit内插入图像 TStringGrid中插入、删除一行 TStringGrid保存和装载 TTreeview控件显示粗体节点 TWebBrowser中调用“查找”对话框 为Listview栏添加双击事件 为工具栏的TToolButton设置新的索引 仅通过Classname创建和管理任意窗体 从RichEdit取Rtf格式 从TListBox中拖放项目到TRichEdit 从一个TRichedit复制格式Rtf文本到其它 使用CustomSort方法排序TListView 使用DBGrid字段队列同步列标题队列 使用Interfaces和TInterfaceList 使用TRichEdit存储大于64K数据 使用代码移动StringGrid的行和列 使用圆形角显示控件 使用类名显示窗体 保存 装载TCheckListbox值 保存和装载TListView 保存和装载TTreeView 允许TDBGrid栏调整大小但防止移动 克隆控件 克隆窗体 列出控件的所有属性和事件 创建可编辑ListBox 删除Listbox的所有选定项目 删除stringlist中重复项目 删除TStringGrid中的列 动画窗体 取ComboBox List句柄 取TListview内所有选定项目 取TShellListView中选定文件的路径 取列举值的名称 在combobox中实现autocomplete 在DBGrid中使用回车键改变到下一个段 在listbox内列出所有目录、文件和驱动器 在Listbox内显示水平滚动条 在memo中实现UNDO 在RichEdit内搜索文本并选择它 在StringGrid内使用Combobox作为编辑器 在StringGrid单元中画不同颜色 在TComboBox中显示栏 在TComboBox内绘制位图 在TDateTimePicker内显示星期几 在TFileListBox内显示多列 在TListBox中创建彩色项目 在TListBox内拖拽 在TListbox内绘制位图 在TListview内执行二进制搜索 在TListView内拖拽多个项目 在TListView列中创建进度条 在TMemo内查看和编辑MS-DOS文本 在TPageControl拖拽Tabs 在TPrintDialog上放置定制控件 在TRichEdit内使用上标和下标 在TRichEdit内使用超链接 在TRichEdit内文本使用不同的下划线样式 在TRichedit内设置段落行距 在TStringGrid内删除一行 在TStringGrid实现OnColumnClick事件 在自己的控件显示信息 复制Listbox项目到剪贴板 失效TTreeview的tooltips 定制TDateTimePicker的格式 居中控件 屏蔽在EditBox内按回车键的都都声 强制在TEdit内输入 排序StringGrid 搜索和替换RichEdit中文本 搜索和选定TListBox的项目 改变TDBGrid的DefaultRowHeight 改变TRichEdit内选定文本的背景颜色 改变TStringGrid内选定单元的颜色 改变标准对话框 改变状态栏字体样式 改变窗体上所有控件的属性 改变进度条颜色 根据StringGrid列内容自动调整大小 根据列排序TStringGrid 检查Stringgrid中指定单元示范选定 检查TMemo能否取消操作 检查TreeView是否完全展开或折叠 检查TStringGrid是否有滚动条 添加接口对象到list 清空StringGrid的所有单元 移动listbox项目 移动TListView项目 移动TRichEdit内光标到指定位置 聚焦TDBGrid某些单元 自动打开TDateTimePicker 自定义Memo边界 获取TRichEdit中鼠标指针下面的字 访问TRadioGroup的控件 调整TComboBox下拉列表的宽度 转换Editbox的首字符为大写 转换TEdit中每个词的首字母为大写 输出TStringGrid到TListView 运行时创建TButtons队列 运行时创建控件 运行时创建菜单项 运行时替换控件 返回TTreeView内字符串路径 防止在TEdit内剪贴、复制、粘贴 防止用户调整TListView栏大小 限制TEdit的输入 隐藏TListView滚动条 隐藏最小化MDI子窗口 颜色Combo Box 验证TEdit中输入的是数字
Borland Delphi 2005 Architect Update 3----------Report #: Short Description: Rating: Status12241 IntToHex returns lowercase letters0.00 of 5 Closed11881 TStringStream constructor for .NET sets Position incorrectly0.00 of 5 Closed11787 Access violation using Code Insight when editing the uses clause0.00 of 5 Closed11759 TList instance leaked in DBCommon.pas when using IN operator in Filter expression5.00 of 5 Closed11651 New EllipsisPosition property causes text to be invisible & process to hang5.00 of 5 Closed11471 Cannot manage StarTeam association since applying Update20.00 of 5 Closed11353 (strict) protected nested class become public3.67 of 5 Closed11314 Too much inlining yields unstable compiled code.0.00 of 5 Closed11266 Inline causes compile error : F2084 Internal Error SCR765.00 of 5 Closed11193 Inlined function affects working of the enclosing for loop0.00 of 5 Closed11184 [Fatal Error] F2084 Internal Error: ILLK26340.00 of 5 Closed11069 Compact the TApplication.ProcessMessage code fragment for Unicode handling5.00 of 5 Closed10918 TColorBox control custom color selection causes AV0.00 of 5 Closed10873 Internal error: URW 793 when using constant struct with enums2.00 of 5 Closed10861 Access Violation when compiling code with default index property0.00 of 5 Closed10772 Designer improperly handles controls tagged with [ToolboxItem]0.00 of 5 Closed10669 TWideStrings has no GetEnumerator method0.00 of 5 Closed10661 Switching between form and source view cause long delay4.67 of 5 Closed10567 Ability to create all VCL packages0.00 of 5 Closed10498 D2005: DataSet lots of methods missing in code completion0.00 of 5 Closed10450 The Ellipse is drawn over some of the caption text when a form is docked in IDE0.00 of 5 Closed10381 Constant array of procedural types crashes compiler5.00 of 5 Closed10376 The Delphi 2k5 IDE UpTime & project loading is very SLOW4.00 of 5 Closed10314 Error Insight fails to flag an error4.00 of 5 Closed10248 Alignment Palette in VCL form designer malfunctions5.00 of 5 Closed10190 List view selected item no longer selected5.00 of 5 Closed10009 Double-click in the Search Results window0.00 of 5 Closed9912 Press Enter on Component crashes Delphi 20050.00 of 5 Closed9847 Transactional Object Wizard Missing0.00 of 5 Closed9712 The XML documentation feature doesn't associate comments with the correct symbols5.00 of 5 Closed9690 "Cannot focus a disabled or invisible window" in IDE0.00 of 5 Closed9666 Code Completion doesn't show all available items when using overload5.00 of 5 Closed9639 Active Form wizard not available in Delphi 2005 but mentioned in online help5.00 of 5 Closed9634 IDE response too SLOW4.63 of 5 Closed9578 TXPManifest + TFrame + TLabel + TRadioGroup = bad5.00 of 5 Closed9477 Delphi .NET assembly references with strong names5.00 of 5 Closed9254 Incorrect reference public key token included into Delphi8 strong-named assembly metadata4.00 of 5 Closed9021 Memory Leak in TreeView0.00 of 5 Closed8866 Assigning long string values (greater than 8192 characters) to TStringFields results in access violation.5.00 of 5 Closed8785 Filter or Ranges on Nested Dataset's should restrict master as well.0.00 of 5 Closed8417 TCustomSQLDataSet.GetFieldData implementation0.00 of 5 Closed8229 Error in code when database doesn't support schema/owner names5.00 of 5 Closed8032 Show modified code in the scrollbar0.00 of 5 Closed8021 TIndexDef.Assign Doesn't copy DescFiels and CaseInsFields5.00 of 5 Closed8001 SQLDataset doesn't call .Close on dbExpress driver cursor when closing5.00 of 5 Closed7912 TCustomClientdataset: properties "IndexDefs" and "IndexName" should be public0.00 of 5 Closed7872 double exception in method TDataSetProvider.InternalGetRecords5.00 of 5 Closed7809 SetOptionalParam don't work0.00 of 5 Closed7768 An Easy Feature Request: TDataset.CopyFields4.00 of 5 Closed7290 Grid's functionality enhancement5.00 of 5 Closed7199 Class Completion erroneously adds fields to a complete class if the getter functions starts with F5.00 of 5 Closed7082 Add SSE3 / PNI instructions to the BASM5.00 of 5 Closed7005 New control request - TDBStaticText - Add possibility to have Edge Borders0.00 of 5 Closed6985 Add node for unit name in Find in Files results in Message Window0.00 of 5 Closed6803 WebToolBar problem with buttons0.00 of 5 Closed6558 AV in DBGrid when destroying DataSet0.00 of 5 Closed6428 Labels disappear with XPManifest5.00 of 5 Closed6368 Resolving lookup- & calculated fields to datafields4.00 of 5 Closed6238 DBNavigator and DBGrid compliant with Federal Section 5080.00 of 5 Closed6021 Labels not visible in Windows XP0.00 of 5 Closed5951 Internal error: SY5764.44 of 5 Closed5812 TComboBox.Focused method does not always return good results.1.00 of 5 Closed5803 Unable to delete component if code editor minimized in saved desktop5.00 of 5 Closed5763 Cannot debug datamodule containing many objects0.00 of 5 Closed5699 Apache shared modules do not work with Apache 2.0.4x5.00 of 5 Closed5592 New event OnValidate that is fired before applying updates2.00 of 5 Closed5336 fatal error: Internal Error: L6812.33 of 5 Closed5283 Cannot read complete compiler error message.0.00 of 5 Closed5280 MessageDlg returns mrNone instead of mrCancel when aborted5.00 of 5 Closed5248 Assert fails to break and leaves FPU invalid op exception set.4.50 of 5 Closed4627 Cannot assign -2147483648 into INTEGER Variable4.00 of 5 Closed4624 A way to set both Width and Height of TBitmap (speed reason)4.85 of 5 Closed4581 Field description and Label3.00 of 5 Closed4444 IncMilliSeconds...IncHour with DateTime<0 wrong0.00 of 5 Closed4343 Proposal of modification of the TDataSet.Post method in DB.pas unit1.00 of 5 Closed4184 Delphi DLL using sharemem called from VC++ exe AV on exit3.25 of 5 Closed4172 tFrames and XPMan hiding controls5.00 of 5 Closed3850 TGraphicControl descendants invisible in frames with XP Manifest4.60 of 5 Closed3792 TryEncodeDateTime not correct for dates before Dec. 30, 18995.00 of 5 Closed3776 Add full IDL support0.00 of 5 Closed3718 Automatic Required setting of TField not consistent with SQL3.71 of 5 Closed3542 Strange limits in property CommandText2.40 of 5 Closed2881 DBGrid doesn't repaint when Enabled changed.5.00 of 5 Closed2625 Impossible to open a .TLB into the TLB Editor if missing uses reference3.50 of 5 Closed2608 Compiler crashes, dcc70.dll Read of address 000000004.00 of 5 Closed2572 Unable to sort on columns in module view3.00 of 5 Closed2382 TListView doesn't update correctly in OwnerData mode.4.00 of 5 Closed2276 'deprecated' warning appears when it shouldn't4.31 of 5 Closed2258 Failure to parse/compile integer constant --2147483648 and lower4.86 of 5 Closed2236 Delphi converts Text DFM's to binary with these steps4.71 of 5 Closed1685 Compiler error message refers to HIGH when it should be LOW3.43 of 5 Closed1455 GetRange method3.38 of 5 Closed1209 Context help does not work for TComboboxEx3.67 of 5 Closed1177 ResourceString limitation of 1024 characters3.89 of 5 Closed(出处:www.borland.com)

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧