用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;
}

阅读更多
换一批

只是用SDK创建一个窗口的小问题

06-19

从网上下了这个创建窗口的程序如下:(相信大家都写过)rn#include rn rnLRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ; rn rn///////////////////////////////////////////////////////////////////////// rn/*Windows程序则至少两个主程序,一个是WinMain(),另一个是窗口过程函数WndProc, rn它的函数原型为: rnlong FAR PASCAL WndProc(HWND hWnd,WORD message,WORD wParam,LONG lParam); rnWindows应用程序的编程就围绕这两个部份进行的。 rn其中WinMain函数为应用程序的入口点,它的名字一定要是WinMain。 rn*/ rn//////////////////////////////////////////////////////////////////////// rnint WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, rn PSTR szCmdLine, int iCmdShow) rn rn rn static TCHAR szAppName[] = TEXT ("HelloWin") ; rn HWND hwnd ; rn MSG msg ; rn////////////////////////建立,注册窗口类 rn WNDCLASS wc ; rn wc.style = CS_HREDRAW ; CS_VREDRAW ; rn wc.lpfnWndProc = WndProc ; rn wc.cbClsExtra = 0 ; rn wc.cbWndExtra = 0 ; rn wc.hInstance = hInstance ; rn wc.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; rn wc.hCursor = LoadCursor (NULL, IDC_ARROW) ; rn wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; rn wc.lpszMenuName = NULL ; rn wc.lpszClassName = szAppName ; rn rn//////////////////////注册窗口类 rn if (!RegisterClass (&wc)) rn rn MessageBox (NULL, TEXT ("This program requires Windows NT!"), rn szAppName, MB_ICONERROR) ; rn return 0 ; rn rn rn/////////////////////(二)创建窗口 rn hwnd = CreateWindow (szAppName, // window class name rn TEXT ("欢迎你的到来!"), // window caption rn WS_OVERLAPPEDWINDOW, // window style rn CW_USEDEFAULT, // initial x position rn CW_USEDEFAULT, // initial y position rn CW_USEDEFAULT, // initial x size rn CW_USEDEFAULT, // initial y size rn NULL, // parent window handle rn NULL, // window menu handle rn hInstance, // program instance handle rn NULL) ; // creation parameters rn rn rn/////////////////////(三)显示和更新窗口 rn ShowWindow (hwnd, iCmdShow); rn //ShowWindow (hwnd, SW_SHOWMINIMIZED) ;//显示最小化,还有其它的参数可参看MSDN rn //ShowWindow (hwnd, SW_SHOWMAXIMIZED) ;// 显示全屏,参数SW_MAXIMIZE也是显示全屏 rn UpdateWindow (hwnd) ; rn rn rn////////////////////(四)创建消息循环 rn while (GetMessage (&msg, NULL, 0, 0)) rn rn TranslateMessage (&msg) ; rn DispatchMessage (&msg) ; rn rn return msg.wParam ; rn rn rn////////////////////(六)窗口过程 rnLRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) rn rn HDC hdc ; rn PAINTSTRUCT ps ; rn RECT rect ; rn///////////////////(七)处理消息 rn switch (message) rn rn case WM_PAINT: rn hdc = BeginPaint (hwnd, &ps) ; rn GetClientRect (hwnd, &rect) ; rn // DrawText (hdc, TEXT ("你好,欢迎你来到VC之路!"), -1, &rect, rn // DT_SINGLELINE ; DT_CENTER ; DT_VCENTER ); rn EndPaint (hwnd, &ps) ; rn return 0 ; rn rn///////////////////(五)终止应用程序. rn rn case WM_DESTROY: rn PostQuitMessage (0) ; rn return 0 ; rn rn rn return DefWindowProc (hwnd, message, wParam, lParam) ; rn rnrnrnrn这段程序编译没错误,可link时就有错误了“unresolved external symbol _main”rn请问这是什么意思,怎么解决? 先谢了

没有更多推荐了,返回首页