Passing to a Dialog

Example:
PostMessage(hwnd, ID_HWND_CLICK, 0, 0)
What I wanted to know is, using PostMessage(...) to send a control message (ie, ID_HWND_CLICK) defined within the program (ie, sending a message when a button is clicked), what would I use the wParam and lParam parameters for?
If its a custom message you are sending (and from your example it looks like it is), then what you put in or fail to put in the WPARAM and LPARAM parameters doesn't matter. Leave them zero if it suits you. Their purpose is to efficiently transfer application specific data. If there isn't any in the context of what you are doing, then there isn't any.

On the other hand, if all you are wanting to do is simulate a button click message, then send a WM_COMMAND message, and fill in the WPARAM and LPARAM parameters with the correct data pertaining to a button click. Windows has macros like MAKEDWORD (that might not be spelled right) that helps put both 'packets' together, i.e., the lo and hi word 16 bit parts of each parameter.

Maybe its just me, but I tend to use SendMessage() more frequently than PostMessage().
You NOT need to send ID of the control (ID_HWND_CLICK as you seem to do in this example), you send custom message itself (like WM_USER + 1 for example).

wparam and lparam can have any value you want.


If you want to send a message to a control, use his HWND instead (in your case I assume hwnd is the HWND of main dialog).


It may be better to explain exactly what you want to do, a custom message is rarely needed.
Okay, I'm fairly new (in terms of experience) to Win32.
There may be Is most likely another/simpler way to do this...I guess I'll just paste my whole source code I have so far here...

The program will be a "Virtual Library". It will allow the user to "upload" ebooks (essentially move the ebooks to a folder within the program's directory), add information about each one, display them and open them in their default external programs to read. (I'm aware there are programs like this, probably much better than mine will be, out there. This is a personal gift though, and it would mean more that I wrote out each line myself) XD

Also, I will probably split the project into multiple files, or move a lot to resource files, once it gets bigger but for now I am doing mostly everything dynamically (not sure if that's bad or not).

The whole source code is a little too long for one post (but by no means anywhere near complete. I still have A LOT to do), so I'll split between two posts, and add the reason behind my question at the end of the second post, so here it is: (Okay, my "Dialog" is not a conventional dialog window, I know)

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
#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <mmsystem.h>

#define IDM_FILE_ADD 01001
#define IDM_FILE_DEL 01002
#define IDM_FILE_EXIT 01003
#define IDM_LIB_AUTH 02001
#define IDM_LIB_TITL 02002
#define IDM_LIB_GENR 02003
#define IDM_LIB_DATE 02004
#define IDC_MAIN_LIB 10001
#define IDC_MAIN_ROW1 10002
#define IDC_MAIN_ROW2 10003
#define IDC_MAIN_ROW3 10004
#define IDC_MAIN_A01 10005
#define IDC_MAIN_A02 10006
#define IDC_MAIN_A03 10007
#define IDC_MAIN_A04 10008
#define IDC_MAIN_A05 10009
#define IDC_MAIN_A06 10010
#define IDC_MAIN_A07 10011
#define IDC_MAIN_A08 10012
#define IDC_MAIN_A09 10013
#define IDC_MAIN_A10 10014
#define IDC_MAIN_A11 10015
#define IDC_MAIN_A12 10016
#define IDC_DLG_BIBG 20001
#define IDC_DLG_COVER 20002
#define IDC_DLG_AUTH 20003
#define IDC_DLG_GENRE 20004
#define IDC_DLG_PUB 20005
#define IDC_DLG_SERI 20006
#define IDC_DLG_TITLE 20007
#define IDC_DLG_DATE 20008
#define IDC_DLG_PAGE 20009
#define IDC_DLG_ISER 20010
#define IDC_DLG_SNAU 20011
#define IDC_DLG_SNTI 20012

class clBook
{
  HBITMAP bmCover;
  wchar_t sAuthor[];
  wchar_t sGenre[];
  wchar_t sPublisher[];
  wchar_t sSeries[];
  wchar_t sTitle[];
  UINT iDate;
  UINT iPages;
  UINT iSeries;
  UINT iSerialA;
  UINT iSerialT;
};

LRESULT CALLBACK MainWinProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK GenWinProc(HWND, UINT, WPARAM, LPARAM);
void CreateDLG(HWND);
void GetExistingBooks(clBook&);
void GetNewBook(clBook&);

const wchar_t sMain[]=L"Virtual Library";
const wchar_t sDlg[]=L"Library Options";
HINSTANCE hDlgInst;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR sArg, int iShow)
{
  HWND hMain;
  MSG iMsg;
  WNDCLASSEX clMain;
  clMain.hInstance=hInstance;
  clMain.lpszClassName=sMain;
  clMain.lpfnWndProc=MainWinProc;
  clMain.style=CS_DBLCLKS;
  clMain.cbSize=sizeof(WNDCLASSEX);
  clMain.hIcon=LoadIcon(NULL, IDI_APPLICATION);
  clMain.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
  clMain.hCursor=LoadCursor(NULL, IDC_ARROW);
  clMain.lpszMenuName=NULL;
  clMain.cbClsExtra=0;
  clMain.cbWndExtra=0;
  clMain.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
  if(!RegisterClassEx(&clMain))
  {
    MessageBox(NULL, L"Windows Failed To Register Class.", L"Fatal Error",
               MB_ICONERROR|MB_OK|MB_DEFBUTTON1|MB_SYSTEMMODAL);
    return 0;
  }
  else
  {
    RECT clRect={0, 0, 800, 600};
    AdjustWindowRect(&clRect, WS_OVERLAPPED|WS_BORDER|WS_SYSMENU|WS_CAPTION, TRUE);
    hMain=CreateWindowEx(WS_EX_CLIENTEDGE, clMain, sMain, WS_OVERLAPPED|WS_BORDER|WS_SYSMENU|WS_CAPTION, CW_USEDEFAULT, CW_USEDEFAULT,
                         clRect.right-clRect.left, clRect.bottom-clRect.top, HWND_DESKTOP, NULL, hInstance, NULL);
    if(hMain==NULL)
    {
      MessageBox(NULL, L"Windows Failed To Create Window.", L"Fatal Error",
                 MB_ICONERROR|MB_OK|MB_DEFBUTTON1|MB_SYSTEMMODAL);
      return 0;
    }
    else
    {
      ShowWindow(hMain, iShow);
      UpdateWindow(hMain);
      while(GetMessage(&iMsg, NULL, 0, 0))
      {
        if(!IsDialogMessage(hDlg, &iMsg))
        {
          TranslateMessage(&iMsg);
          DispatchMessage(&iMsg);
        }
      }
    }
  }
  return iMsg.wParam;
}
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
LRESULT CALLBACK MainWinProc(HWND hMain, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
  switch(iMsg)
  {
    case WM_CREATE:
    {
      HWND hLib, hRow1, hRow2, hRow3;
      HMENU mMain, mFile, mOrganize;
      HBITMAP bmBG, bmR;
      RECT clRect;
      mMain=CreateMenu();
      mFile=CreatePopupMenu();
      AppendMenu(mFile, MF_STRING, IDM_FILE_ADD, L"Add Book");
      AppendMenu(mFile, MF_STRING, IDM_FILE_DEL, L"Remove Book");
      AppendMenu(mFile, MF_SEPARATOR, NULL, NULL);
      AppendMenu(mFile, MF_STRING, IDM_FILE_EXIT, L"Exit");
      AppendMenu(mMain, MF_STRING|MF_POPUP, (UINT)mFile, L"File");
      mOrganize=CreatePopupMenu();
      AppendMenu(mOrganize, MF_STRING, IDM_LIB_AUTH, L"By Author");
      AppendMenu(mOrganize, MF_STRING, IDM_LIB_TITL, L"By Title");
      AppendMenu(mOrganize, MF_STRING, IDM_LIB_GENR, L"By Genre");
      AppendMenu(mOrganize, MF_STRING, IDM_LIB_DATE, L"By Date");
      AppendMenu(mMain, MF_STRING|MF_POPUP, (UINT)mOrganize, L"Organize");
      SetMenu(hMain, mMain);
      GetClientRect(hMain, &clRect);
      bmBG=(HBITMAP)LoadImage(NULL, L"C:/Users/User/Desktop/Projects/Win32/For Anagel/Images/lbbg.bmp",
                              IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
      bmR=(HBITMAP)LoadImage(NULL, L"C:/Users/User/Desktop/Projects/Win32/For Anagel/Images/lbr.bmp",
                             IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
      hLib=CreateWindowEx(0, L"Static", L"", WS_CHILD|WS_VISIBLE|SS_BITMAP, 0, 0, 800, 600, hMain,
                         (HMENU)IDC_MAIN_LIB, GetModuleHandle(NULL), NULL);
      hRow1=CreateWindowEx(0, L"Static", L"", WS_CHILD|WS_VISIBLE|SS_BITMAP, 50, 30, 700, 160, hMain,
                          (HMENU)IDC_MAIN_ROW1, GetModuleHandle(NULL), NULL);
      hRow2=CreateWindowEx(0, L"Static", L"", WS_CHILD|WS_VISIBLE|SS_BITMAP, 50, 220, 700, 160, hMain,
                          (HMENU)IDC_MAIN_ROW2, GetModuleHandle(NULL), NULL);
      hRow3=CreateWindowEx(0, L"Static", L"", WS_CHILD|WS_VISIBLE|SS_BITMAP, 50, 410, 700, 160, hMain,
                          (HMENU)IDC_MAIN_ROW3, GetModuleHandle(NULL), NULL);
      SendDlgItemMessage(hMain, IDC_MAIN_LIB, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmBG);
      SendDlgItemMessage(hMain, IDC_MAIN_ROW1, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmR);
      SendDlgItemMessage(hMain, IDC_MAIN_ROW2, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmR);
      SendDlgItemMessage(hMain, IDC_MAIN_ROW3, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmR);
      DeleteObject(bmBG);
      DeleteObject(bmR);
    }
      break;
    case WM_PAINT:
    {
      PAINTSTRUCT ps;
      HDC hdc=BeginPaint(hMain, &ps);
      FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW+1));
      EndPaint(hMain, &ps);
    }
      break;
    case WM_SIZE:
    {
      HWND hLib;
      RECT clRect;
      GetClientRect(hMain, &clRect);
      hLib=GetDlgItem(hMain, IDC_MAIN_LIB);
      SetWindowPos(hLib, NULL, clRect.left, clRect.top, clRect.right, clRect.bottom, NULL);
    }
      break;
    case WM_COMMAND:
    {
      switch(LOWORD(wParam))
      {
        case IDM_FILE_ADD:
        {
          CreateDlg(hInstance, hMain);
        }
          break;
        case IDM_FILE_DEL:
        {
        }
          break;
        case IDM_FILE_EXIT:
        {
          PostMessage(hMain, WM_CLOSE, 0, 0);
        }
          break;
        case IDM_LIB_AUTH:
        {
        }
          break;
        case IDM_LIB_TITL:
        {
        }
          break;
        case IDM_LIB_GENR:
        {
        }
          break;
        case IDM_LIB_DATE:
        {
        }
          break;
        case IDC_MAIN_A01:
        {
        }
          break;
        case IDC_MAIN_A02:
        {
        }
          break;
        case IDC_MAIN_A03:
        {
        }
          break;
        case IDC_MAIN_A04:
        {
        }
          break;
        case IDC_MAIN_A05:
        {
        }
          break;
        case IDC_MAIN_A06:
        {
        }
          break;
        case IDC_MAIN_A07:
        {
        }
          break;
        case IDC_MAIN_A08:
        {
        }
          break;
        case IDC_MAIN_A09:
        {
        }
          break;
        case IDC_MAIN_A10:
        {
        }
          break;
        case IDC_MAIN_A11:
        {
        }
          break;
        case IDC_MAIN_A12:
        {
        }
          break;
      }
    }
      break;
    case WM_DESTROY:
    {
      PostQuitMessage(0);
    }
      break;
    case WM_QUIT:
    {
      DestroyWindow(hMain);
    }
      break;
    default:
      return DefWindowProc(hMain, iMsg, wParam, lParam);
  }
  return 0;
}

BOOL CALLBACK GenWinProc(HWND hMain, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
  switch(iMsg)
  {
    case WM_INITDIALOG:
    {
      HWND hBookInfo;
      HBITMAP bmBG;
      bmBG=(HBITMAP)LoadImage(NULL, L"C:/Users/User/Desktop/Projects/Win32/For Anagel/Images/bil.bmp",
                              IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
      hBookInfo=CreateWindowEx(0, L"Static", L"", WS_CHILD|WS_VISIBLE|SS_BITMAP, 0, 0, 800, 600, hMain,
                              (HMENU)IDC_DLG_BIBG, GetModuleHandle(NULL), NULL);
      SendDlgItemMessage(hDLG, IDC_DLG_BIBG, STM_SETIMAGE, IMAGE_BITMAP, (WPARAM)bmBG);
      return TRUE;
    }
      break;
    case WM_COMMAND:
    {
      switch(LOWORD(wParam))
      {
      }
    }
      break;
    default:
      return FALSE;
  }
  return TRUE;
}

BOOL CreateDLG(HINSTANCE hInstance, HWND hMain)
{
  WNDCLASSEX clTemp;
  if(GetClassInfoEx(hInstance, &clDlg, &clTemp)!=0)
  {
    WNDCLASSEX clDlg;
    cMain.hInstance=hDlgInst;
    cMain.lpszClassName=sDlg;
    cMain.lpfnWndProc=GenWinProc;
    cMain.style=0;
    cMain.cbSize=sizeof(WNDCLASSEX);
    cMain.hIcon=LoadIcon(NULL, IDI_APPLICATION);
    cMain.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
    cMain.hCursor=LoadCursor(NULL, IDC_ARROW);
    cMain.lpszMenuName=NULL;
    cMain.cbClsExtra=0;
    cMain.cbWndExtra=0;
    cMain.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
    if(!RegisterClassEx(&clDlg))
    {
      MessageBox(NULL, L"Windows Failed To Register Dialog Class.", L"Fatal Error",
                 MB_ICONERROR|MB_OK|MB_DEFBUTTON1|MB_SYSTEMMODAL);
      return FALSE;
    }
  }
  if(FindWindowEx(hMain, NULL, &clDlg, sDlg)!=NULL)
  {
    RECT clRect={0, 0, 800, 600};
    AdjustWindowRect(&clRect, WS_VISIBLE|WS_SYSMENU|WS_CAPTION, FALSE);
    hDlg=CreateWindowEx(WS_EX_DLGMODALFRAME|WS_EX_TOPMOST, clDlg, sDlg, WS_OVERLAPPED|WS_BORDER|WS_SYSMENU|WS_CAPTION, CW_USEDEFAULT,
                        CW_USEDEFAULT, clRect.right-clRect.left, clRect.bottom-clRect.top, hMain, NULL, hInstance, NULL);
    if(hDlg==NULL)
    {
      MessageBox(NULL, L"Windows Failed To Create Dialog Window.", L"Fatal Error",
                 MB_ICONERROR|MB_OK|MB_DEFBUTTON1|MB_SYSTEMMODAL);
      return FALSE;
    }
    return TRUE;
  }
}


The reason I asked this question, is because the second window, or "Dialog" if you will, should take on different looks (display different controls, etc) based on the reason it was called. Basically, I wanted to be able to send a message to the "Dialog" but include information which could be pulled out in the WM_INITDIALOG/WM_CREATE section of the second window to determine which "look" it will take on and display the relative controls. Or, should I just split the second window, and give each "look" it's own window and message loop?
Sorry for leaving in all of the stuff not really important to the question, but for the sake of showing you exactly what I'm working with so far, I left it XD

I have tried compiling it, and it does run so far. So far, everything is exactly as it should be. The bitmaps used with the static controls are essentially wood grain textures used to simulate a bookshelf look XD

The large amount of IDC_MAIN_Axx IDs will be buttons, with book covers as bitmaps placed over them for each of the books displayed, and when clicked will open the second window, showing the information for the book. The options to change information for the book, as well as open it will all be handled in the second window, with the first essentially being there for "looks" XD

I wanted to be able to use PostMessage(...) to send messages from window one to window two. When window two is called, it will check to see if the class is registered and the window is created to avoid reregistering it, or recreating it, since it will act as a "modeless dialog". As I've mentioned, the second window will display different controls in different areas depending on why it was called (which is the reason behind asking what would the wParam and lParam paramaters be used for) so I could find a way to pass in additional information on which look it should attain if it is not already open, or if it is already open, update it on which look it should change to (depending upon the message sent).

There may be an easier way to do this (and as I've said, I'm fairly new to using the Win32 API, so if there is please let me know). Thank you for your time and responses.

- John
65U7UIJGH

Interesting...


May I please just put in a request to have this thread deleted (as there is nothing worth keeping)?
I'll just employ a trial error system until I get it.
Nah, you can't delete a thread. They just stay up almost forever, in case they're useful for someone else.
Topic archived. No new replies allowed.