0

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?

4 Answers 4

3
  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.

4
  • Can you explain how WS_EX_TRANSPARENT changes the appearance of the border? I didn't think it would have any affect on the appearance at all. Have to agree with you on it being misunderstood. Commented Dec 27, 2012 at 20:37
  • No idea, never considered trying this until a half hour ago. It looks like it skips WM_ERASEBKGND, not unusual for transparency simulation. Commented Dec 27, 2012 at 20:57
  • You're right. When I remove the transparent flag, it puts a grey border instead.. I made it transparent so that it won't be grey like in this image: i.imgur.com/sgcE4.png
    – Brandon
    Commented Dec 27, 2012 at 21:05
  • Well, you found out .NET doesn't use it. Why it doesn't implement WM_CTLCOLORBTN either is another one of life's mysteries I long ago put in the forget-about-it box. Raw user32 is just not productive. If you don't want to use the battleship-gray dialog background then you'll need owner-draw. Commented Dec 27, 2012 at 22:00
0

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.

0
0

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?

0

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.

2
  • No, the black border is there because of the non-default extended window style Commented Dec 28, 2012 at 7:58
  • @DavidHeffernan: it appears you're right in general. however, as demonstrated here the black border doesn't appear until one mouses over the button, or click the other button (maybe also other ways). so it seems to be a bug. i misidentified it as a the default button border. Commented Dec 28, 2012 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.