My First Windows Program

closed account (oN3AqMoL)
Hello! Im creating my first "window" in windows but I keep coming up with one error: the fact that my refrence to WIndowProc is undefined when I do:
wc1.lpfnWndProc = WindowProc.
Its irritating mostly because the code on microsoft.com's tutorials says the same thing. I think since WIndowProc looks like a function I have to use the parameter values, but I dont know what to put for them.
Here's the code:


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
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
int main(int argc, char *argv[]) {
    return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    const wchar_t ClassName1[] = L"ClassName1";
    const wchar_t ClassName2[] = L"ClassName2";
    WNDCLASS wc1 = { };
    WNDCLASS wc2 = {1};
    wc1.lpfnWndProc = WindowProc;
    wc2.lpfnWndProc = WindowProc;
    wc1.hInstance = hInstance;
    wc2.hInstance = hInstance;
    wc1.lpszClassName = ClassName1;
    wc2.lpszClassName = ClassName2;

    RegisterClass(&wc1); RegisterClass(&wc2);
        HWND lala = CreateWindowEx(
        0,                              // Optional window styles.
        ClassName1,                     // Window class
        L"ClassName1",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );
                HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        ClassName2,                     // Window class
        L"ClassName2",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );

    if (hwnd == NULL)
    {
        return 0;
    }
    if (lala == NULL)
    {
        return 0;
    }
ShowWindow(hwnd,nCmdShow);
ShowWindow(lala,nCmdShow);
}

Last edited on
You have correctly declared the WindowProc's function prototype in line 8 but you have failed to actually define it. In other words: You declared the function but did not define it. You need to write the function body.

Also note that main() is not required on a Windows graphical executable because the entry point is WinMain, not main(). main() is for console projects only.
closed account (oN3AqMoL)
Ok then, what does WinProc do and how do I define it?
Doesn't the tutorial you are following tell you? That's weird. Check out a different tutorial, I say, as that is just not going to work.

I also don't see a message loop (a. k. a. message pump), another fundamental piece of code.

See if the tutorial @ www.winprog.org fits you better. Just a warning: It is old, back in the day where the default was ANSI compilation. Now everyone should be UNICODE-compiling. I think you already know about this, so just a heads up.
As
webJose
said, you have defined WindowProc but not implement it; however about
the fact that my refrence to WIndowProc is undefined when I do
,it makes me confused, maybe you just spell "i" to "I" of WIndowProc.
You can define the WindowProc like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		case WM_CHAR:char szChar[20];
			sprintf(szChar,"char is %d",wParam);//sprintf格式化wParam值,存入szChar中
			MessageBox(hwnd,szChar,"weixin",0);
			break;
		case WM_CLOSE:
			if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
			{
				DestroyWindow(hwnd);      //使用这个函数时,会发送WM_DESTROY消息
				//销毁窗口
			}
			break;
		default:
			return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}
}


maybe It can help you .
closed account (oN3AqMoL)
Thanks Marguarite! Your def really helped!
Last edited on
1
2
3
4
5
if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
			{
				DestroyWindow(hwnd);      //使用这个函数时,会发送WM_DESTROY消息
				//销毁窗口
			}

Wtf?
Topic archived. No new replies allowed.