Kind, Uwe wrote:
Just so I understand what you are trying to do. Do you mean the change in the menu colour (to blue) when the mouse moves over the menu buttons

I want to 'grey out' the text when $mw loses the focus.

The structures used by a toolbar and header are different

This appears to be key. It looks like the toolbar that uses the new NMTBCUSTOMDRAW structure (comctl32.dll > 4.70) ignores colors you set in the DC. This behaviour is documented for ListViews ... but it appears not for toolbars.

Setting the clrText member of the new structure seems to work a treat.

I've tucked this code away to add to Coolbar.pm when I next get back to it.

Regards,
Rob.

#!perl -w
use strict;
use warnings;

use Win32::GUI 1.02;
use Win32::API;

sub I_IMAGENONE()         {-2};
sub NM_CUSTOMDRAW()       {-12};
sub WM_NOTIFY()           {0x4E};
sub CDDS_PREPAINT()       {1};
sub CDDS_ITEMPREPAINT()   {65537};
sub CDRF_NOTIFYITEMDRAW() {32};
sub CDRF_DODEFAULT()      {0};
sub CDRF_NEWFONT()        {2};
sub RDW_INVALIDATE()      {1};

# Win32::API call that we want, defined the old way for maximum
# backwards compatability with Win32::API
my $CopyMemory =
        Win32::API->new("kernel32", "RtlMoveMemory", "NPI", "V")
          or die "Can't find CopyMemory";

my $mw = Win32::GUI::Window->new(
        -name =>  'MainWindow',
        -text =>  'CustomDraw',
        -pos  =>  [ 100, 100 ],
        -size =>  [ 300, 350 ],

        -onActivate   =>
          sub { $_[0]->Toolbar->Redraw(RDW_INVALIDATE); return 1; },
        -onDeactivate =>
          sub { $_[0]->Toolbar->Redraw(RDW_INVALIDATE); return 1; },
        -onTerminate  =>  sub { -1 },
);

my $tb = $mw -> AddToolbar(
        -name      =>  'Toolbar',
        -nodivider =>  1,
        -flat      =>  1,
        -list      =>  1,
);

for ( 0 .. 3 )
{
    $tb->AddString("Test $_");
    $tb->AddButtons( 1,
        I_IMAGENONE,
        1000 + $_,
        TBSTATE_ENABLED,
        TBSTYLE_AUTOSIZE|TBSTYLE_BUTTON|BTNS_SHOWTEXT,
        $_ );
}
$tb -> Hook ( NM_CUSTOMDRAW, \&onCustumDraw );

$mw->Show();
Win32::GUI::Dialog();
exit(0);

sub onCustumDraw
{
    my ($object, $wparam, $lparam, $message_type, $message_code ) = @_;

    return unless $message_type == WM_NOTIFY;
    return unless $message_code == NM_CUSTOMDRAW;

    my $pNMTBCD = pack (  'L!', $lparam );
    my $sNMTBCD = unpack( 'P20', $pNMTBCD );
    my ($dwDrawStage, $dc ) = unpack ( 'x12IL', $sNMTBCD );

    if ( $dwDrawStage == CDDS_PREPAINT ) {
        $object->Result(CDRF_NOTIFYITEMDRAW);
    }
    elsif ($dwDrawStage==CDDS_ITEMPREPAINT) {
        if($mw->{-handle} != Win32::GUI::GetForegroundWindow()) {
                my $clrText=pack("I",0x808080);
                $CopyMemory->Call($lparam+60, $clrText, 4);
        }
        $object->Result(CDRF_DODEFAULT);
    }
    else {
        $object->Result(CDRF_DODEFAULT);
    }

    return 0;
}

Reply via email to