#include "stdafx.h"
//----------------------------------
long _stdcall WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
// char ch[5];
switch(message)
{
case WM_PAINT:
hdc = ::BeginPaint(hWnd,&ps);
::GetClientRect(hWnd,&rect);
:EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
}
return::DefWindowProc(hWnd,message,wParam,lParam);
}
//--------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHAR szAppName[] = "MainForm";
MSG msg;
static HWND hWnd;
WNDCLASS wce;
RECT w;
POINT pt; wce.style = CS_HREDRAW | CS_VREDRAW;
wce.lpfnWndProc = (WNDPROC)WndProc;
wce.cbClsExtra = 0
wce.cbWndExtra = 0;
wce.hCursor = ::LoadCursor(NULL,IDC_ARROW);
wce.hbrBackground = (HBRUSH)(BLACK_BRUSH);
wce.lpszMenuName = NULL;
wce.lpszClassName = szAppName;
wce.hInstance = hInstance;
if(!RegisterClass(&wce))
{
::MessageBox(hWnd,TEXT("注册失败!"),szAppName,MB_ICONERROR);
return 0;
}
hWnd = CreateWindow(szAppName,"Main Form",WS_BORDER|WS_SYSMENU,0,0,//WS_POPUP
800,600,NULL,NULL,hInstance,NULL);
::GetClientRect(hWnd,&w);
pt.x=w.right-w.left;
pt.y=w.bottom-w.top;
MoveWindow(hWnd, (GetSystemMetrics(SM_CXFULLSCREEN)- pt.x)/2,
(GetSystemMetrics(SM_CYFULLSCREEN)- pt.y)/2,
pt.x, pt.y, TRUE);
if(!hWnd)
{
long i = GetLastError();
return 0;
}
::ShowWindow(hWnd,SW_SHOW);
::UpdateWindow(hWnd);
while(::GetMessage(&msg,NULL,0,0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}问题1:wce.hbrBackground = (HBRUSH)(BLACK_BRUSH); //窗口颜色为什么不是黑色?
问题2:我想以这个新建的窗口做为主窗口,但不知怎么样用WinMain函数里的 hWnd?我设置为全局变量,程序运行后会死
问题3:新建子窗口的wce.lpfnWndProc 怎么设置呢?是不是建一个和主窗口一样的回调函数?
问题4:新建子窗口的hInstance值从哪获得?怎么获得呢?本人比较菜,可能问的这些问题对于高手的您来说是小菜,请指教一二,谢谢
//----------------------------------
long _stdcall WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
// char ch[5];
switch(message)
{
case WM_PAINT:
hdc = ::BeginPaint(hWnd,&ps);
::GetClientRect(hWnd,&rect);
:EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
::PostQuitMessage(0);
break;
}
return::DefWindowProc(hWnd,message,wParam,lParam);
}
//--------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHAR szAppName[] = "MainForm";
MSG msg;
static HWND hWnd;
WNDCLASS wce;
RECT w;
POINT pt; wce.style = CS_HREDRAW | CS_VREDRAW;
wce.lpfnWndProc = (WNDPROC)WndProc;
wce.cbClsExtra = 0
wce.cbWndExtra = 0;
wce.hCursor = ::LoadCursor(NULL,IDC_ARROW);
wce.hbrBackground = (HBRUSH)(BLACK_BRUSH);
wce.lpszMenuName = NULL;
wce.lpszClassName = szAppName;
wce.hInstance = hInstance;
if(!RegisterClass(&wce))
{
::MessageBox(hWnd,TEXT("注册失败!"),szAppName,MB_ICONERROR);
return 0;
}
hWnd = CreateWindow(szAppName,"Main Form",WS_BORDER|WS_SYSMENU,0,0,//WS_POPUP
800,600,NULL,NULL,hInstance,NULL);
::GetClientRect(hWnd,&w);
pt.x=w.right-w.left;
pt.y=w.bottom-w.top;
MoveWindow(hWnd, (GetSystemMetrics(SM_CXFULLSCREEN)- pt.x)/2,
(GetSystemMetrics(SM_CYFULLSCREEN)- pt.y)/2,
pt.x, pt.y, TRUE);
if(!hWnd)
{
long i = GetLastError();
return 0;
}
::ShowWindow(hWnd,SW_SHOW);
::UpdateWindow(hWnd);
while(::GetMessage(&msg,NULL,0,0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}问题1:wce.hbrBackground = (HBRUSH)(BLACK_BRUSH); //窗口颜色为什么不是黑色?
问题2:我想以这个新建的窗口做为主窗口,但不知怎么样用WinMain函数里的 hWnd?我设置为全局变量,程序运行后会死
问题3:新建子窗口的wce.lpfnWndProc 怎么设置呢?是不是建一个和主窗口一样的回调函数?
问题4:新建子窗口的hInstance值从哪获得?怎么获得呢?本人比较菜,可能问的这些问题对于高手的您来说是小菜,请指教一二,谢谢
----------------------------------------------------------------
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;