How to create a Windows-style text box in a C ++ Win32 application

I tried this:

#include <windows.h> #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow) { LPTSTR windowClass = TEXT("WinApp"); LPTSTR windowTitle = TEXT("Windows Application"); WNDCLASSEX wcex; wcex.cbClsExtra = 0; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbWndExtra = 0; wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wcex.hInstance = hInstance; wcex.lpfnWndProc = WndProc; wcex.lpszClassName = windowClass; wcex.lpszMenuName = NULL; wcex.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, TEXT("RegisterClassEx Failed!"), TEXT("Error"), MB_ICONERROR); return EXIT_FAILURE; } HWND hWnd; if (!(hWnd = CreateWindow(windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL))) { MessageBox(NULL, TEXT("CreateWindow Failed!"), TEXT("Error"), MB_ICONERROR); return EXIT_FAILURE; } HWND hWndEdit = CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return EXIT_SUCCESS; } LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(EXIT_SUCCESS); default: return DefWindowProc(hWnd, msg, wParam, lParam); } return FALSE; } 

I use:

 CreateWindow(TEXT("Edit"), TEXT("test"), WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 140, 20, hWnd, NULL, NULL, NULL); 

to create a text box, and it's just an ugly text box with black text.

How to create a Windows style text box (with a three-dimensional light blue border)?

+7
source share
3 answers

Instead of using CreateWindow use CreateWindowEx and specify WS_EX_CLIENTEDGE as the first parameter.

You can compare the styles of the created edit control with a margin (for example, when you show "Properties" in a file in Explorer) using the Spy ++ tool that comes with Visual Studio.

+13
source

The OP edited his question by deleting the source code and, therefore, invalidating the answers, saying:

This is a simple example for creating a window with a text box.

I returned it to restore the source code, but I thought the working example was nice, so here:

  #include <windows.h>

 #pragma comment (linker, "/ manifestdependency: \" type = 'win32' name = 'Microsoft.Windows.Common-Controls' version = '6.0.0.0' processorArchitecture = 'x86' publicKeyToken = '6595b64144ccf1df' language = '*' \ "")

 LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

 int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPSTR nCmdLine, int nCmdShow)
 {
   LPTSTR windowClass = TEXT ("WinApp");
   LPTSTR windowTitle = TEXT ("Windows Application");
   WNDCLASSEX wcex;

   wcex.cbClsExtra = 0;
   wcex.cbSize = sizeof (WNDCLASSEX);
   wcex.cbWndExtra = 0;
   wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
   wcex.hCursor = LoadCursor (NULL, IDC_ARROW);
   wcex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
   wcex.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
   wcex.hInstance = hInstance;
   wcex.lpfnWndProc = WndProc;
   wcex.lpszClassName = windowClass;
   wcex.lpszMenuName = NULL;
   wcex.style = CS_HREDRAW |  CS_VREDRAW;
   if (! RegisterClassEx (& wcex))
   {
     MessageBox (NULL, TEXT ("RegisterClassEx Failed!"), TEXT ("Error"),
                MB_ICONERROR);
     return EXIT_FAILURE;
   }

   HWND hWnd;

   if (! (hWnd = CreateWindow (windowClass, windowTitle, WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                             CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
   {
     MessageBox (NULL, TEXT ("CreateWindow Failed!"), TEXT ("Error"), MB_ICONERROR);
     return EXIT_FAILURE;
   }

   HWND hWndEdit = CreateWindowEx (WS_EX_CLIENTEDGE, TEXT ("Edit"), TEXT ("test"),
                                WS_CHILD |  WS_VISIBLE, 100, 20, 140,
                                20, hWnd, NULL, NULL, NULL);

   ShowWindow (hWnd, nCmdShow);
   UpdateWindow (hWnd);

   Msg msg;

   while (GetMessage (& msg, NULL, 0, 0))
   {
     TranslateMessage (& msg);
     DispatchMessage (& msg);
   }
   return EXIT_SUCCESS;
 }

 LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
   switch (msg)
   {
   case WM_DESTROY:
     PostQuitMessage (EXIT_SUCCESS);
   default:
     return DefWindowProc (hWnd, msg, wParam, lParam);
   }
   return FALSE;
 }
+1
source

In Codeblocks, place the manifest file next to the project: program_name.exe.manifest

screenshot

And for program_name.exe.manifest write this:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="CompanyName.ProductName.YourApplication" type="win32" /> <description>Program name</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency> </assembly> 

And then your program will look like this:

Screen shot

0
source

All Articles