用SDK创建一个简单的窗口
//=====================================================================================================
//TITLE:
// 用SDK创建一个简单的窗口
//AUTHOR:
// norains
//DATE:
// Friday 7-April-2006
//=====================================================================================================
在EVC编译环境下,不使用MFC框架创建一个极其简单的窗口----甚至连关闭按钮都没有,只有最简单的消息循环.
此代码分为两个文件,分别是:HelloWindow.h和HelloWindow.cpp;代码根据《WindowCE程序设计》一书的第一个代码例子进行精简。
/*-----------------HelloWindow.h----------------------*/
//----------------------------------------------------------------------
// Function prototypes
// Returns number of elements
int InitApp (HINSTANCE);
HWND InitInstance (HINSTANCE, LPWSTR, int);
int TermInstance (HINSTANCE, int);
// Window procedures
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);
// Message handlers
LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);
/*---------------HelloWindow.cpp----------------------*/
#include "stdafx.h"
#include "HelloWindow.h"
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("HelloCE");
HINSTANCE hInst; // Program instance handle
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
int rc = 0;
HWND hwndMain;
// init application
rc = InitApp (hInstance);
if (rc) return rc;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
return 0;
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 1;
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine,
int nCmdShow) {
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT("Hello"), // Window title
WS_VISIBLE, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create parameters
/*----------------------------------------------------------------------------------------------------
//------If you want the window not to display in the taskbar,you should use the following code.------//
hWnd=CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_NOACTIVATE, // Window type
szAppName, // Window class
TEXT("Hello"), // Window title
WS_POPUP, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL // Pointer to create parameters
);
-----------------------------------------------------------------------------------------------------*/
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam)
{
// Search message list to see if we need to handle this
// message. If in list, call procedure.
switch(wMsg)
{
case WM_DESTROY:
return DoDestroyMain(hWnd,wMsg,wParam,lParam);
default:
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}