5

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

smit
  • 959
  • 1
  • 9
  • 20
  • 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. – smit Jan 01 '12 at 21:10
  • This is driving me crazy...how the he** in this world I can enable visual styles for simple non dialog based controls??????????????????? – smit Jan 01 '12 at 21:16
  • 1
    possible duplicate of [Programming In C + Win API: How To Get Windows 7 Look For Controls?](http://stackoverflow.com/questions/5663091/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. – Cody Gray - on strike Jan 01 '12 at 22:26
  • 1
    Make sure that you call [`InitCommonControlsEx`](http://msdn.microsoft.com/en-us/library/windows/desktop/bb775697.aspx) *before* you create your edit control window. – Cody Gray - on strike Jan 01 '12 at 22:27
  • @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. – smit Jan 02 '12 at 11:55
  • 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. – Cody Gray - on strike Jan 03 '12 at 02:11
  • 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. – smit Jan 03 '12 at 18:45

2 Answers2

8

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

flumpb
  • 1,707
  • 3
  • 16
  • 33
  • Embeding manifest worked for dialog based applications but for my simple win32 application no change. – smit Jan 01 '12 at 20:54
  • 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. – smit Jan 03 '12 at 19:04
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").

Fredrik
  • 5,759
  • 2
  • 26
  • 32
  • 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. – smit Jan 01 '12 at 20:14
  • 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. – Fredrik Jan 01 '12 at 20:17
  • By animated border i mean the hover effect in windows vista+ – smit Jan 01 '12 at 21:13
  • figured, just haven't seen it: -) – Fredrik Jan 02 '12 at 08:53