Brandon
Brandon

Reputation: 23500

WINAPI Button background

In WINAPI, I create a button like:

case WM_CREATE:
{
    Start = CreateWindowEx(WS_EX_TRANSPARENT, "Button", "Start", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20, 50, 75, 25, window, (HMENU)ID_START, hInstance, NULL);
            break;
}

The button looks like: enter image description here

But I need it to look like this one (which I did in .Net):

enter image description here

How can I get rid of that black border/background?

Upvotes: 0

Views: 2434

Answers (4)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145269

EDIT: I mis-identified the border as the default-button border. As David Hefferman pointed out, it's not. That is, I have now reproduced that border effect; when the program starts there is no border, but just a little mousing over the left button creates the border – at least in Windows 7.

Original answer follows:


The black border is because it's default. You shouldn't interfere with the system's visualization of default buttons etc. Users rely on those cues.

To change the font, maybe, like, WM_SETFONT?

Disclaimer: I haven't tried that. It might be you need to handle WM_CTRLCOLOR or something like that. Just try it out, and in the end, if the default buttons don't cut it for you, simply implement your own button.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941465

  Start = CreateWindowEx(WS_EX_TRANSPARENT, ...);

You got the black outline because you used the WS_EX_TRANSPARENT style flag. It isn't clear why you used it, there's not much you can do with it when you use the Button control. It is otherwise probably the least understood style flag. Pass 0 instead to get a normal looking button.

It is otherwise a lost cause to get the exact look of a .NET button, Winforms doesn't use the built-in Button control. It creates its own, using a custom renderer to get the gradient look. Reproducing that native code is a lot of work.

Upvotes: 3

Blue Wizard
Blue Wizard

Reputation: 171

I haven't tested this, but try creating the button like this:

Start = CreateWindowEx(0, WC_BUTTON, "Start", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT, 20, 50, 75, 25, window, (HMENU)ID_START, hInstance, NULL);
        break;

It also might depend on how you've created your main window. Can you post the code for your main window creation?

Upvotes: 0

LarryF
LarryF

Reputation: 5083

I think the border you are seeing is because the button has the "Default Button" property. If you turn that off, then it will have a normal border. The Default property just tells Win32 which button to activate if the users hits ENTER on the dialog/form. If you only have one button, then it will always have the default property. If you add a second, it will not.

The property is the BS_DEFPUSHBUTTON property, so in your CreateWindowEx call, you should be able to do something like:

Start = CreateWindowEx(WS_EX_TRANSPARENT, "Button", "Start", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | ~BS_DEFPUSHBUTTON, 20, 50, 75, 25, window, (HMENU)ID_START, hInstance, NULL);

If not, you'll have to set it with ModifyStyle or ModifyStyleEx, and pass it in the "Remove" parameter. I forget which one the specific styles have to be passed in, but if I recall correctly, it's the normal style params, NOT the EX params.

Upvotes: 0

Related Questions