CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Location
    Spain
    Posts
    335

    How can I place an Small Icon in a button ?

    Hi,

    I have tried with SetIcon to put an small icon ( 16x16) on a button, but the problem, is that it puts there as a big icon, mmm..., and the problem is that seticon with parameter for big or small icons are in the CWindow class not in the CButton, so..., what can I do ?, I do:

    m_pbWrite.SetIcon(AfxGetApp()->LoadIcon(IDI_WRITEU),FALSE);

    Thanks, Bye !


  2. #2
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: How can I place an Small Icon in a button ?

    I'm afraid the only way I know of is to subclass the button and paint the icon on it yourself. To do this, take the following steps:

    1. Remove the existing call to SetIcon() for this button, but leave the Icon style. If this button is in the dialogue resource and you have it mapped to a member variable of type CButton, REMOVE this mapping. You will be able to refer to the button via its subclassed control.

    2. From the ClassWizard, derive a new class based on CButton. Call it CSmallIconButton, for example.

    3. In this new class, override the OnPaint() method (WM_PAINT message) and put the following:

    ---
    CClientDC *pDC ;
    CRect rctClient ;
    HICON hIcon ;
    int iLeft, iTop ;

    CButton::OnPaint(); // Paint a normal button.

    GetClientRect(&rctClient);
    iLeft = (rctClient.Width() - 16) / 2 ;
    iTop = (rctClient.Height() - 16) / 2 ;

    hIcon = AfxGetApp()->LoadIcon(IDI_WRITEU);
    pDC = new CClientDC(this);
    :rawIconEx(pDC->GetSafeHdc(), iLeft, iTop, hIcon, 16, 16, 0, NULL, 0);
    delete pDC ;


    ---

    4. In your dialogue class' header file, add a private member variable:

    ---
    .
    .
    .
    private:
    CSmallIconButton m_btnWrite ;
    .
    .
    .


    ---

    5. In your dialogue class' code file, in OnInitDialog(), add this line:

    ---
    m_btnWrite.SubclassDlgItem(IDC_WRITE, this);


    ---

    (where IDC_WRITE is the ID of your button). If your button is created at run time rather than already present in the dialogue resource script, then this line should come after the button is created (which should be done as normal).


    You can now refer to your button as m_btnWrite (object) rather than m_pbWrite (pointer to object); so you may need to change all references to m_pbWrite.

    Note that this always draws the icon in the same place on the button, even when the button is pressed; this will look slightly odd. If you want to cure that, you will have to trap the mouse down and up events and use a BOOL member variable to track the button pressed state. If the button is pressed, adjust the iLeft and iTop parameters by 1 or 2 to the right and down to match the button change. If you want further help with that, let me know at [email protected]

    Does this help?

    --
    Jason Teagle
    [email protected]

  3. #3
    Join Date
    May 1999
    Location
    Spain
    Posts
    335

    Thanks !

    Hi !,

    Thanks very much for your code, looks very interesting ( for me is very good because lets me learn a lot about owner draw and this "nasty" ;-) things).

    I have just tried one "****" trick, and it´s to use a 32x32 Icon, but putted centered on it the 16x16 icon, it works ( put the small icon in the small button right), but I think that is not a clean solution..., I will try with yours, Thanks, Bye !
    Braulio


  4. #4
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Thanks !

    There's nothing particularly 'dirty' about your method - go with whatever works. It's not like you're hacking the code.

    --
    Jason Teagle
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured