您的位置:首页 > 其它

windows程序设计 学习记录1

2013-09-14 11:59 375 查看
刚开始的入门程序,在窗口中输出Hello windows 98!,窗口名是hellomsg。

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MessageBox(NULL,TEXT("Hello Windows 98!"), TEXT("HelloMsg") , 0 );
return 0;
}


//

调用几个API函数,输出屏幕的像素。

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
TCHAR szBuffer [1024];
va_list pArgList;

va_start (pArgList, szFormat);

_vsntprintf (szBuffer, sizeof(szBuffer)/sizeof(TCHAR), szFormat, pArgList );

va_end (pArgList);

return MessageBox(NULL, szBuffer, szCaption, 0);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
int cxScreen, cyScreen;

cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);

MessageBoxPrintf(TEXT("ScrnSize"),
TEXT("The screen is %i pixels wide by %i pixels high."),
cxScreen, cyScreen);
return 0;
}


//

用createwindow创建一个多功能的窗口,用消息队列处理从交互中获得的信息。

#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
static TCHAR szAppName[]= TEXT ("HelloWin");

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd;               /* This is the handle for our window */
MSG msg;            /* Here messages to the application are saved */
WNDCLASS wndclass;        /* Data structure for the windowclass */

/* The Window structure */
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szAppName;
wndclass.lpfnWndProc = WndProc;      /* This function is called by windows */
wndclass.style = CS_HREDRAW | CS_VREDRAW;                 /* Catch double-clicks */
//wndclass.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
//wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.lpszMenuName = NULL;                 /* No menu */
wndclass.cbClsExtra = 0;                      /* No extra bytes after the window class */
wndclass.cbWndExtra = 0;                      /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

/* Register the window class, and if it fails quit the program */
if (!RegisterClass (&wndclass)){
MessageBox (NULL,TEXT("This program requires Windows NT!"),
szAppName,MB_ICONERROR);
return 0;
}

/* The class is registered, let's create the program*/
hwnd = CreateWindow (
/* Extended possibilites for variation */
szAppName,         /* Classname */
TEXT("The Hello Program"),       /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT,       /* Windows decides the position */
CW_USEDEFAULT,       /* where the window ends up on the screen */
CW_USEDEFAULT,                 /* The programs width */
CW_USEDEFAULT,                 /* and height in pixels */
NULL,        /* The window is a child-window to desktop */
NULL,                /* No menu */
hInstance,       /* Program Instance handler */
NULL                 /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, iCmdShow);
UpdateWindow(hwnd);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&msg, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&msg);
/* Send message to WindowProcedure */
DispatchMessage(&msg);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return msg.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch (message)                  /* handle the messages */
{
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:
PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
return 0;
//default:                      /* for messages that we don't deal with */
//  return DefWindowProc (hwnd, message, wParam, lParam);
}

return DefWindowProc(hwnd, message, wParam, lParam);
}


//

各种函数的名称,返回类型,初始化的变量还不懂,太多太乱。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: