C++用Win32的API写一个简单的窗口,有时进程运行了就是没有窗口显示~~有时又可以~~
如题,真的很奇怪,给代码你们看看有什么问题没有~~#include<Windows.h>ATOMMyRegister(HINSTANCEhInstance);BOOLIn...
如题,真的很奇怪,给代码你们看看有什么问题没有~~
#include<Windows.h>
ATOM MyRegister(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WindowProc(
__in HWND hwnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
);
LPCWSTR lpInstanceName = TEXT("freeShortWindow");
HWND hWnd;
WNDCLASS wc;
int APIENTRY WinMain(
__in HINSTANCE hInstance,
__in HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nCmdShow
)
{
MyRegister(hInstance);
InitInstance(hInstance, nCmdShow);
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegister(HINSTANCE hInstance)
{
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_CROSS);
wc.hIcon = LoadIcon(NULL, IDI_SHIELD);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = lpInstanceName;
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW|CS_HREDRAW;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd = CreateWindow( lpInstanceName, lpInstanceName,
WS_OVERLAPPEDWINDOW,
100,100,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
return 1;
}
LRESULT CALLBACK WindowProc(
__in HWND hwnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
)
{
switch(uMsg)
{
case WM_CREATE:
{}break;
case WM_PAINT:
{}break;
case WM_COMMAND:
{}break;
case WM_DESTROY:
{
PostQuitMessage( 0 );
}break;
default:
{
return DefWindowProc(hWnd, uMsg, wParam,lParam);
}
}
} 展开
#include<Windows.h>
ATOM MyRegister(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WindowProc(
__in HWND hwnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
);
LPCWSTR lpInstanceName = TEXT("freeShortWindow");
HWND hWnd;
WNDCLASS wc;
int APIENTRY WinMain(
__in HINSTANCE hInstance,
__in HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nCmdShow
)
{
MyRegister(hInstance);
InitInstance(hInstance, nCmdShow);
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegister(HINSTANCE hInstance)
{
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_CROSS);
wc.hIcon = LoadIcon(NULL, IDI_SHIELD);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = lpInstanceName;
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW|CS_HREDRAW;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd = CreateWindow( lpInstanceName, lpInstanceName,
WS_OVERLAPPEDWINDOW,
100,100,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
return 1;
}
LRESULT CALLBACK WindowProc(
__in HWND hwnd,
__in UINT uMsg,
__in WPARAM wParam,
__in LPARAM lParam
)
{
switch(uMsg)
{
case WM_CREATE:
{}break;
case WM_PAINT:
{}break;
case WM_COMMAND:
{}break;
case WM_DESTROY:
{
PostQuitMessage( 0 );
}break;
default:
{
return DefWindowProc(hWnd, uMsg, wParam,lParam);
}
}
} 展开
1个回答
展开全部
这个是一个能用的版本。。自己对照一下吧。。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd= NULL;
WNDCLASS wc;
MSG msg;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW);
wc.hCursor =LoadCursor(NULL, IDC_ARROW);
wc.hIcon =LoadIcon(NULL, IDI_WINLOGO);
wc.hInstance =hInstance;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.lpszClassName ="WndClass";
wc.lpszMenuName =NULL;
wc.style =0;
RegisterClass(&wc);
hWnd= CreateWindow("WndClass", "XX", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);//放在这里
}
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd= NULL;
WNDCLASS wc;
MSG msg;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW);
wc.hCursor =LoadCursor(NULL, IDC_ARROW);
wc.hIcon =LoadIcon(NULL, IDI_WINLOGO);
wc.hInstance =hInstance;
wc.lpfnWndProc =(WNDPROC)WndProc;
wc.lpszClassName ="WndClass";
wc.lpszMenuName =NULL;
wc.style =0;
RegisterClass(&wc);
hWnd= CreateWindow("WndClass", "XX", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);//放在这里
}
追问
你用的是vc6.0吧?2010的字符串不能直接转成LPWSTR,所以编译会报错。
其实我自己也可以写一个正常显示的,但问题是有时候重新写它(用一样的风格)就会显示不出来~~
~~我还是看不出我代码里有什么问题啊~~
追答
你在工程属性里把字符集改成多字符集就好了,我用的VS2008
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询