/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szClassName[] = TEXT ("MyWindow") ;
static TCHAR szWindowsName1[] = TEXT ("Window1") ;
static TCHAR szWindowsName2[] = TEXT ("Window2") ;
HWND hwnd1,hwnd2 ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szClassName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szClassName, MB_ICONERROR) ;
return 0 ;
}
hwnd1 = CreateWindow (szClassName, // window class name
TEXT ("window1"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
hwnd2 = CreateWindow (szClassName, // window class name
TEXT ("windows2"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd1, iCmdShow) ;
ShowWindow (hwnd2, iCmdShow) ;
UpdateWindow (hwnd1) ;
UpdateWindow (hwnd2) ;
while(GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
//這段是利用winmain中來檢測兩個視窗是否關閉
// while(1)
//{
// if (GetMessage (&msg, NULL, 0, 0))
// {
// if(hwnd1==0 && hwnd2==0)
// {
// if(msg.message==WM_QUIT)
// {
// break;
// }
// }
// TranslateMessage (&msg) ;
// DispatchMessage (&msg) ;
// }
//}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
//PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
//if(hwnd==hwnd1)
//{
// hwnd1=0;
//}
//else
//{
// hwnd2=0;
//}
//這種做法也可以在回呼函式中檢查兩個視窗是否關閉
//if(hwnd1==0 && hwnd2==0)
//{
// PostQuitMessage(0);
//}
PostQuitMessage(0);
return 0 ;
return DefWindowProc (hwnd, message, wParam, lParam);
}
}
uj5u.com熱心網友回復:
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <stdio.h>
#include <math.h>
// DEFINES ////////////////////////////////////////////////
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
// GLOBALS ////////////////////////////////////////////////
//
HWND m_hwnd1=0;
HWND m_hwnd2=0;
// FUNCTIONS //////////////////////////////////////////////
HBRUSH g_hYellow;
HBRUSH g_hPink;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
static BOOL FirstWin=TRUE;
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
RECT rc;
HDC hdc; // handle to a device context
// what is the message
switch(msg)
{
case WM_CREATE:
// do initialization stuff here
g_hYellow=CreateSolidBrush(RGB(255,255,0));//yellow
g_hPink=CreateSolidBrush(RGB(255,0,255)); //pink
// with CS_OWNDC ,put here !
hdc=GetDC(hwnd);
SetBkMode(hdc,TRANSPARENT);
if(FirstWin)
{
FirstWin=FALSE;
SetTextColor(hdc,RGB(255,0,255));
}
else
{
SetTextColor(hdc,RGB(0,0,0));
}
// return success
return 1;
//
case WM_ERASEBKGND:
hdc = (HDC) wparam;
GetClientRect(hwnd,&rc);
// When creating m_hwnd1-2 may = NULL !
if(hwnd==m_hwnd1)
{// " Window 1 “
FillRect(hdc,&rc,g_hYellow);
}
else if(hwnd==m_hwnd2)
{ // " Window 2“
FillRect(hdc,&rc,g_hPink);
}
return 1;// break;
//
case WM_PAINT:
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// you would do all your painting here
if(m_hwnd1==hwnd)
{// Client Rect 400,400
TextOut(hdc,80,160,"Window 1 Based on WINCLASS1",27);
TextOut(hdc,80,180," Client Rect 400,400",27);
}
else
{// Window Rect 400,400
TextOut(hdc,80,160,"Window 2 Based on WINCLASS1",27);
TextOut(hdc,80,180," Window Rect 400,400",27);
}
EndPaint(hwnd,&ps);
// return success
return 1;
////
case WM_DESTROY:
if(m_hwnd1 && m_hwnd2) // there are 2 windows, one of them closes!
{// do not quit !
if(hwnd==m_hwnd1) m_hwnd1=0;
else m_hwnd2=0;
return 1;
}
PostQuitMessage(0);
return 1;
} // end switch
// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{// CS_OWNDC: 給予每個視窗實體它本身的DC。注意,盡管這樣是很方便,
// 但它必須慎重使用,因為每個DC大約要占800個位元組的記憶體。
WNDCLASSEX winclass; // this will hold the class we create
MSG msg; // generic message
// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;// ::DefWindowProc
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// register the window class
if (!RegisterClassEx(&winclass)) return(0);
// get window rect
RECT rc={100,100,500,500};
AdjustWindowRectEx(&rc,WS_OVERLAPPEDWINDOW,FALSE,NULL);
// create the first window
if (!(m_hwnd1 = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"Window 1 Based on WINCLASS1", // title
WS_OVERLAPPEDWINDOW,// | WS_VISIBLE,// 1
rc.left,rc.top, // initial x,y
rc.right-rc.left,rc.bottom-rc.top, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
{
return(0);
}
// 1
ShowWindow(m_hwnd1,SW_SHOW);
// create the second window
if (!(m_hwnd2 = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"Window 2 Also Based on WINCLASS1", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,// 2
500,100, // initial x,y
400,400, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance, // instance of this application
NULL))) // extra creation parms
{
return(0);
}
// 2
InvalidateRect(m_hwnd2,0,TRUE);
#if 0 // no effects !
WNDPROC OldWnd2Proc = (WNDPROC)SetWindowLong(m_hwnd2, GWL_WNDPROC, (LONG)Wnd2Proc);
SetProp(m_hwnd2 , // handle of window
"OldWnd2Proc", // atom or address of string
OldWnd2Proc);
#endif
// enter main event loop, but this time we use PeekMessage()
// instead of GetMessage() to retrieve messages
while(TRUE)
{// test if there is a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{ // test if this is a quit
if (msg.message == WM_QUIT) break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end if
// main game processing goes here
} // end while
// return to Windows like this
return(msg.wParam);
} // end WinMain
case WM_DESTROY:
if(m_hwnd1 && m_hwnd2) // there are 2 windows, one of them closes!
{// do not quit !
if(hwnd==m_hwnd1) m_hwnd1=0;
else m_hwnd2=0;
return 1;
}
PostQuitMessage(0);
return 1;
uj5u.com熱心網友回復:
不要發送PostQuitMessage(0);WinMain就不會退出
定義一個全域變數
int mWinNum=0;
訊息處理函式中
case WM_CREATE:
....
mWinNum++;
......
case WM_DESTROY:
mWinNum--;
if (mWinNum==0)
PostQuitMessage(0);
......
uj5u.com熱心網友回復:
if(!IsWindow(hWnd1) && !IsWidow(hWnd2))PostQuitMessage(0);
uj5u.com熱心網友回復:
#2 樓代碼可行,多一個變數。2個視窗,以上 方便。#3 樓的代碼 , 好像沒有真正退出。
uj5u.com熱心網友回復:
#3 樓的代碼 缺:The program 'C:\test\twownds\Debug\twownds.exe' has exited with code 0 (0x0).
uj5u.com熱心網友回復:
#if 1 // ???
if(hwnd==g_hwnd1) g_hwnd1=0;
else g_hwnd2=0;
// test
int b1=IsWindow(g_hwnd1);
int b2=IsWindow(g_hwnd2);
char prompt[80];
sprintf(prompt,"%d;%d\n",b1,b2);// 1;1 // 1;0
OutputDebugString(prompt);
//
if((!IsWindow(g_hwnd1)) && (!IsWindow(g_hwnd2)))
PostQuitMessage(0);
#endif
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/123988.html
標籤:基礎類
上一篇:vc2010檔案打開總是失敗?
下一篇:如何學習
