WINAPI

I am trying to get an EDITTEXT resource to show up. I created the resource file and the resource header file but not sure what to add to my main file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

#include <windows.h>

#include "resource.h"



#define ID_TIMER 1



LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;



TCHAR szAppName[] = TEXT ("MenuDemo") ;



int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

                    PSTR szCmdLine, int iCmdShow)

{

     HWND     hwnd ;

     MSG      msg ;

     WNDCLASS wndclass ;

     

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;

     wndclass.lpfnWndProc   = WndProc ;

     wndclass.cbClsExtra    = 0 ;

     wndclass.cbWndExtra    = 0 ;

     wndclass.hInstance     = hInstance ;

     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;

     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;

     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;

     wndclass.lpszMenuName  = szAppName ;

     wndclass.lpszClassName = szAppName ;

     

     if (!RegisterClass (&wndclass))

     {

          MessageBox (NULL, TEXT ("This program requires Windows NT!"),

                      szAppName, MB_ICONERROR) ;

          return 0 ;

     }

     

     hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),

                          WS_OVERLAPPEDWINDOW,

                          CW_USEDEFAULT, CW_USEDEFAULT,

                          CW_USEDEFAULT, CW_USEDEFAULT,

                          NULL, NULL, hInstance, NULL) ;

     

     ShowWindow (hwnd, iCmdShow) ;

     UpdateWindow (hwnd) ;

     

     while (GetMessage (&msg, NULL, 0, 0))

     {

          TranslateMessage (&msg) ;

          DispatchMessage (&msg) ;

     }

     return msg.wParam ;

}



LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{



     HMENU      hMenu ;

     

     switch (message)

     {

     case WM_COMMAND:

          hMenu = GetMenu (hwnd) ;

          

          switch (LOWORD (wParam))

          {

          case IDM_FILE_NEW:

          case IDM_FILE_OPEN:

          case IDM_FILE_SAVE:

          case IDM_FILE_SAVE_AS:

               MessageBox (NULL, TEXT ("Hello"), TEXT ("Top"),0);

               return 0 ;

               

          case IDM_APP_EXIT:

               SendMessage (hwnd, WM_CLOSE, 0, 0) ;

               return 0 ;

          }

          break ;

                        

     case WM_DESTROY:

          PostQuitMessage (0) ;

          return 0 ;

     }

     return DefWindowProc (hwnd, message, wParam, lParam) ;

}

BOOL CALLBACK DlgProc (HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_SETFOCUS:
			SetFocus(GetDlgItem (hdlg, IDC_LF_HEIGHT));
			return FALSE;
	}
	return FALSE;
}
Last edited on
It's not easy to understand your question and what you are trying to do.
from what I see, you created dialog resource but don't know how to show it?

you may need to call DialogBoxParam function to create and show the dialog,
second parameter of this function takes a resource ID you made, 4th parameter is window procedure pointer of the dialog.
for the rest of parameters see:
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dialogboxparama

You need to set this function call every time menu item is clicked or every time some button is clicked to create and show the dialog.

EDITTEXT resource, or what ever resource made with resource editor should be put onto the dialog in design window.

inside the dialog procedure you simply call API's to manipulate the resource such as:
1
2
3
SetDlgItemText
SendMessage
GetDlgItem

you usually do this on WM_INITDIALOG message.

resources and dialog design are cumbersome and limited on what you can do. it's always better to make controls and dialogs from scrach.

Also consider object oriented design, for example creating C++ classes representing controls, dialogs and windows etc.. instead of procedural approach as shown in you example code.
@malibor,
creating classes is a good idea, but first you need to understand how the WIN API works. Seems the OP is new to this kind of code.
Also consider object oriented design, for example creating C++ classes representing controls, dialogs and windows etc.. instead of procedural approach as shown in you example code.

The Microsoft Foundation Class library already did that.

The Win23 API is written in C, not C++.

Creating an edit control at run-time is an alternative:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

const int IDC_EDIT     = 1;
TCHAR     szAppName[]  = TEXT("PopPad");
TCHAR     szAppTitle[] = TEXT("Pop Pad");


int WINAPI WinMain(_In_ HINSTANCE hInstance,
                   _In_opt_ HINSTANCE hPrevInstance,
                   _In_ PTSTR szCmdLine,
                   _In_ int iCmdShow)
{
   HWND      hwnd;
   MSG        msg;
   WNDCLASSEX wc;

   wc.cbSize        = sizeof(WNDCLASSEX);
   wc.hInstance     = hInstance;
   wc.lpszClassName = szAppName;
   wc.lpfnWndProc   = WndProc;
   wc.style         = CS_HREDRAW | CS_VREDRAW;
   wc.hIcon         = (HICON) LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED);
   wc.hIconSm       = NULL;
   wc.hCursor       = (HCURSOR) LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
   wc.lpszMenuName  = NULL;
   wc.cbClsExtra    = 0;
   wc.cbWndExtra    = 0;
   wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);

   if (RegisterClassEx(&wc) == 0)
   {
      MessageBox(NULL, TEXT("Can't Register the Window Class!"),
                 szAppName, MB_OK | MB_ICONERROR);
      return 0;
   }

   hwnd = CreateWindow(szAppName, szAppTitle,
                       WS_OVERLAPPEDWINDOW,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       NULL, NULL, hInstance, NULL);

   if (hwnd == NULL)
   {
      MessageBox(NULL, TEXT("Unable to Create the Main Window!"),
                 szAppName, MB_OK | MB_ICONERROR);
      return 0;
   }

   ShowWindow(hwnd, iCmdShow);
   UpdateWindow(hwnd);

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

   return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   static HWND hwndEdit;

   switch (message)
   {
   case WM_CREATE:
      hwndEdit = CreateWindow(TEXT("edit"), NULL,
                              WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
                              WS_BORDER | ES_LEFT | ES_MULTILINE |
                              ES_AUTOHSCROLL | ES_AUTOVSCROLL,
                              0, 0, 0, 0, hwnd, (HMENU) IDC_EDIT,
                              ((LPCREATESTRUCT) lParam)->hInstance, NULL);
      return 0;

   case WM_SETFOCUS:
      SetFocus(hwndEdit);
      return 0;

   case WM_SIZE:
      MoveWindow(hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
      return 0;

   case WM_COMMAND:
      if (LOWORD(wParam) == IDC_EDIT)
      {
         if (HIWORD(wParam) == EN_ERRSPACE ||
             HIWORD(wParam) == EN_MAXTEXT)
         {
            MessageBox(hwnd, TEXT("Edit control out of space."),
                       szAppName, MB_OK | MB_ICONSTOP);
         }
      }
      return 0;

   case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
   }

   return DefWindowProc(hwnd, message, wParam, lParam);
}
Here are awesome code samples that you may find useful for learning to create custom dialogs and controls:
http://winapi.foosyerdoos.org.uk/info/res_dialogs.php

the root page:
http://winapi.foosyerdoos.org.uk/index.php
Thanks
Topic archived. No new replies allowed.