C语言子窗口:我输入下列程序,想弄出来一个子窗口,可是却提示出错。不解,求助

Python011

C语言子窗口:我输入下列程序,想弄出来一个子窗口,可是却提示出错。不解,求助,第1张

#include <windows.h> 

const LPSTR szClassName = "sdfkljs"

const LPSTR szWindowName = "sdfk"

const LPSTR szChildClassName = "asdfklajd"

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

LRESULT CALLBACK ChildProc(HWND, UINT, WPARAM, LPARAM)

WNDCLASSEX wc

HWND hwnd1,hwnd2

HINSTANCE hInst

int nCmdShow

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow){

::nCmdShow = nCmdShow 

::hInst = hInstance

wc.cbSize = sizeof(wc)

wc.style = CS_HREDRAW | CS_VREDRAW

wc.lpfnWndProc = WndProc

wc.cbClsExtra = 0

wc.cbWndExtra = 0

wc.hInstance = hInstance

wc.hIcon = LoadIcon(NULL, IDI_APPLICATION)

wc.hCursor = LoadCursor(NULL, IDC_ARROW)

wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH)

wc.lpszMenuName = NULL

wc.lpszClassName = szClassName

wc.hIconSm = NULL

RegisterClassEx(&wc)

hwnd1 = CreateWindowEx(0, szClassName, szWindowName, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL)

ShowWindow(hwnd1, nCmdShow)

UpdateWindow(hwnd1)

wc.lpfnWndProc = ChildProc

wc.hIcon = NULL

wc.hCursor = NULL

wc.lpszClassName = szChildClassName

RegisterClassEx(&wc)

hwnd2 = CreateWindowEx(0, szChildClassName, NULL, WS_CHILDWINDOW|WS_OVERLAPPEDWINDOW, 0, 0, 100, 100, hwnd1, NULL, hInst, NULL)

if (hwnd2==NULL){

MessageBox(NULL, "Wrong!", "adfa", MB_OKCANCEL)

return -1

}

ShowWindow(hwnd2, nCmdShow)

UpdateWindow(hwnd2)

MSG msg

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

TranslateMessage(&msg)

DispatchMessage(&msg)

}

return msg.wParam

}

LRESULT CALLBACK WndProc(HWND h1, UINT msg, WPARAM w1, LPARAM l1){

switch (msg) {

case WM_LBUTTONDOWN:

MessageBox(h1, "Frame Click", "Click!", MB_OK) return 0

default: return DefWindowProc(h1, msg, w1, l1)

}

}

LRESULT CALLBACK ChildProc(HWND h1, UINT msg, WPARAM w1, LPARAM l1){

switch (msg) {

case WM_LBUTTONDOWN:

MessageBox(h1, "View Click", "Click!", MB_OK) return 0

default: return DefWindowProc(h1, msg, w1, l1)

}

}

已改好,主要的问题是:

你在设置好子窗口类后没有调用RegisterClassEx()函数注册窗口类,导致子窗口创建不成功,我把子窗口的创建从WM_CREATE放到WINMAIN里了,你可以看看,然后做进一步修改

windows下通过调用API来创建窗口:

#include<windows.h>

int main()

{

MessageBox(NULL,"Hello World!","C图形程序",MB_OK)

return 0

}

linux下通过调用图形库来创建窗口。

楼主如果是学C的话,先不要急于搞这些东西,把基础打扎实才是最重要的,GUI可以后学。基础扎实了,这些只是很简单的东西。