Why my edit control looks odd in my win32 c++ application using no MFC?
Asked Answered
K

2

6

I have this program where i created a window and inside that i added an edit control using plain C (no MFC or dialogs), the edit control creation code is as

hWnd=::CreateWindowExA(NULL,      //no extended style
                     "EDIT",     
                      NULL,           //no title       
                      WS_CHILD|WS_VISIBLE|WS_BORDER,      
                      x,          
                      y,        
                      Width,      
                      Height,    
                      hWndParent,
                      (HMENU)id,
                      (HINSTANCE) GetWindowLong(hWndParent, GWL_HINSTANCE),//the module instance
                      NULL);

But the rendered control looks ugly...

enter image description here


And here's what i want my controls to look like...

Cool vista themed edit control

I tried calling InitCommonControlsEx and included comctl32.lib but nothing changed.
I think adding an application manifest file describing all the dependencies would fix the problem but I don't know how to do that using Visual Studio 1010 IDE(I can't edit a manifest file myself)

Is it possible to get the normal vista style controls using just c/c++(no MFC or anything like .NET). If adding manifest resource would fix the problem then how can I write/generate one manifest file and add it to my exe?

#include<Windows.h>
#include <commctrl.h >
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib,"comctl32.lib")

HWND hwndEdit;
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp)
{
switch(uMsg)
{
case WM_CREATE:
    hwndEdit = CreateWindow( 
            "EDIT",     /* predefined class                  */ 
            NULL,       /* no window title                   */ 
            WS_CHILD | WS_VISIBLE | 
            ES_LEFT | ES_AUTOHSCROLL|WS_BORDER, 
            0, 0, 100, 50, /* set size in WM_SIZE message       */ 
            hWnd,       /* parent window                     */ 
            (HMENU) 1, /* edit control ID         */ 
            (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE), 
            NULL);                /* pointer not needed     */
    return 0;
    break;
case WM_CLOSE:
    ::PostQuitMessage(0);//quit application
    break;
default:
    return ::DefWindowProcA(hWnd,uMsg,wp,lp);
  }
 return 0l;
 }
 int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,char *cmd,int show)
 {
   INITCOMMONCONTROLSEX icc;
   icc.dwICC=ICC_ANIMATE_CLASS|ICC_NATIVEFNTCTL_CLASS|ICC_STANDARD_CLASSES;
   icc.dwSize=sizeof(icc);
   InitCommonControlsEx(&icc);
   char* tst="Simple edit control";

   WNDCLASSEX mywindow;
   MSG msg;
   HWND hwnd;
   mywindow.cbClsExtra=0;
   mywindow.cbWndExtra=0;
   mywindow.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
   mywindow.hCursor=LoadCursor(NULL,IDC_CROSS);
   mywindow.hIcon=LoadIcon(NULL,IDI_APPLICATION);
   mywindow.hInstance=hinstance;
   mywindow.lpfnWndProc=WndProc;
   mywindow.lpszClassName="Test";
   mywindow.lpszMenuName=NULL;
   mywindow.style=0;
   mywindow.cbSize=sizeof(WNDCLASSEX);
   mywindow.hIconSm=NULL;

if(!RegisterClassEx(&mywindow))
    MessageBox(NULL,"Window Registration failed","Error occured",NULL);

hwnd=CreateWindowEx(WS_EX_TOPMOST,"Test","My window",WS_OVERLAPPEDWINDOW,900,300,400,350,NULL,NULL,hinstance,tst);
if(hwnd==NULL)
    MessageBox(NULL,"Window creation failed","error",NULL);

::ShowWindow(hwnd,SW_SHOW);
::UpdateWindow(hwnd);

while (1)                  //NOTE: Game engine type message loop
{ Sleep(1);
    if ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) 
    {
        if (msg.message == WM_QUIT) 
            break;
        TranslateMessage( &msg );
        DispatchMessage ( &msg );

    } 
}
return msg.wParam;
}

UPDATE: I updated my project to use unicode charset/libraries and now visual styles are working except for the edit control...take a look..
enter image description here
I used styles for edit control WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL|ES_NOHIDESEL

Kershaw answered 1/1, 2012 at 20:3 Comment(7)
Ok i read the "Enabling Visual Styles" in msdn site and added the manifest then checked that in resource hacker...every went fine but still didn't get the expected result.Kershaw
This is driving me crazy...how the he** in this world I can enable visual styles for simple non dialog based controls???????????????????Kershaw
possible duplicate of Programming In C + Win API: How To Get Windows 7 Look For Controls?. My answer there provides a simpler method of embedding a manifest into your application by getting the linker to do it for you. No resource editor required.Oralla
Make sure that you call InitCommonControlsEx before you create your edit control window.Oralla
@Cody Gray I added the code now, please take a look at it and point out if anything is wrong. I am using the exact same code and visual studio 2010 ultimate.Kershaw
The first question that comes to mind is why you're compiling without UNICODE defined. You're explicitly calling CreateWindowA and DefWindowProcA, which is the ANSI (non-Unicode) version of the function, which is very strange. Windows applications have been fully Unicode for over a decade. If you're going to eschew the use of macros and call specific versions of functions, always call the W versions. This also means that you need to use wide character strings, either wchar_t or TCHAR, rather than char. I suspect that visual styles requires a Unicode application.Oralla
I also changed the font of the edit control...look at the pic, other controls are rendered perfectly..but this edit control is behaving strangely.Kershaw
H
9

Enabling Visual Styles: http://msdn.microsoft.com/en-us/library/bb773175.aspx

Honeydew answered 1/1, 2012 at 20:10 Comment(2)
Embeding manifest worked for dialog based applications but for my simple win32 application no change.Kershaw
I was wrong...manifest did work correctly, but I don't know what's wrong with the particular edit control, today as I tried displaying a button then I knew that the problem is not with manifestation but something else.Kershaw
F
4

It was a long time since I was doing Win32 GUI stuff but as I remember it you should use WS_EX_CLIENTEDGE and not zero as an extended style (at least the sunken 3d effect, not sure what you mean with the "Animated border").

Frag answered 1/1, 2012 at 20:10 Comment(4)
I tried that too but just got a sunken edge that looks even more uglier with the yellow background, removing this style I got a flat edit box with black border....relatively better.Kershaw
oh yes, on a yellow background it will look quite ugly by default: -) I assumed the choice of background was to highlight the flat look.Frag
By animated border i mean the hover effect in windows vista+Kershaw
figured, just haven't seen it: -)Frag

© 2022 - 2024 — McMap. All rights reserved.