Windows SDK 学习笔记(1)
#include "windows.h"
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MessageBox(NULL,TEXT("WinDebug"),TEXT("WWWW"),0);
return 0;
}
hInstance:应用程序的当前实例句柄
hPrevInstance:应用程序的前一个实例句柄,别管它,对于Win32位而言,它一般是NULL.
szCmdLine:指向任何传给程序的命令行参数。PSTR代表"指向字符串的指针"。
iCmdShow:它告诉应用程序如何初始化窗口,如最大化,最小化等状态。
几个要点:
1.句柄(handle):在标准C库中句柄用来对文件输入输出。如下面的代码:
int handle;
handle=open("filename","r")
if(handle)
{
read(handle,block,bytesToread);
}
close(handle);
在文件被成功打开后,open()返回一个句柄,在read()中使用这个句柄来阅读这个文件。句柄不是指针。程序不能直接使用句柄来阅读文件中的信息。如果不能把它传送给输入输出函数调用的话,句柄就没有用了。句柄不返回零。句柄命名以h开始。这是匈牙利表示法的规定。
#include "stdafx.h"
#include "windows.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wc ;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = WndProc ;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = 0 ;
wc.hInstance = hInstance ;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wc.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wc.lpszMenuName = NULL ;
wc.lpszClassName = szAppName ;
if (!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // windows 类名
TEXT ("Windows Win32 App!"), // 窗口标题
WS_OVERLAPPEDWINDOW, // 窗口样式
CW_USEDEFAULT, // initial x position 初始X位置
CW_USEDEFAULT, // initial y position 初始Y位置
CW_USEDEFAULT, // initial x size X大小
CW_USEDEFAULT, // initial y size Y大小
NULL, // parent window handle 父窗体句柄
NULL, // window menu handle 菜单句柄
hInstance, // program instance handle 实例句柄
NULL) ; // creation parameters 参数
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
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_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("测试windows SDK 文字!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
//stdafx.h
#pragma once
#include <iostream>
#include <tchar.h>
浙公网安备 33010602011771号