用createwindow 为啥没窗口出来???
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用createwindow 为啥没窗口出来???相关的知识,希望对你有一定的参考价值。
#include<windows.h>int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) HWND hwnd ;hwnd=CreateWindow( "MySub ", "Test ",WS_BORDER, 50, 50, 140, 100, NULL, (HMENU)121, NULL, NULL); return 0 ;编译通过运行起没窗口是咋回事
首先,你的窗口类注册了吗?也就是RegisterClass。。。没注册窗口类CreateWindow就会调用失败。
其次,你创建的窗口没有WS_VISIBLE 属性,这样的窗口需要调用ShowWindow来显示。
给你一个完整的小实例吧
#include <windows.h>
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
TCHAR szname[]=TEXT("classname");
HWND hwnd;
MSG msg;
ZeroMemory(&msg,sizeof(msg));
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc=WinProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpszClassName=szname;
wndclass.lpszMenuName=NULL;
RegisterClass(&wndclass);
hwnd=CreateWindow(szname,TEXT("windowsnametest"),WS_OVERLAPPEDWINDOW ,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(hwnd)
ShowWindow(hwnd,SW_SHOW);
else
MessageBox(NULL,TEXT("创建窗口失败!"),TEXT("提示"),MB_OK);
UnregisterClass(szname,hInstance);
return 1;
UpdateWindow(hwnd);
while(GetMessage(&msg,hwnd,0,0)>0)
TranslateMessage(&msg);
DispatchMessage(&msg);
UnregisterClass(szname,hInstance);
return msg.wParam;
LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
switch(message)
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
return 0;
参考技术A 如果窗口创建成功的话.
那么还需要调用ShowWindow这个API,
窗口刚创建的时候默认是隐藏的
ShowWindow(hwnd, SW_SHOW); // 显示窗口
ShowWindow(hwnd,SW_HIDE); // 隐藏窗口
此外, 还需要 UpdateWindow来刷新.
具体百度这个2函数, 百科有很详细的解释 参考技术B 因为你只是创建了窗口,你并没有显示以及更新窗口,你必须在创建之后继续显示更新窗口。 参考技术C WS_BORDER | WS_VISIBLE
我使用vc++编程,我使用RegisterClassEx注册了窗口类,怎么用CreateWindow创建该类的MDI窗口?
使用RegisterClassEx创建子窗口类后,
使用CreateWindow(_TEXT("MDICLIENT"), NULL, WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
0, 0, rectMain.right, 50, hWnd, NULL, hInstance, (PSTR)&lsSet1);
创建了hwnd的MDI窗口,怎样才能使新创建的MDI窗口与刚刚注册的窗口类相关联呢?
#include <windows.h>
#include <tchar.h>
HINSTANCE hInst;
ATOM WindowRegister(WNDPROC lpfnWndProc, char *lpClname, HBRUSH hbrBack)
WNDCLASSEX wnd;
wnd.cbClsExtra = 0;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.cbWndExtra = 0;
wnd.hbrBackground = hbrBack;
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wnd.hInstance = hInst;
wnd.lpfnWndProc = lpfnWndProc;
wnd.lpszClassName = lpClname;
wnd.lpszMenuName = NULL;
wnd.style = CS_HREDRAW | CS_VREDRAW;
return RegisterClassEx(&wnd);
LRESULT CALLBACK ClientProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
HWND hWndEdit;
switch (uMsg)
case WM_CREATE:
RECT rect;
GetClientRect(hWnd, &rect);
hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_MULTILINE,
0, 0, rect.right, rect.bottom, hWnd, NULL, hInst, NULL);
return 0;
case WM_SIZE:
hWndEdit = GetWindow(hWnd, GW_CHILD);
GetClientRect(hWnd, &rect);
SetWindowPos(hWndEdit, NULL, 0, 0, rect.right, rect.bottom, SWP_NOMOVE | SWP_NOZORDER);
break;
case WM_MDIACTIVATE:
return 0;
return DefMDIChildProc(hWnd, uMsg, wParam, lParam);
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
static HWND hClient;
MDICREATESTRUCT mdic;
switch (uMsg)
case WM_CREATE:
WindowRegister(ClientProc, _T("Client"), (HBRUSH)GetStockObject(WHITE_BRUSH));
HMENU hMenu;
hMenu = CreateMenu();
AppendMenu(hMenu, MF_STRING, 1000, _T("New(&N)"));
SetMenu(hWnd, hMenu);
CLIENTCREATESTRUCT ccs;
ccs.hWindowMenu = NULL;
ccs.idFirstChild = 100;
hClient = CreateWindow(_T("MDICLIENT"), NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
0, 0, 0, 0, hWnd, NULL, hInst, &ccs);
return 0;
case WM_DESTROY: PostQuitMessage(0); return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
case 1000:
mdic.szClass = _T("Client");
mdic.szTitle = _T("Document");
mdic.hOwner = hInst;
mdic.x = CW_USEDEFAULT;
mdic.y = CW_USEDEFAULT;
mdic.cx = CW_USEDEFAULT;
mdic.cy = CW_USEDEFAULT;
mdic.style = 0;
mdic.lParam = 0;
HWND hChild;
hChild = (HWND)SendMessage(hClient, WM_MDICREATE, 0, (LPARAM)&mdic);
break;
default:
hChild = (HWND)SendMessage(hClient, WM_MDIGETACTIVE, 0, 0);
if (IsWindow(hChild)) SendMessage(hChild, WM_COMMAND, wParam, lParam);
break;
break;
return DefFrameProc(hWnd, hClient, uMsg, wParam, lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd)
hInst = hInstance;
WindowRegister(WndProc, _T("Sample"), (HBRUSH)(COLOR_APPWORKSPACE + 1));
HWND hWnd = CreateWindow(_T("Sample"), _T("Sample"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
100, 100, 500, 400, NULL, NULL, hInstance, NULL);
if (!hWnd) return FALSE;
HWND hClient = GetWindow(hWnd, GW_CHILD);
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
if (!TranslateMDISysAccel(hClient, &msg))
TranslateMessage(&msg);
DispatchMessage(&msg);
return msg.wParam;
参考技术A 你在用CreateWindow函数创建窗口时,该函数的第一个参数,是窗口类名,你只要用你上面注册的窗口类的类名,这样就联系上了。
以上是关于用createwindow 为啥没窗口出来???的主要内容,如果未能解决你的问题,请参考以下文章
我使用vc++编程,我使用RegisterClassEx注册了窗口类,怎么用CreateWindow创建该类的MDI窗口?