C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[807] [Controls] Focus될때 자동 Checked되지 않는 RadioButton
장성호 [nasilso] 7209 읽음    2008-09-24 16:37
어제 델마당에 올라온 Q&A를 보고 테스트 해보았었는데..

TRadioButton은 사용자가 Click하지 않았는데
그냥 WM_SETFOCUS 메세지만 받으면 버튼이 Checked되어버린다.

예전에는 핸들이 있는 컨트롤을 많이 썼기때문에 이런 현상이 별로 문제될 상황이 나오지 않았었던것 같은데
요즘은 스킨에 신경을 쓰다보니 Button같은것도 Graphic컨트롤을 이용해서 만드는경우가 많아져 그런것 같다

[상황설명]
1. RadioButton에 Focus가 있고 , 체크된 상황에서  이미지를 클릭하여 다음코드를 실행했다.
//
void __fastcall TForm1::Image1Click(TObject *Sender)
{
    //상황2
    RadioButton1->Checked =false;                                           

    //상황3
    ShowMessage("메세지 박스를 닫으면 RadioButton Check 가 어떻게 될까?"); 

   //상황4 
   // ...
}



2.  상황2 에서 RadioButton은 unChecked된다.
3.  상황3 에서 Focus는 MessagBox로 가게 된다.
4.  사황4 에서 메세지 박스가 닫히면서 Focus는 Form1에서 맨마지막에 Focus가 가 있던 컨트롤인
              RadioButton1로 오게 된다.
    이때 RadioButton이 다시 Checked되어버린다.


델마당에 질문자는 상황4에서 포커스가 다시 RadioButton으로 가더라도 버튼이 Checked되지 않았으면 원했다.


TRadioButton의 Message를  Spy해 보니 이런 현상은 win32 RadioButton의 기본기능인것 같다.

그래서 TRadioButton을 교체식컨트롤로 간단히 좀 수정하여 만들어 보았다.
원리는 마우스 메세지(WM_LBUTTONDOWN)이 있는후에 WM_SETFOCUS가 오면 통과하고
마우스 메세지(WM_LBUTTONDOWN) 없이  WM_SETFOCUS가 오면
포커스는 주되 RadioButton이 체크는 되지 않게 하였다.


//C++Builder에서

class TRadioButton : public  Stdctrls::TRadioButton
{
private:
    bool bMouseBtn;
    void __fastcall WndProc( TMessage &Msg);
};

#define  TRadioButton ::TRadioButton

//....

void __fastcall TRadioButton::WndProc( TMessage &Msg)
{
    if( Msg.Msg==WM_LBUTTONDOWN ) bMouseBtn=true;  //마우스 이벤트가 있을때 플래그를 설정해둠
    if( (Msg.Msg==CN_COMMAND) && (!bMouseBtn))
    {
        bMouseBtn=false;            //플래그가 설정되어있지 않으면 그냥 끝내버림
        return;
    }
    if( Msg.Msg==BM_SETCHECK )  bMouseBtn=false;
    TButtonControl::WndProc(Msg);
}




//Delphi 
type
  TRadioButton = class(StdCtrls.TRadioButton)
  private
    bMouseBtn: boolean;
  public
    procedure WndProc(var Message: TMessage); override;
  end;

implementation

procedure TRadioButton.WndProc(var Message: TMessage);
begin

  if Message.Msg=WM_LBUTTONDOWN then bMouseBtn:=true;  //마우스 이벤트가 있을때 플래그를 설정해둠
  if (Message.Msg=CN_COMMAND) and (not bMouseBtn) then
  begin
    bMouseBtn:=false;            //플래그가 설정되어있지 않으면 그냥 끝내버림
    Exit;
  end;

  if Message.Msg=BM_SETCHECK then  bMouseBtn:=false;

  inherited;
end;



이게 어디 쓸일이 있을지 모르겟지만, 정리해놓지 않으면 잊어버릴것 같아서 ....
혹누군가에게 유용하게 쓰여지길 기대하며...


그럼..

+ -

관련 글 리스트
807 [Controls] Focus될때 자동 Checked되지 않는 RadioButton 장성호 7209 2008/09/24
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.