ERROR_THREAD_MODE_ALREADY_BACKGROUND
400 (0x190) The thread is already in background processing mode.怎么回有这样的错误? 我写了一个很简单的Win32程序,只是想显示一个最简单的窗口,如下代码:
#include<Windows.h>
#include<stdio.h>
const char className[]="mywndclass";
LRESULT __stdcall myproc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
return 0L;
}
int __stdcall WinMain(
HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmdLine,
int nCmdShow
){
WNDCLASSEX wnd;
ZeroMemory(&wnd,sizeof(wnd));
wnd.cbSize=sizeof(wnd);
wnd.lpszClassName=className;
wnd.lpszMenuName=NULL;
wnd.hInstance=hInst;
wnd.lpfnWndProc=myproc;
wnd.hbrBackground=(HBRUSH)MAKEINTRESOURCE(GRAY_BRUSH);
HWND hWnd=CreateWindowEx(0,className,"窗口名称",0,0,0,300,200,NULL,NULL,hInst,0);
if(INVALID_HANDLE_VALUE==hWnd){
MessageBox(NULL,"Error!","CreateWindowEx失败\n",MB_OK);
return 1;
}
if(!ShowWindow(hWnd,SW_SHOW)){
char dip[200];
sprintf(dip,"ShowWindow失败:%d\n",GetLastError());
MessageBox(NULL,"Error!",dip,MB_OK);
return 1;
}
return 0;
}
400 (0x190) The thread is already in background processing mode.怎么回有这样的错误? 我写了一个很简单的Win32程序,只是想显示一个最简单的窗口,如下代码:
#include<Windows.h>
#include<stdio.h>
const char className[]="mywndclass";
LRESULT __stdcall myproc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
return 0L;
}
int __stdcall WinMain(
HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmdLine,
int nCmdShow
){
WNDCLASSEX wnd;
ZeroMemory(&wnd,sizeof(wnd));
wnd.cbSize=sizeof(wnd);
wnd.lpszClassName=className;
wnd.lpszMenuName=NULL;
wnd.hInstance=hInst;
wnd.lpfnWndProc=myproc;
wnd.hbrBackground=(HBRUSH)MAKEINTRESOURCE(GRAY_BRUSH);
HWND hWnd=CreateWindowEx(0,className,"窗口名称",0,0,0,300,200,NULL,NULL,hInst,0);
if(INVALID_HANDLE_VALUE==hWnd){
MessageBox(NULL,"Error!","CreateWindowEx失败\n",MB_OK);
return 1;
}
if(!ShowWindow(hWnd,SW_SHOW)){
char dip[200];
sprintf(dip,"ShowWindow失败:%d\n",GetLastError());
MessageBox(NULL,"Error!",dip,MB_OK);
return 1;
}
return 0;
}
winproc是空的,这个没有什么问题吧
,0,0,300,200,NULL,NULL,hInst,0);
if(!hWnd){
MessageBox(NULL,"Error!","CreateWindowEx失败\n",MB_OK);
return 1;
}
2)判断hwnd,应该是非0,而不是-1。
Nonzero indicates that the window was previously visible. Zero indicates that the window was previously hidden.
我按照你说的两条改了一下。但是问题是现在CreateWindowEx虽然成功了(GetLastError=0),但是返回hWnd=0!
这是为什么呢?
现在的代码是:#include<Windows.h>
#include<stdio.h>
const char className[]="mywndclass";
LRESULT __stdcall myproc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
return 0L;
}
int __stdcall WinMain(
HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmdLine,
int nCmdShow
){
WNDCLASSEX wnd;
ZeroMemory(&wnd,sizeof(wnd));
wnd.cbSize=sizeof(wnd);
wnd.lpszClassName=className;
wnd.lpszMenuName=NULL;
wnd.hInstance=hInst;
wnd.lpfnWndProc=myproc;
wnd.hbrBackground=(HBRUSH)MAKEINTRESOURCE(GRAY_BRUSH);
ATOM reg;
if((reg=RegisterClassEx(&wnd))==0){
MessageBox(NULL,"RegisterClassEx失败\n","有错!",MB_OK);
return 1;
}
char dip[200];
HWND hWnd=CreateWindowEx(0,className,"窗口名称",
WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,0,0,300,200,NULL,NULL,hInst,0);
if(0==hWnd){
sprintf(dip,"CreateWindowEx失败:%d\n",GetLastError());
MessageBox(NULL,dip,"有错r!",MB_OK);
return 1;
}
if(!ShowWindow(hWnd,SW_SHOW)){
sprintf(dip,"ShowWindow失败:%d\n",GetLastError());
MessageBox(NULL,dip,"有错!",MB_OK);
return 1;
}
return 0;
}
RegisterClassEx并且尚需判断窗口是否创建成功
IsWindow
//#include "stdafx.h"
#include<Windows.h>
#include<stdio.h>
char className[]="mywndclass";
LRESULT __stdcall myproc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp){
MSG msg;
while (GetMessage(&msg, (HWND) NULL, 0, 0) != 0 && GetMessage(&msg, (HWND) NULL, 0, 0) != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return 0;
}
int __stdcall WinMain(
HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmdLine,
int nCmdShow
){
WNDCLASSEX wnd;
ZeroMemory(&wnd,sizeof(wnd));
wnd.cbSize=sizeof(wnd);
wnd.lpszClassName=className;
wnd.lpszMenuName=NULL;
wnd.hInstance=hInst;
wnd.lpfnWndProc=myproc;
wnd.hbrBackground=(HBRUSH)MAKEINTRESOURCE(GRAY_BRUSH);
RegisterClassEx(&wnd); HWND hWnd=CreateWindowEx(0,className,"窗口名称", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL
,0,0,300,200,NULL,NULL,hInst,0);
if(!hWnd){
MessageBox(NULL,"Error!","CreateWindowEx失败\n",MB_OK);
return 1;
}
if(!ShowWindow(hWnd,SW_SHOW)){
char dip[200];
sprintf(dip,"ShowWindow失败:%d\n",GetLastError());
MessageBox(NULL,"Error!",dip,MB_OK);
return 1;
}
return 0;
}